簡體   English   中英

Unity3D-創建一個NGUI按鈕

[英]Unity3D - Create a NGUI Button

我正在開發一個需要使用NGUI快速創建按鈕的項目。 我找到了一些答案,但是沒有一個可以幫助我。 我知道這很簡單,但是根據我在http://www.tasharen.com/forum/index.php?topic=81.0和NGUI腳本(UICreateWidgetWizard.cs)中找到的內容,例如:

            UILabel lbl = NGUITools.AddWidget<UILabel>(go);

它仍然無法正常工作..

我的代碼如下:

            UIButton button =  NGUITools.AddWidget<UIButton>(parent);

謝謝你們的幫助!

就像@pfranza建議的那樣,為您的UIButton創建一個預制體,您可以在腳本中將其作為公共對象進行引用,然后使用

GameObject newButton = NGUITools.AddChild(mParentGameObject, mButtonPrefab);

另外,您可以根據需要在運行時完全創建它:

    UISprite mButtonSprite = NGUITools.AddSprite(mParentGameObject, mAtlas, "Button");
    //Button is the name of the sprite in the atlas

    mButtonSprite.MakePixelPerfect();
    NGUITools.AddWidgetCollider(mButtonSprite.gameObject);
    mButtonSprite.gameObject.AddComponent<UIButton>();
    //add any further components/set up you like.

您必須使用要用作模板的按鈕創建一個預制件。 將該預制件作為GameObject附加到腳本中。 然后,您可以在腳本中克隆該對象並激活它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM