腳本創建文本
新建文件夾 Resources
方便引用字體
在文件Resources中新建Fonts,並且下載一個ttf字體
沒有字體,文本內容無法顯示
腳本如下
public GameObject objprent;
public string objName;
void Start()
{
GameObject obj =new GameObject("文本");
obj.transform.SetParent(objprent.transform,false);
Text text = obj.AddComponent<Text>();
text.text="這是你創建出來的"+objName;
obj.transform.Translate(new Vector2(0f,200f));
text.font=Resources.Load("Fonts/123456",typeof(Font)) as Font;
}
拖拽一下,確認父物體
結果