【问题标题】:Instatiate prefab Unity3D实例化预制 Unity3D
【发布时间】:2016-05-07 19:06:20
【问题描述】:

我有 5 个预制件作为玩家选项,我的游戏菜单上有一个“更改”按钮,用户可以在其中单击,更改角色。

如何在game2d启动时应用的“菜单”中做出这个选择?

预制件:

Bee、Bee1、Bee2、Bee3 和 Bee4。

蜜蜂(玩家)脚本。

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityStandardAssets.CrossPlatformInput;

public class Bee : MonoBehaviour {

    public float moveSpeed;

    public Transform bee;
    private Animator animator;

    public bool isGrounded = true;
    public float force;

    public float jumpTime = 0.1f;
    public float jumpDelay = 0.1f;
    public bool jumped = false;
    public Transform ground;


    // Use this for initialization
    void Start ()
    {
        animator = bee.GetComponent<Animator> ();
    }

    void Update ()
    {
        Move ();

    }


    void Move ()
    {

        isGrounded = Physics2D.Linecast (this.transform.position, ground.position, 1 << LayerMask.NameToLayer ("Floor"));
        animator.SetFloat ("runB", Mathf.Abs (CrossPlatformInputManager.GetAxis ("Horizontal")));
        if (CrossPlatformInputManager.GetAxisRaw ("Horizontal") > 0) {

            transform.Translate (Vector2.right * moveSpeed * Time.deltaTime);
            transform.eulerAngles = new Vector2 (0, 0);
        }
        if (CrossPlatformInputManager.GetAxisRaw ("Horizontal") < 0) {

            transform.Translate (Vector2.right * moveSpeed * Time.deltaTime);
            transform.eulerAngles = new Vector2 (0, 180);
        }

        if (CrossPlatformInputManager.GetButtonDown ("Vertical") && isGrounded && !jumped) {

            //  rigidbody2D.AddForce (transform.up * force);
            GetComponent<Rigidbody2D> ().AddForce (transform.up * force);
            jumpTime = jumpDelay;
            animator.SetTrigger ("jumpB");
            jumped = true;
        }

        jumpTime -= Time.deltaTime;

        if (jumpTime <= 0 && isGrounded && jumped) {

            animator.SetTrigger ("groundB");
            jumped = false;

        }

    }

}   

【问题讨论】:

  • 预制件有什么区别?例如。如果它只是在视觉效果上有所不同,您应该使用一个预制件并让它的某个子级保持视觉效果(例如精灵)并且只改变它。
  • @Gunnar B. 只改变精灵,其他都一样!

标签: c# unity3d unityscript unity5 unity3d-2dtools


【解决方案1】:

也许激活你需要的那个并停用所有其他的就足够了:

That tutorial can help you to do that

【讨论】:

    【解决方案2】:

    我建议将播放器的视觉部分移动到子对象中,但这不是必需的。如果你这样做了,你的根玩家对象可以只是一个空的游戏对象。

    如果您有场景更改,您将需要在场景之间保持不变的东西,例如带有DontDestroyOnLoad 的单身人士。无论如何,这必须有关于所有可能的精灵的信息,例如在您使用Resources.Load/LoadAll 填充的列表中。您可以使用此列表在菜单中显示精灵。选择后保存索引并在游戏开始时将玩家精灵更改为所选的类似这样的内容(这将在此类中,例如称为GameController):

    playerGO.GetComponentInChildren<SpriteRenderer>().sprite = spritesList[selected];
    

    (这是假设视觉对象位于子对象上。)

    【讨论】:

      猜你喜欢
      • 2014-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-28
      • 2015-10-16
      • 2021-03-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多