在Unity

时间:2017-06-06 09:30:37

标签: c# unity3d unity3d-gui

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

enter image description here

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:数组索引超出范围。   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 handler,UnityEngine.EventSystems.BaseEventData eventData)(在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)(在C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI /EventSystem/ExecuteEvents.cs:261)   UnityEngine.EventSystems.EventSystem:更新()

另外正如您在屏幕截图中看到的那样,New Game Object下面没有三角形,就像它位于Run下面的那个按钮......

如何从包含Text的脚本按钮添加到Unity的面板?

1 个答案:

答案 0 :(得分:2)

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

仅具有RectTransform和按钮的游戏对象将不可见,并且不会对点击做出反应,因为您需要额外的组件(例如Canvas Renderer和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&gt; UI&gt;按钮)时,Unity会创建几个带有所有适当组件的游戏对象(按钮+子)(按钮,画布渲染器,文本,...)。

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

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