Author Topic: widget positions get/set via script  (Read 7321 times)

didier

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 21
    • View Profile
widget positions get/set via script
« on: January 04, 2014, 09:27:57 AM »
Hi

I would like to :

- Get the position and dimension of a widget (in screen coordinate)
- instanciate another widget (from a prefab) and set its position manually (via script),

i.e like widget1.GetPosition(), widget1.GetDimension() and widget2.SetPosition(x,y)

Using GUITexture its pretty simple, however with NGUI UISprite i am not sure what is the best members/method to use,

Is it possible to do this ?

Thanks


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,151
  • Toronto, Canada
    • View Profile
Re: widget positions get/set via script
« Reply #1 on: January 04, 2014, 09:54:42 AM »
UIWidget.worldCorners tells you world space position of the 4 corners of the widget. Use Camera.WorldToScreenSpace to convert them to screen coordinates.

However... screen coordinates are meaningless in your case since what you're trying to do is position another widget there, and NGUI works in virtual coordinates, not screen coordinates.

What you need to do instead is to convert world coordinates to local coordinates. For example if you have an object called "myGameObject", you can get its transform and inverse transform to local space:
  1. Vector3 localPos = myGameObject.transform.InverseTransformPoint(worldPos);
You can then position your widget:
  1. myWidget.transform.localPosition = localPos;

didier

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 21
    • View Profile
Re: widget positions get/set via script
« Reply #2 on: January 04, 2014, 11:47:30 AM »
So i can work with transform that 's really nice, thanks

However in this case is it not simpler to do this:

myWidget.transform.position = myGameObject.transform.position ?

it works for me this way.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,151
  • Toronto, Canada
    • View Profile
Re: widget positions get/set via script
« Reply #3 on: January 04, 2014, 12:40:45 PM »
If the widget is centered, sure.

didier

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 21
    • View Profile
Re: widget positions get/set via script
« Reply #4 on: January 04, 2014, 04:26:52 PM »
Ok,

It seems to work fine but now i am facing a weird issue that happens sometimes:

I set the transform.position of my widget, most of the time it is fine, but sometimes the widget transform is not updated (or the object not refreshed on screen).
i.e i instanciate my object on 0.0.0, then i set uiwidget.transform.position to x,y,z, but widget doesnt move.
Once it failed once i may have to stop and resize the editor, then eventually it got moved correctly.

I tried to invalidate , steDirty UpdateGeometry etc but its still failing sometimes.

I have another related question is: when shall I be sure to be able to get the final position of a widget? (in the Start() of my script or shall i receive an event from UIWidget telling me geometry is now up to date ?)

thanks

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,151
  • Toronto, Canada
    • View Profile
Re: widget positions get/set via script
« Reply #5 on: January 04, 2014, 06:31:48 PM »
Turn off the static flag on your UIPanel. There is a warning about it.

didier

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 21
    • View Profile
Re: widget positions get/set via script
« Reply #6 on: January 05, 2014, 04:52:55 AM »
This is the first thing I checked, but i dont have any panel on top of my object. I also deactivated all unity static flag, but problem is still there.

Shall i create a panel and instanciate my prefab inside this panel to make it work? i can try.

didier

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 21
    • View Profile
Re: widget positions get/set via script
« Reply #7 on: January 05, 2014, 05:44:50 AM »
I created a new panel and i now instanciate the prefabs in the panel. It works.
Not sure if it is a bug or not (nothing mentions that you must use panel), but thats fine for me thanks.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,151
  • Toronto, Canada
    • View Profile
Re: widget positions get/set via script
« Reply #8 on: January 05, 2014, 08:56:31 AM »
Widgets can't be drawn unless there is a panel present. To see which panel draws your selected widget, open the Panel Tool. It will be highlighted in blue. The static flag I was talking about is under the Advanced Options, not the Static game object flag next to the object's name.

konsnos

  • Newbie
  • *
  • Thank You
  • -Given: 8
  • -Receive: 0
  • Posts: 26
    • View Profile
Re: widget positions get/set via script
« Reply #9 on: May 07, 2014, 11:09:26 AM »
My apologies for reviving this old topic but it seems that I have the same problem. Was this solved?

i.e i instanciate my object on 0.0.0, then i set uiwidget.transform.position to x,y,z, but widget doesnt move.

It happens to me after the instantiation of objects. Static is not selected in UIRoot. My initial thought was that there were scripts running from NGUI when I instantiate an object, so I set an invoke but still nothing happens. I tried to set the transform of both the GameObject and the UISprite but no result. GameObjects remain at 0,0,0.

Help would be welcome :)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,151
  • Toronto, Canada
    • View Profile
Re: widget positions get/set via script
« Reply #10 on: May 08, 2014, 01:44:51 AM »
Quote
Static is not selected in UIRoot.
UIRoot contains a panel by default, but it's likely not the only panel you have. Open the Panel Tool to see the full list.

Not sure what you mean by setting an invoke.

When you instantiate something using NGUITools.AddChild(parent, prefab) its position gets reset to (0, 0, 0) and it's up to you to reposition it -- but do it by setting transform.localPosition, not transform.position.

konsnos

  • Newbie
  • *
  • Thank You
  • -Given: 8
  • -Receive: 0
  • Posts: 26
    • View Profile
Re: widget positions get/set via script
« Reply #11 on: May 08, 2014, 02:38:41 AM »
Thank you Aren!

I didn't know that I had to instantiate gui objects with the NGUITools.AddChild. I was going the traditional way of Unity with Instantiate. That solved it. I didn't knew about the localPosition too! My apologies if this was written in the documentation. I didn't saw it :(

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,151
  • Toronto, Canada
    • View Profile
Re: widget positions get/set via script
« Reply #12 on: May 08, 2014, 02:55:25 AM »
Yup, Unity documentation. :P

The difference between local and world coordinates is an obvious one. Think of yourself sitting inside a car. You may be driving around on the wrong side of the road in London, but according to the car itself you are always in the same exact position. Relative to the car, you don't move (read: local coordinate). Relative to the world you do (read: world coordinate).