当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;
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;
}
}
}
如果场景发生变化,您将需要在场景之间保持持久的东西,例如带有
DontDestroyOnLoad
的单例。无论如何,这必须具有有关所有可能的精灵的信息,例如在您用
Resources.Load/LoadAll
填充的列表中。您可以使用此列表在菜单中显示精灵。选择后,您保存索引,并在游戏开始时,只需将玩家精灵更改为选定的精灵,如下所示(这将在此类中,例如称为
GameController
):
playerGO.GetComponentInChildren<SpriteRenderer>().sprite = spritesList[selected];
(假设视觉效果位于子对象上。)