简体   繁体   中英

Unity3D - Create a NGUI Button

I'm working on some project where I need to create on the fly, buttons with NGUI. I found some answers but none of them could help me. Iknow it's simple but according to what I found on http://www.tasharen.com/forum/index.php?topic=81.0 and in NGUI script (UICreateWidgetWizard.cs), like:

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

it's still not working..

An my code is the following:

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

Thanks for yout help guys !

Either as @pfranza suggests, create a prefab of your UIButtons that you can reference as a public object in your script, then to create it use the

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

Alternatively, you can fully create it at runtime if you wish:

    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.

You have to create a prefab out of a button that you want to use as a template; Attach that prefab to your script as a GameObject. Then in your script you can clone that object and activate it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM