publicclassPlayerController: MonoBehaviour
{// Start is called before the first frame updatevoidStart(){}// Update is called once per framevoidUpdate(){//得到軸
float vertical = Input.GetAxis("Vertical");
float horizontal = Input.GetAxis("Horizontal");
Vector3 dir =newVector3(horizontal,0, vertical);//如果向量不爲零if(dir != Vector3.zero){//朝向量方向前進,每秒3米
transform.Translate(dir *3* Time.deltaTime);}}}
代碼:金幣CoinController : MonoBase
publicclassCoinController: MonoBase
{// Start is called before the first frame updatevoidStart(){}// Update is called once per framevoidUpdate(){}//觸發privatevoidOnTriggerEnter(Collider other){if(other.CompareTag("Player")){//加分。組件想調用其它功能,就用SendCustomMessage(消息類型,什麼命令,添加幾分)SendCustomMessage(MessageType.Type_UI, MessageType.UI_AddScore,1);//銷燬Destroy(gameObject);}}}
//因爲使用UI,所以添加名稱空間
using UnityEngine.UI;//相當於紫色部分,修改繼承,可以接收消息。publicclassScoreController: MonoBase
{public Text text;private int score =0;voidStart(){//將當前類註冊到UI管理器類中,這樣才能接收消息。把this放在這裏
UIManager.Instance.RegisterReceiver(this);}//拿到消息public override voidReceiveMessage(Message message){
base.ReceiveMessage(message);//判斷得到的命令是不是“增加分數”if(message.Command == MessageType.UI_AddScore){//添加分數//添加的分數當做Content(參數)傳進來,並強轉成int類型。//Content此時表示你添加的分數數值。
int add =(int)message.Content;//增加分數
score += add;//刷新。轉成字符串
text.text = score +"";}}}
代碼:小地圖 MapController : MonoBase
publicclassMapController: MonoBase
{// Start is called before the first frame updatevoidStart(){//將當前類註冊到UI管理器類中,這樣才能接收消息。把this放在這裏
UIManager.Instance.RegisterReceiver(this);}// Update is called once per framevoidUpdate(){}}