简体   繁体   English

Unity3D-创建一个NGUI按钮

[英]Unity3D - Create a NGUI Button

I'm working on some project where I need to create on the fly, buttons with NGUI. 我正在开发一个需要使用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: 我知道这很简单,但是根据我在http://www.tasharen.com/forum/index.php?topic=81.0和NGUI脚本(UICreateWidgetWizard.cs)中找到的内容,例如:

            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 就像@pfranza建议的那样,为您的UIButton创建一个预制体,您可以在脚本中将其作为公共对象进行引用,然后使用

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. 将该预制件作为GameObject附加到脚本中。 Then in your script you can clone that object and activate it. 然后,您可以在脚本中克隆该对象并激活它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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