Author Topic: position issue with dynamically created buttons  (Read 4172 times)

eedok

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
position issue with dynamically created buttons
« 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

ENAY

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 248
    • View Profile
Re: position issue with dynamically created buttons
« Reply #1 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);

eedok

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Re: position issue with dynamically created buttons
« Reply #2 on: May 28, 2012, 08:47:26 PM »
That's exactly what I was looking for, thank you

ENAY

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 248
    • View Profile
Re: position issue with dynamically created buttons
« Reply #3 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.

dlewis

  • Guest
Re: position issue with dynamically created buttons
« Reply #4 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;