- Home /
NGUI Sprite not visible in 3d when added by code
When i add them by hand its nice two-sided GO with sprites, but when i do this by code
GameObject squadUnit = new GameObject("squadUnit"+i);
UISprite uiSpr = squadUnit.AddComponent<UISprite>();
uiSpr.atlas = atlas;
uiSpr.spriteName = "frame2";
uiSpr.panel = uiPanel;
squadUnit.transform.parent = panel.transform;
UISpriteAnimation uiSprAnim = squadUnit.AddComponent<UISpriteAnimation>();
uiSprAnim.framesPerSecond = 8;
uiSprAnim.namePrefix = "frame";
I see a rectangles but animation frames not visible How to add them properly?
Answer by vexe · Oct 02, 2013 at 11:53 PM
You're probably having parenting issues with your widgets and panel.
Instead of having to worry about parenting, let NGUI do it for you, use NGUITools.AddChild
:
GameObject squadUnit = NGUITools.AddChild(uiPanel.gameObject, new GameObject("squadUnit"+i)); // this will create the object and parent it properly
UISprite uiSpr = squadUnit.AddComponent<UISprite>();
uiSpr.atlas = atlas;
uiSpr.spriteName = "frame2";
// uiSpr.panel = uiPanel; don't need that no more
// squadUnit.transform.parent = panel.transform; nor that
And btw, sometimes widgets don't appear even if you add them manually in-scene, if that happens just pull your panel a little bit forward. See this for more info as to why this happens (it's a Unity thing)
Not sure if i will have the chance to check this solution, as i have moved to 2dtoolkit for sprites. Still have the feeling this might be just the thing to get them going on NGUI. Thanks.
I $$anonymous$$gest you move back to NGUI 3.0 - As the new UnityGUI will be based on NGUI, that way your migration will be easy. See this.
Answer by whydoidoit · Oct 05, 2013 at 11:39 AM
It's probably not on the right layer to be rendered by the camera that is doing the NGUI stuff. That's normally what it is when it happens to me :)