Author Topic: Moving objects outside UIRoot breaks anchoring  (Read 12099 times)

Ana

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
Moving objects outside UIRoot breaks anchoring
« on: April 01, 2014, 03:40:28 AM »
I would like to have a widget anchored to a game object as a child or a sibling to that game object.
I've set up the widget exactly as I want it to appear on the screen. Currently, it is a child of UIRoot but has its own panel. I anchored it to target game object, so far so good.

However, the problem arises when I try to change its parent. I tried moving it so it has no parent and I tried reparenting it to another game object. Both fail spectacularly.
If I turn off the anchoring and move it so it has no parent, widget stays where I want it, but as soon as I turn the anchoring back on it goes far offscreen.
Of course, not turning the anchoring off before moving makes it go offscreen right away.

When I try to reparent it to another game object even more bizarre things happen. Even when I turn OFF the anchoring and reparent it, it is no longer in the position it is supposed to be in. In the scene view it appears to stay in the same position but in the game view it switches the position about 500px to the center of the screen and trying to move the widget results in it snapping to discrete intervals that seem to be around 800px wide. I assume it is a problem with float precision? Turning On anchoring at this point makes the widget go offscreen and its size to change to 2x2.

What am I doing wrong?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Moving objects outside UIRoot breaks anchoring
« Reply #1 on: April 01, 2014, 08:29:36 AM »
Anchoring only works while it's under the UIRoot. It's for 2D UIs.

Once you move your widget into the game world, it's a part of the 3D world and everything changes. Why are you doing this? If you want a widget to follow a 3D object, then just keep it anchored to that object under the UIRoot or use the UIFollowTarget script if you have HUDText.

Ana

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
Re: Moving objects outside UIRoot breaks anchoring
« Reply #2 on: April 01, 2014, 08:47:53 AM »
I remember being able to have widgets outside UIRoot that are anchored to the parts of the screen. That doesn't exist anymore?

I want the widget to display above the object and it would be convenient if the widget could be a part of the same prefab as mentioned object, I'd like to avoid instantiating the widget at runtime.

Can I add an additional UIRoot script to the object the widget resides on or his parent? Would that fix it? Would there be any potential problems with multiple UIRoots? Would performance suffer? Anything I need to be careful about?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Moving objects outside UIRoot breaks anchoring
« Reply #3 on: April 01, 2014, 10:42:11 AM »
The anchoring system was not designed to be outside of the UI hierarchy.

You can likely use the UIFollowTarget script from the HUDText package, but even that's a "maybe" as it's not something I tested.

You don't need more than one UIRoot. What are you trying to do? To display something above the character's head, make it a regular widget, a part of the UI hierarchy, and make it anchored to a 3D object. Why are you trying to make it a part of the game world?

Even if you did make it a part of the game world, you don't need a UIRoot, and you don't need to make it "follow" anything. Simply making it a child of the 3D object will make it "follow" it.

Ana

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
Re: Moving objects outside UIRoot breaks anchoring
« Reply #4 on: April 01, 2014, 11:50:13 AM »
Quote
You don't need more than one UIRoot. What are you trying to do? To display something above the character's head, make it a regular widget, a part of the UI hierarchy, and make it anchored to a 3D object. Why are you trying to make it a part of the game world?

I need to display a 2d widget above an Unity sprite, something similar to a health bar. I need it to be "bundled" together with the game object because not making it that way would cause a lot of in game systems to feel "dirty" and require a lot of additional work to ensure everything from dynamic loading and unloading, instantiating, object painting etc, works.

Quote
"Even if you did make it a part of the game world, you don't need a UIRoot, and you don't need to make it "follow" anything. Simply making it a child of the 3D object will make it "follow" it."
Yes. Transform does "follow" the object but everything else, including the widget appearance, does not. Moving the the widget doesn't seem to move the appearance of the widget until a certain threshold is passed then it snaps to a position 800 px away. All in all a very strange behaviour.

See a screenshot in the attachment to see what I mean.
The widget appears in the scene view in one position but appears in game view in another (or it isn't in the game view at all). Note, the problems occur only if I move the widget outside of UIRoot as a parent of my game object. The widget does not have anchoring enabled.
If I just move it out of UIRoot so it doesn't have a parent it looks and behaves as it should. However, I can't use it like that because of previously mentioned problems.


To sum up what the widget needs to do:
  • be a child/sibling of a game object that exists outside the UIRoot
  • "follow" its target in x and y axis like a hud health bar

I don't need a bunch of advanced stuff and polish. I need just those two things.
Is it doable? How?

Ana

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
Re: Moving objects outside UIRoot breaks anchoring
« Reply #5 on: April 02, 2014, 12:41:22 PM »
Bump?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Moving objects outside UIRoot breaks anchoring
« Reply #6 on: April 02, 2014, 05:17:28 PM »
There is no reason for the gizmos to show up in one place and the geometry be draw in another unless your UIPanel has the static flag checked.

Ana

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
Re: Moving objects outside UIRoot breaks anchoring
« Reply #7 on: April 02, 2014, 05:26:15 PM »
It doesn't.
And like I mentioned, it only happens if I move the widget out of UIRoot and parent it to a game object.
Trying to move the widget in the sceneview results in gizmos snapping back to the place where they were supposed to be. However, I can't move the widget at all.
When I try moving it by changing the transform directly, it again becomes "out of sync" and the image of the widget moves in large discrete intervals while gizmos move normally.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Moving objects outside UIRoot breaks anchoring
« Reply #8 on: April 02, 2014, 06:05:23 PM »
Oh, doing it yourself, I take it? You need to notify the widget that their parent changed -- NGUITools.MarkParentAsChanged.

Otherwise if you just reparent stuff, how will the widget know that you did that?

Ana

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
Re: Moving objects outside UIRoot breaks anchoring
« Reply #9 on: April 02, 2014, 06:07:19 PM »
I am "drag and dropping" the widget in the editor not via code.
How would I even call that function in the editor? :s Write a custom menu item that would refresh currently selected widget? Or do you already have something similar?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Moving objects outside UIRoot breaks anchoring
« Reply #10 on: April 02, 2014, 06:09:50 PM »
In the editor it should work fine. But when moving a widget, you must ensure that there is a panel underneath it to receive it. When moving something outside of the UIRoot you need to first add a UIPanel at the target location where you're moving something. All widgets must go under a UIPanel. UIRoot has one such panel.

Ana

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
Re: Moving objects outside UIRoot breaks anchoring
« Reply #11 on: April 02, 2014, 06:26:39 PM »
Ah, so putting a UIPanel on the same object the widget is on doesn't work anymore (it used to work in previous versions).
Alright, when I put it as a child of UIPanel it works. But anchoring to a game object still doesn't. It goes off screen and its dimensions change to 2 x 2.
What else am I missing?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Moving objects outside UIRoot breaks anchoring
« Reply #12 on: April 02, 2014, 06:28:39 PM »
Panels should never be on the same object as widgets. It never worked. Your widgets were actually using UIRoot's panel, I'm guessing.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Moving objects outside UIRoot breaks anchoring
« Reply #13 on: April 02, 2014, 06:31:49 PM »
"Anchoring a game object" -- I already mentioned that you should not anchor anything when you're simply parenting something underneath another object. Anchoring will never work outside of UIRoot anyway.

Ana

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
Re: Moving objects outside UIRoot breaks anchoring
« Reply #14 on: April 02, 2014, 06:37:32 PM »
Thank you for your support.

Quote
Panels should never be on the same object as widgets. It never worked. Your widgets were actually using UIRoot's panel, I'm guessing.
Could be, or the only widgets that were on the same objects were layout related ones such as grids.

Quote
"Anchoring a game object" -- I already mentioned that you should not anchor anything when you're simply parenting something underneath another object. Anchoring will never work outside of UIRoot anyway.
I see. So the only solutions I have is either to instantiate the widget under the UIRoot at runtime or write a custom script that would move it into the ui camera's view when needed?