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(){}}