2017-06-06 137 views
0

我正在使用C#Unity 5.6.1f1,我做了一個機制,點擊UI按鈕應該添加一個額外的按鈕與文本到Panel已經在用戶界面上,它確實工作......因爲我能看到對象出現在那裏添加按鈕與文本動態UI在運行時在Unity

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 (在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 )(在C:/ buildslave/unity/build/run//build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35) Unity Engine.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 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:Update()

而且你可以在截圖中看到,沒有三角在New Game Object喜歡它Run下,這也是一個按鈕...

如何從具有文本上它面板在Unity腳本按鈕添加?

+0

@Hellium答案是正確的,但我只是補充說,你可以通過代替'GameObject按鈕=新的GameObject();'''與GameObject按鈕=新遊戲對象(「Button_Name」,新類型[] {typeof(圖片) ),typeof(Button)});'。當然,你需要用'Text'組件創建另一個對象,然後將它設置爲child。 – Kardux

回答

2

我強烈建議你instantiate a prefab而不是從頭開始創建按鈕。

僅具有RectTransform和按鈕的gameobject將不可見,並且不會對點擊作出反應,因爲您需要額外的組件(例如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; 

} 

而且,你必須要小心一個額外的東西:當你問團結創建一個按鈕,所有的佔有(用遊戲對象> UI>按鈕),統一創建幾個gameobjects(按鈕+孩子)組件(按鈕,畫布渲染器,文本...)。

添加按鈕組件不會添加Unity所有的其他組件。因此button.GetComponentsInChildren<Text>()[0].text = "New Super Cool Button Text";將無法​​正常工作(因爲你還沒有加入的孩子,在你的腳本的文本組件)

附加說明:檢查此鏈接,如果你有一些問題,把你的對象:https://docs.unity3d.com/Manual/HOWTO-UICreateFromScripting.html

+0

嗨Hellium,謝謝你的回答...我需要添加多個不同的按鈕,這將形成一個列表,基於用戶點擊UI ....我無法知道他將點擊多少次按鈕...... –

+0

這是什麼問題?您仍然可以通過更改組件的值來自定義實例化的按鈕。如果您需要放置按鈕以創建列表,請查看可添加到面板的[AutoLayout](https://docs.unity3d.com/Manual/comp-UIAutoLayout.html)組件。 – Hellium

+0

只是檢查,大聲笑...我是Unity的完整noob,閱讀prefabs,感謝隊友.... –