Author Topic: Get NGUI World Space Position  (Read 4921 times)

Ace

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 24
    • View Profile
Get NGUI World Space Position
« on: September 01, 2014, 09:45:19 PM »
Hello,

I am trying to add a custom tooltip. I wish to have a drop down box go over the GUI sprite on the screen. However my position fetch is not correct.

My code is:

//card is a GUI sprite on the UI Root panel inside of a grid
  1. Debug.Log("x is " + card.transform.position.x + " vs local pos is " + card.transform.localPosition.x);
  2. _cardToolTip.transform.position = new Vector3(card.transform.position.x,
  3.                                                                 card.transform.position.y + card.GetComponent<UISprite>().height,
  4.                                                                 card.transform.position.z);
  5.  

The local statment prints "x is -1.16977 vs local pos is 9"

The local position of 9 makes perfect sense. The child object (the card itself which sits inside a border) is inset by 9 pixels. The x = -1.167 does not make sense to me, as clearly the object is at least over by 100 pixels and I suspect it has to do with the fact that is in a grid (but I don't see -1.167 in any value in either grid or the nest objects within the grid)?

Regardless of how nested my child is, how can I properly get the NGUI space position?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Get NGUI World Space Position
« Reply #1 on: September 02, 2014, 11:55:11 AM »
Never use world position with GUI. Have a look at transform.OverlayPosition (found in NGUIMath.cs)

Ace

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 24
    • View Profile
Re: Get NGUI World Space Position
« Reply #2 on: September 02, 2014, 06:20:37 PM »
My object always seems to be stuck at 0,0,0 after I instantiate it.

I made sure the static flag was not set on my UI Root.

I made it a child of the UI Root by using NGUITools.AddChild(uiRoot,prefab)

And I set the prefab.transform.localPosition

What else could cause this to be stuck at 0,0,0?

Using the panel tool I can confirm the prefab is a child of the UI Root


  1. _uiRoot = GameObject.Find("UI Root");
  2. _cardToolTip = MonoBehaviour.Instantiate(Resources.Load("Prefabs/UI/cardToolTip")) as GameObject;
  3. NGUITools.AddChild(_uiRoot,_cardToolTip);
  4. _cardToolTip.transform.localPosition = new Vector3(500,500,0);

the _cardToolTip is always at 0,0,0. If I move the prefab around it will update on the screen it just seems setting in my code has no effect.
« Last Edit: September 02, 2014, 06:31:50 PM by Ace »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Get NGUI World Space Position
« Reply #3 on: September 03, 2014, 11:24:37 AM »
1. Never use Instantiate. use NGUITools.AddChild.

2. You first instantiate the object, then you instantiate it again via AddChild. This is wrong. Get rid of the first Instantiate and keep only Resources.Load.

3. AddChild returns an instantiated object. Set its transform.localPosition, not the prefab's.

Ace

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 24
    • View Profile
Re: Get NGUI World Space Position
« Reply #4 on: September 04, 2014, 11:09:46 PM »
Gotcha thank you!

Previous frameworks Ive used AddChild was strictly a parenting thing. I see here now that in NGIUI instantiate in part of AddChild as well