Author Topic: instantiate ngui sprite  (Read 2911 times)

Saxi

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 24
    • View Profile
instantiate ngui sprite
« on: August 09, 2014, 05:38:32 PM »
I know it is recommended to use NGUITOOLS.AddChild rather than Instantiate but I'm kind of in a conundrum.

I am trying to make my sprites nGUI Sprites so I can keep my game sprites in one atlas.   I was using Unity Sprite and was instantiating it at top of the screen randomly between left/right of the screen.  When I tried converting to nGUI sprite so I can share the atlas I can't get the behavior to match unity sprites as NGUITOOLS.AddChild requires a gameobject to represent the start position, and I don't want to have tons of game objects at the top of the screen representing many different starting points. 

If I use instantiate it always start at 0,0.
Is there is no way to do this using a nGUI sprite, or do I need to stick with Unity sprites?

Since the game is 2D, I was trying to keep everything under UIRoot and keep them all using nGUI widgets rather than mixing nGUI sprites and unity sprites.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: instantiate ngui sprite
« Reply #1 on: August 10, 2014, 04:40:47 PM »
All NGUI sprites must be a child of a panel, so there has to be a parent anyway. If there isn't one, NGUI will attempt to find it and create one. You should never just instantiate UI elements in the middle of nowhere. If nothing else, then UIRoot.list[0].gameObject can be your parent.

Saxi

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 24
    • View Profile
Re: instantiate ngui sprite
« Reply #2 on: August 10, 2014, 04:48:40 PM »
All NGUI sprites must be a child of a panel, so there has to be a parent anyway. If there isn't one, NGUI will attempt to find it and create one. You should never just instantiate UI elements in the middle of nowhere. If nothing else, then UIRoot.list[0].gameObject can be your parent.

I created float leftBoundsX and rightBoundX that represents the furthest left/right via the camera.  I was then trying to instantiate using random.Range(leftBoundsX, rightBoundsX).  It works fine as Unity Sprite, and these are not UI elements, but I was using UISprite so I can keep it all in one sprite sheet (it's a small game for my 7yo son but good practice).  I was trying to keep everything under UIRoot and using the camera that is built by default w/ nGUI when adding your first nGUI object.

When I use AddChild, I can't seem to create a random start location based on left/right max X at the top of the screen like I want (they drop from the top of the screen downwards).

Right now I changed them back to Unity sprites and it all works perfectly, but I was trying to keep everything under nGUI since it is a 2D game with all sprites, it seems easier if I just keep everything under UIRoot as some things have ui elements even though they are game pieces and not purely gui.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: instantiate ngui sprite
« Reply #3 on: August 10, 2014, 06:20:12 PM »
You're likely using wrong coordinates for movement. All NGUITools.AddChild does is adds a child to a parent, and resets its local position to (0, 0, 0). Everything under UIRoot is in virtual pixels, so you need to be aware that widget.transform.localPosition is going to be vastly different from widget.transform.position.