iLMS知識社群歷程檔登入
位置: 黃國哲 > Unity
1110319 尋找子物件用法transform.Find
by 黃國哲 2022-03-19 15:41:52, 回應(0), 人氣(340)

使用語法transform.Find

應用如下:

public string buttonText;

transform.Find ("Text").GetComponent<Text>().text = buttonText; 
//這句可以把它底下的Text文字內容替換成buttonText輸入的內容

將這個腳本套用在按鈕上,可以讓按鈕父物件的文字框的內容變成子物件Text裡面輸入的文字

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

[ExecuteInEditMode] //不用進去執行也能在編輯模式中執行

public class 子物件transformfind  : MonoBehaviour {

public string buttonText;
// Use this for initialization
void Start () 
{
transform.Find ("Text").GetComponent<Text>().text = buttonText; 
//這句可以把它底下的Text文字內容替換成buttonText輸入的內容
}

// Update is called once per frame
void Update () 
{
transform.Find ("Text").GetComponent<Text>().text = buttonText;
}

}

回應