在 Unity 运行时将带有文本的按钮动态添加到 UI

Adding button with Text dynamically to UI at runtime in Unity

我正在使用 C#Unity 5.6.1f1,我做了一个机制,在 UI 按钮上单击应该向已经在 [=35] 上的面板添加一个带有文本的附加按钮=],它确实有效...因为我能够看到 Object 出现在那里

public void MakeButton(string direction)
{

    GameObject button = new GameObject();
    button.AddComponent<RectTransform>();
    button.AddComponent<Button>();

    var panel = GameObject.Find("CommandPanel");
    button.transform.position = panel.transform.position;
    button.GetComponent<RectTransform>().SetParent(panel.transform);
    button.GetComponent<RectTransform>().SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left,0,10);
    button.SetActive(true);
    button.layer = 5;

    //button.GetComponentsInChildren<Text>()[0].text = "New Super Cool Button Text";

}

问题是它不可见,原因是我认为那里没有文字...现在我怎么知道...

我尝试了 运行 以下 button.GetComponentsInChildren<Text>()[0].text = "New Super Cool Button Text";

它给了我

IndexOutOfRangeException: Array index is out of range. Hero.MakeButton (System.String direction) (at Assets/Scripts/Hero.cs:37) Hero.Down () (at Assets/Scripts/Hero.cs:14) UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:154) UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:637) UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:773) UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:52) UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35) UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44) UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50) UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261) UnityEngine.EventSystems.EventSystem:Update()

另外,正如您在屏幕截图中看到的那样,在 New Game Object 下方没有三角形,就像在 Run 下方一样,这也是一个按钮...

如何从带有文本的脚本按钮添加到 Unity 中的面板?

我强烈建议您 instantiate a prefab 而不是从头开始创建按钮。

只有 RectTransform 和按钮的游戏对象将不可见,并且不会对点击做出反应,因为您需要其他组件(例如 Canvas 渲染器和图像/原始图像)。

// Drag & Drop the prefab of the button you will instantiate
public GameObject buttonPrefab ;

public void MakeButton(string direction)
{
    // Instantiate (clone) the prefab    
    GameObject button = (GameObject) Instantiate( buttonPrefab ) ;

    var panel = GameObject.Find("CommandPanel");
    button.transform.position = panel.transform.position;
    button.GetComponent<RectTransform>().SetParent(panel.transform);
    button.GetComponent<RectTransform>().SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left,0,10);
    button.layer = 5;

}

此外,您还必须注意一件事:当您要求 Unity 创建一个按钮(使用 GameObject > UI > Button)时,Unity 会创建多个游戏对象(按钮 + 子对象)适当的组件(按钮,canvas 渲染器,文本,...)。

添加 Button 组件不会添加 Unity 会添加的所有其他组件和子组件。因此 button.GetComponentsInChildren<Text>()[0].text = "New Super Cool Button Text"; 将不起作用(因为您尚未在脚本中添加子项及其文本组件)

附加说明:如果您在放置对象时遇到问题,请检查此 link:https://docs.unity3d.com/Manual/HOWTO-UICreateFromScripting.html