在 Unity 中在运行时将带有文本的按钮动态添加到 UI [英] Adding button with Text dynamically to UI at runtime in Unity

查看:22
本文介绍了在 Unity 中在运行时将带有文本的按钮动态添加到 UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 C#Unity 5.6.1f1,我完成了一种机制,即在 UI 按钮单击时应向面板添加一个带有文本的附加按钮,该按钮已打开UI,它确实有效......因为我能够看到对象出现在那里

I am working with C# an Unity 5.6.1f1, I done a mechanism that on UI button click should add an additional button with text to Panel that is already on UI, and it does work... since I am able to see Object appear there

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";

}

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

problem is it isn't visible and reason for that I believe that there is no text there... now how do I know that...

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

它给了我

IndexOutOfRangeException:数组索引超出范围.Hero.MakeButton(System.String 方向)(在 Assets/Scripts/Hero.cs:37)Hero.Down ()(在 Assets/Scripts/Hero.cs:14)UnityEngine.Events.InvokableCall.Invoke (System.Object[] args)(在 C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:154)UnityEngine.Events.InvokableCallList.Invoke(System.Object[] 参数)(在 C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:637)UnityEngine.Events.UnityEventBase.Invoke(System.Object[] 参数)(在 C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:773)UnityEngine.Events.UnityEvent.Invoke()(在 C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:52)UnityEngine.UI.Button.Press () (在 C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35)UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData)(在 C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44)UnityEngine.EventSystems.ExecuteEvents.Execute(IPointerClickHandler 处理程序,UnityEngine.EventSystems.BaseEventData eventData)(在 C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50)UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler](UnityEngine.GameObject 目标,UnityEngine.EventSystems.BaseEventData eventData,UnityEngine.EventSystems.EventFunction`1 函子)(在 C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261)UnityEngine.EventSystems.EventSystem:Update()

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 下的一样,它也是一个按钮......

Also as you can see in the screenshot there is no triangle Under New Game Object like it is under Run which is also a button...

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

推荐答案

我强烈建议您实例化一个预制件,而不是从头开始创建按钮.

I highly advise you to instantiate a prefab instead of creating the button from scratch.

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

A gameobject with a RectTransform and button only won't be visible, and won't react to clicks, because you need additionnal components (such as Canvas Renderer and Image / Raw Image).

// 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 会创建多个游戏对象(按钮 + 子项)以及所有适当的组件(按钮、画布渲染器,文本,...).

Moreover, you have to be careful to one additional thing : when you ask Unity to create a button (Using GameObject > UI > Button), Unity creates several gameobjects (the button + child) with all the appropriates components (button, canvas renderer, text, ...).

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

Adding the Button component won't add all the other components and children Unity does. Thus button.GetComponentsInChildren<Text>()[0].text = "New Super Cool Button Text"; won't work (since you haven't added the child and its Text component in your script)

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

Additional note : Check this link if you have some problems with placing your object : https://docs.unity3d.com/Manual/HOWTO-UICreateFromScripting.html

这篇关于在 Unity 中在运行时将带有文本的按钮动态添加到 UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆