Unity从脚本创buildUI控制

我通过代码创build了一个切换,但不会显示。 而且,我不能改变我的文本字段的位置。 我尝试了很多,没有任何工作。 这是我现在的版本,也许你看到了错误。

我对Unity很陌生,这非常困难。

public class Game : MonoBehaviour { public GameObject canvas; void Start () { GameObject newGO = new GameObject("myTextGO"); newGO.transform.SetParent(this.transform); newGO.transform.position = new Vector3(0, 0, 0); Text myText = newGO.AddComponent<Text>(); myText.text = "Ta-dah!"; Font ArialFont = (Font)Resources.GetBuiltinResource(typeof(Font),"Arial.ttf"); myText.font = ArialFont; myText.material = ArialFont.material; myText.color = Color.black; myText.transform.position = new Vector3(0, 10, 0); GameObject secGO = new GameObject("myGO"); secGO.transform.SetParent(this.transform); Toggle myToggle = secGO.AddComponent<Toggle>(); myToggle.isOn = true; myToggle.transform.position = new Vector3(10, 10, 0); } } 

你应该做的Toggle Canvas的孩子。 你没有在你的代码中这样做。 另外,你用newGO.GetComponent<RectTransform>().anchoredPosition3D移动一个UI组件和newGO.GetComponent<RectTransform>().anchoredPosition3D不是newGO.transform.position

有三种方法可以在Unity中创build完整的UI控件:

1.使用DefaultControls API生成它(简单和推荐)

使用DefaultControls类,Unity将创build提供的UI,然后返回UI的父代。 这是做这个最简单和推荐的方法。 它将DefaultControls.Resources作为参数,以便您可以提供创build默认UI控件时使用的精灵。

button:

 public GameObject canvas; void Start() { DefaultControls.Resources uiResources = new DefaultControls.Resources(); //Set the Button Background Image someBgSprite; uiResources.standard = someBgSprite; GameObject uiButton = DefaultControls.CreateButton(uiResources); uiButton.transform.SetParent(canvas.transform, false); } 

切换:

 public GameObject canvas; void Start() { DefaultControls.Resources uiResources = new DefaultControls.Resources(); //Set the Toggle Background Image someBgSprite; uiResources.background = someBgSprite; //Set the Toggle Checkmark Image someCheckmarkSprite; uiResources.checkmark = someCheckmarkSprite; GameObject uiToggle = DefaultControls.CreateToggle(uiResources); uiToggle.transform.SetParent(canvas.transform, false); } 

滑块:

 public GameObject canvas; void Start() { DefaultControls.Resources uiResources = new DefaultControls.Resources(); //Set the Slider Background Image someBgSprite; uiResources.background = someBgSprite; //Set the Slider Fill Image someFillSprite; uiResources.standard = someFillSprite; //Set the Slider Knob Image someKnobSprite; uiResources.knob = someKnobSprite; GameObject uiSlider = DefaultControls.CreateSlider(uiResources); uiSlider.transform.SetParent(canvas.transform, false); } 

面板:

 public GameObject canvas; void Start() { DefaultControls.Resources uiResources = new DefaultControls.Resources(); //Set the Panel Background Image someBgSprite; uiResources.background = someBgSprite; GameObject uiPanel = DefaultControls.CreatePanel(uiResources); uiPanel.transform.SetParent(canvas.transform, false); } 

InputField:

 public GameObject canvas; void Start() { DefaultControls.Resources uiResources = new DefaultControls.Resources(); //Set the InputField Background Image someBgSprite; uiResources.inputField = someBgSprite; GameObject uiInputField = DefaultControls.CreateInputField(uiResources); uiInputField.transform.SetParent(canvas.transform, false); uiInputField.transform.GetChild(0).GetComponent<Text>().font = (Font)Resources.GetBuiltinResource(typeof(Font), "Arial.ttf"); } 

落下:

 public GameObject canvas; void Start() { DefaultControls.Resources uiResources = new DefaultControls.Resources(); //Set the Dropdown Background and Handle Image someBgSprite; uiResources.standard = someBgSprite; //Set the Dropdown Scrollbar Background Image someScrollbarSprite; uiResources.background = someScrollbarSprite; //Set the Dropdown Image someDropDownSprite; uiResources.dropdown = someDropDownSprite; //Set the Dropdown Image someCheckmarkSprite; uiResources.checkmark = someCheckmarkSprite; //Set the Dropdown Viewport Mask Image someMaskSprite; uiResources.mask = someMaskSprite; GameObject uiDropdown = DefaultControls.CreateDropdown(uiResources); uiDropdown.transform.SetParent(canvas.transform, false); } 

其余的UI控件:

 public static GameObject CreateImage(Resources resources); public static GameObject CreateRawImage(Resources resources); public static GameObject CreateScrollbar(Resources resources); public static GameObject CreateScrollView(Resources resources); public static GameObject CreateText(Resources resources); 

3.Via预制和实例化

此方法要求您已将UI创build并保存为预制,然后可以在需要时Instantiate UI。

从编辑器创build一个Toggle控制,然后将其保存为预制。 删除原来的一个。 然后,您可以在运行时间位置Instantiate Toggle控制预制,并在需要时对其进行缩放。

 public GameObject canvas; public GameObject togglePrefab; void Start() { GameObject uiToggle = Instantiate(togglePrefab) as GameObject; uiToggle.transform.SetParent(canvas.transform, false); //Move to another position? uiToggle.GetComponent<RectTransform>().anchoredPosition3D = new Vector3(...,...,...); //Re-scale? uiToggle.GetComponent<RectTransform>().localScale = new Vector3(...,...,...); } 

2.一件一件(硬)

首先通过编辑器创build一个用户界面,然后在编辑器中研究连接到它的层次结构和组件,并通过代码重现它。

游戏对象 – > 用户界面 – > 切换

例如,这就是Toggle的样子:

在这里输入图像描述

创build一个切换 GameObject,然后使其成为canvas的子项。

2.创build一个后台游戏对象,然后使其成为切换游戏对象的子对象。

3.创build一个Checkmark GameObject,然后使其成为Background GameObject的子项。

4.创build一个标签 GameObject,然后使其成为Toggle GameObject的子类。

5.现在,像编辑器一样附加像ImageTextToggle等组件。

在代码中:

 public GameObject canvas; void Start() { makeToggle(); } void makeToggle() { GameObject toggleObj = createToggleObj(canvas); GameObject bgObj = createBackgroundObj(toggleObj); GameObject checkMarkObj = createCheckmarkObj(bgObj); GameObject labelObj = createLabelObj(toggleObj); attachAllComponents(toggleObj, bgObj, checkMarkObj, labelObj); } //1.Create a *Toggle* GameObject then make it child of the *Canvas*. GameObject createToggleObj(GameObject cnvs) { GameObject toggle = new GameObject("Toggle"); toggle.transform.SetParent(cnvs.transform); toggle.layer = LayerMask.NameToLayer("UI"); return toggle; } //2.Create a Background GameObject then make it child of the Toggle GameObject. GameObject createBackgroundObj(GameObject toggle) { GameObject bg = new GameObject("Background"); bg.transform.SetParent(toggle.transform); bg.layer = LayerMask.NameToLayer("UI"); return bg; } //3.Create a Checkmark GameObject then make it child of the Background GameObject. GameObject createCheckmarkObj(GameObject bg) { GameObject chmk = new GameObject("Checkmark"); chmk.transform.SetParent(bg.transform); chmk.layer = LayerMask.NameToLayer("UI"); return chmk; } //4.Create a Label GameObject then make it child of the Toggle GameObject. GameObject createLabelObj(GameObject toggle) { GameObject lbl = new GameObject("Label"); lbl.transform.SetParent(toggle.transform); lbl.layer = LayerMask.NameToLayer("UI"); return lbl; } //5.Now attach components like Image, Text and Toggle to each GameObject like it appears in the Editor. void attachAllComponents(GameObject toggle, GameObject bg, GameObject chmk, GameObject lbl) { //Attach Text to label Text txt = lbl.AddComponent<Text>(); txt.text = "Toggle"; Font arialFont = (Font)Resources.GetBuiltinResource(typeof(Font), "Arial.ttf"); txt.font = arialFont; txt.lineSpacing = 1; txt.color = new Color(50 / 255, 50 / 255, 50 / 255, 255 / 255); RectTransform txtRect = txt.GetComponent<RectTransform>(); txtRect.anchorMin = new Vector2(0, 0); txtRect.anchorMax = new Vector2(1, 1); //txtRect.y //Attach Image Component to the Checkmark Image chmkImage = chmk.AddComponent<Image>(); chmkImage.sprite = (Sprite)AssetDatabase.GetBuiltinExtraResource(typeof(Sprite), "UI/Skin/Checkmark.psd"); chmkImage.type = Image.Type.Simple; //Attach Image Component to the Background Image bgImage = bg.AddComponent<Image>(); bgImage.sprite = (Sprite)AssetDatabase.GetBuiltinExtraResource(typeof(Sprite), "UI/Skin/UISprite.psd"); bgImage.type = Image.Type.Sliced; RectTransform bgRect = txt.GetComponent<RectTransform>(); bgRect.anchorMin = new Vector2(0, 1); bgRect.anchorMax = new Vector2(0, 1); //Attach Toggle Component to the Toggle Toggle toggleComponent = toggle.AddComponent<Toggle>(); toggleComponent.transition = Selectable.Transition.ColorTint; toggleComponent.targetGraphic = bgImage; toggleComponent.isOn = true; toggleComponent.toggleTransition = Toggle.ToggleTransition.Fade; toggleComponent.graphic = chmkImage; toggle.GetComponent<RectTransform>().anchoredPosition3D = new Vector3(0, 0, 0); } 

方法#3是最难的方法,所以你应该避免它。 方法#1在这种情况下应该罚款。