我通过代码创建了一个切换,但它不会显示.此外,我无法改变文本字段的位置.我尝试了很多,没有任何作用.这是我目前的版本,也许你看错了.
我是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);
}
}
Run Code Online (Sandbox Code Playgroud)