Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: edaw22 on December 06, 2013, 06:35:02 AM

Title: NGUI parenting a UI element to a gameobject in the world. HELP
Post by: edaw22 on December 06, 2013, 06:35:02 AM
Hey everyone.

So I have a third person platform game where I want there to be NPCs with speech bubbles over their heads that the player can click on to start a conversation with the NPC.
I did some research on how to have NGUI elements in the 3d world and found this video.

http://www.youtube.com/watch?v=YCyNHRv432o
 
at 10:30 he has just created a button in the 3D UI structure and simply drags that button onto a gameobject in the scene to parent it to the object. He then proceeds to add the Camer UI component to his main camera to allow the main cam to allow interaction and presto, its done.

However when I tried to do this, as soon as I drag the button onto my NPC it parents the button to it, but it also grabs the NPC and relocates it to be a part of the 3d UI structure just under the panel that the button was originally a part of.

Anyone have any idea why this isnt working for me?

I realise that im probably using a newer version of NGUI than the guy in the video, but any way around this?
Title: Re: NGUI parenting a UI element to a gameobject in the world. HELP
Post by: edaw22 on December 06, 2013, 07:34:41 AM
I realised that I am able to parent the entire Panel that hold the button to the NPC object. However not just the button itself.

Odd. This is half way to a solution. I mean it works, but id still like to know why I cant do it for just the button as shown in the example video.
Title: Re: NGUI parenting a UI element to a gameobject in the world. HELP
Post by: ArenMook on December 06, 2013, 01:55:15 PM
I'd suggest keeping UI in the UI hierarchy where it belongs, and simply making it follow the 3D object. With NGUI 3.0.7 it's a simple matter of setting the widget's anchor to be your 3D game object. In earlier versions you need to do some math. Transform the object's world position to screen coordinates using the main camera, then use the UI camera to transform from screen space back to world. Then inverse transform the value into local space in order to position the object.

HUDText extension, if you have it, does this in the UIFollowTarget script that does just that -- makes any UI component follow a 3D object.
Title: Re: NGUI parenting a UI element to a gameobject in the world. HELP
Post by: mikewoz on June 23, 2014, 03:26:28 PM
Just a quick follow-up to this question:

It seems that when you anchor to a 3D transform, the anchor is always relative to the center. I want a sprite to always appear above the object, but I can't use a hardcoded offset from the center because the object can change shape. What I really want to do is to set the anchor to the top-center of the 3D bounds of the object, so that it stays above whenever the object's shape changed.  Do I need to set the anchor rects every frame? ie, call sprite.leftAnchor.Set() every frame?

Anyone have ideas? (Thanks!)
Title: Re: NGUI parenting a UI element to a gameobject in the world. HELP
Post by: ArenMook on June 24, 2014, 04:13:56 AM
If the object can change shape, then you shouldn't be anchoring to the object. Instead, anchor to a child of this object and adjust that child's position so that it's where you want it to be.

For example in a game like World of Warcraft you wouldn't anchor the name tag to the NPC. You would anchor the name tag to an object that's above the NPC's head.