
可以在Canvas中建立一個空物件 右鍵 Create Empty 取名為UI程式的控制器
就可以統一相關類似UI的程式放在這裡
選擇Empty,改名後再下放的Add Component 打上你要取名的程式碼 ChangeText後
按下new script並選擇 C#的版本,新版的好像已經不支援JS了

以下為程式碼
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; //UI物件需要增加此行
public class ChangeText : MonoBehaviour {
public Text UItext; //要設公開的才能從外部加入UItext
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void changeUItext(){ //設公開的才能利用Unity內建Button觸發方式來喚出
UItext.text = "按下按鈕了";
}
}



結果如下


明天再新增直接用程式碼觸發按,各有優缺,但程式碼裡面寫好處的自由度可控性更高。
@copyright MRcodingRoom
觀看更多文章請點MRcoding筆記
觀看更多文章請點MRcoding筆記