Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: eedok on May 28, 2012, 07:54:15 PM

Title: position issue with dynamically created buttons
Post by: eedok on May 28, 2012, 07:54:15 PM
Trying to dynamically build an interface, but when I add buttons through code their positions end up all messed up, I'm trying to add a button at position 0,52,0 on the current panel that the script is attached to like so:
  1. GameObject newButton = NGUITools.AddChild(gameObject,buttonPrefab);
  2. newButton.transform.position = new Vector3(0,52,0);
  3.  

but for whatever reason the position ends up at a weird position like 11621, or 19967.5 depending on the resolution, but when I change it in the editor while it's running, the button goes to the position I want it to, is there a step I'm missing to not have the new button set to a weird position
Title: Re: position issue with dynamically created buttons
Post by: ENAY on May 28, 2012, 08:32:49 PM
This happened to me too, you might want to look at using:-

newButton.transform.localPosition = new Vector3(0,52,0);
Title: Re: position issue with dynamically created buttons
Post by: eedok on May 28, 2012, 08:47:26 PM
That's exactly what I was looking for, thank you
Title: Re: position issue with dynamically created buttons
Post by: ENAY on May 28, 2012, 09:01:04 PM
As far as I know, localPosition is literally, the position locally into your menu system, whereas position is going to affect everything inside your menu as well.
Title: Re: position issue with dynamically created buttons
Post by: dlewis on May 28, 2012, 09:04:31 PM
Setting the local position is best (since it is actually affected by the UIRoot scale) but if you must set the global position then you must take the UIRoot scale into account.

ie. globalPos.x *= uiRootScale.x;
globalPos.y *= uiRootScale.y;