Перед использованием ui canvas в сцене обязательно необходимо подключить строкой библиотеки этого интерфейса:
1 |
using UnityEngine.UI; |
В одном проекте необходимо было скриптом создавать канвасы и необходимые элементы, например текст. Вот как я это реализовал:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
private GameObject myGO; private GameObject childGO; void Start () { // create game object and child object myGO = new GameObject (); childGO = new GameObject (); // give them names for fun myGO.name = "TestCanvas"; childGO.name = "ChildObject"; // set the child object as a child of the parent childGO.transform.parent = myGO.transform; // add a canvas to the parent myGO.AddComponent<Canvas> (); // add a recttransform to the child childGO.AddComponent<RectTransform> (); // make a reference to the parent canvas and use the ref to set its properties Canvas myCanvas = myGO.GetComponent<Canvas> (); myCanvas.renderMode = RenderMode.ScreenSpaceOverlay; // add a text component to the child childGO.AddComponent<Text> (); // make a reference to the child rect transform and set its values RectTransform childRectTransform = childGO.GetComponent<RectTransform> (); RectTransform parentRectTransform = myGO.GetComponent<RectTransform> (); // set child anchors for resizing behaviour childRectTransform.anchoredPosition3D = new Vector3(0f,0f,0f); childRectTransform.sizeDelta = new Vector2 (0f, 0f); childRectTransform.anchorMin = new Vector2 (0f,0f); childRectTransform.anchorMax = new Vector2 (1f, 1f); // set text font type and material at runtime from font stored in Resources folder Text textComponent = childGO.GetComponent<Text> (); Material newMaterialRef = Resources.Load<Material> ("3DTextCoolVetica"); Font ArialFont = (Font)Resources.GetBuiltinResource(typeof(Font), "Arial.ttf"); textComponent.font = ArialFont; textComponent.material = ArialFont.material; // set the font text textComponent.text = "Ошибка соединения с базой"; } |
Так же, если необходимо просто добавить текст в существующий канвас:
1 2 3 4 5 6 7 |
void Start() { GameObject newGO = new GameObject("myTextGO"); newGO.transform.SetParent(this.transform); Text myText = newGO.AddComponent<Text>(); myText.text = "Всем привет!"; } |
Хочу обратить особое внимание на фонт, так как в начале я его не задавал и мой текст не отображался. Необходимо обязательное наличие фонта:
1 2 |
Font ArialFont = (Font)Resources.GetBuiltinResource(typeof(Font), "Arial.ttf"); text.font = ArialFont; |
Еще один скрипт, может кому пригодится:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
using UnityEngine; using System.Collections.Generic; using UnityEngine.UI; public class TestScript : MonoBehaviour { public GameObject myGO; void Start () { myGO = new GameObject (); myGO.name = "TestCanvas"; myGO.AddComponent<Canvas> (); Canvas myCanvas = myGO.GetComponent<Canvas> (); myCanvas.renderMode = RenderMode.ScreenSpaceOverlay; myGO.AddComponent<Text> (); Text textComponent = myGO.GetComponent<Text> (); Material newMaterialRef = Resources.Load<Material> ("3DTextCoolVetica"); Font myFont = Resources.Load<Font> ("coolvetica rg"); textComponent.font = myFont; textComponent.material = newMaterialRef; textComponent.text = "Hello World"; myGO.AddComponent<Slider> (); } } |