我通过代码创建了一个切换但不会显示。 此外,我无法改变文本字段的位置。 我尝试了很多,没有任何作用。这是我目前的版本,也许你看错了。
我是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);
}
}
答案 0 :(得分:8)
你应该让Toggle
成为Canvas
的孩子。您没有在代码中执行此操作。此外,您移动了一个UI组件和GameObject,而newGO.GetComponent<RectTransform>().anchoredPosition3D
不是newGO.transform.position
。
在Unity中创建完整的UI控件有三种方法:
1.使用DefaultControls
API生成它(简单和推荐)
使用DefaultControls
类,Unity将创建提供的UI,然后返回UI的父级。这是最简单和推荐的方法。
它需要DefaultControls.Resources
作为参数,以便您可以在创建默认UI控件时提供要使用的精灵。
按钮:
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 prefab和Instantiate
此方法要求您已创建UI并将其保存为预制件,然后您可以在需要时Instantiate
使用UI。
从编辑器创建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.Piece by Piece(Hard)
首先从编辑器创建UI,然后在编辑器中研究附加到它的层次结构和组件,并通过代码重现它。
游戏物体 - &GT; UI - &GT; 切换
例如,这就是Toggle的样子:
1 。创建一个切换 GameObject,然后将其作为 Canvas 的子项。
2 。创建背景 GameObject,然后将其作为 Toggle GameObject的子项。
3 。创建一个 Checkmark GameObject,然后将其作为 Background GameObject的子项。
4 。创建一个 Label GameObject,然后将其作为 Toggle GameObject的子项。
5 。现在将Image
,Text
和Toggle
等组件附加到每个GameObject中,就像它出现在编辑器中一样。
在代码中:
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 在这种情况下应该没问题。