Author Topic: Why positions seems so funny when used UIAnchor  (Read 3260 times)

galuodo

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 65
    • View Profile
Why positions seems so funny when used UIAnchor
« on: September 06, 2013, 05:16:36 AM »
When I set the position of a game object, it works well at first. However after I used UIAnchor so that the game can be fit other resolution, the positions have a large deviation after the resolution changed.
  1.         void Update () {
  2.                 ul.transform.position = sp.transform.position+ new Vector3(5,5,0);
  3.         }
  4.  

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Why positions seems so funny when used UIAnchor
« Reply #1 on: September 06, 2013, 07:41:54 AM »
Remember to set localPosition for UI stuff. Position is the global position which may vary and make no sense.

galuodo

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 65
    • View Profile
Re: Why positions seems so funny when used UIAnchor
« Reply #2 on: September 08, 2013, 08:57:01 PM »
Thanks for your reply.
but i need world position to set underline or detect the location of mouse cursor and something else with out colliders, what should I do next?

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Why positions seems so funny when used UIAnchor
« Reply #3 on: September 11, 2013, 05:56:13 PM »
Well then you have to convert between local space and global space and maybe even screen space to be able to match everything. Setting some position + Vector3(5,0,0) can mean quite a difference when set in global space. Remember that NGUI operates in a very small global area (2 / UIRoot.activeHeight) so you should calculate how much you want to move in localSpace and convert to global.

I think there's a bunch of helper methods in NGUITools and NGUIMath that can help you as well.