playMaker

Author Topic: Attach a GUItext to a game object  (Read 11854 times)

mweyna

  • Full Member
  • ***
  • Posts: 242
Attach a GUItext to a game object
« on: November 19, 2013, 11:51:11 AM »
So trying to figure this out, but I'm a bit stumped. If I wanted to attach an GUI text box to a moving gameobject, what is the best way to approach that? Using the Unity default GUIText Object there is no way to set the anchor to be a specific game object. Looking at the playmaker options I don't see this either. If I wanted to have a text box like this -



How would I accomplish that?

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Attach a GUItext to a game object
« Reply #1 on: November 19, 2013, 11:57:21 AM »
Hi,

It's very easy actually:

 Use the action "World to Screen point" and set it to "normalize", this will return you the exact position to use on your guiTexture to match.

 Then set the offset either as a "screen" relative offset, adding to this normalized position, OR modify the pixel inset offset to achieve a "pixel" based offset that will not vary depending on the screen size.


 Bye,

 Jean

mweyna

  • Full Member
  • ***
  • Posts: 242
Re: Attach a GUItext to a game object
« Reply #2 on: November 19, 2013, 01:14:57 PM »
So that's using a GUITexture object, would I not want to use a GUIText object instead since I want to add in a string variable into it.
« Last Edit: November 19, 2013, 03:25:17 PM by mweyna »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Attach a GUItext to a game object
« Reply #3 on: November 20, 2013, 12:15:37 AM »
Hi,

 sorry that was a typo, it works exactly the same with a GuiText actually. It also has a "Pixel offset" property.

Bye,

 Jean

mweyna

  • Full Member
  • ***
  • Posts: 242
Re: Attach a GUItext to a game object
« Reply #4 on: November 20, 2013, 01:40:15 AM »
So heres my FSM



and heres the normalized numbers it puts on the object



However, the GUI element is dramatically offscreen (to the point where I can't see it). However, if I have the element alone in the editor (prior to pressing play) and enter those exact values, it doesn't appear to work either. What am I doing wrong?

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Attach a GUItext to a game object
« Reply #5 on: November 20, 2013, 03:09:19 AM »
Hi,

 you need to inject the normalized value in the transform position itself, the pixel inset is only for an offset in pixels on top of the transform position.

Bye,

 Jean

mweyna

  • Full Member
  • ***
  • Posts: 242
Re: Attach a GUItext to a game object
« Reply #6 on: November 20, 2013, 04:05:16 PM »
So I have the normalized Vector3 apply to both the pixel offset and the position, but it appears to be slightly off.



Something in the normalization process appears to be going wrong. Additionally, if I move the camera (zoom in/out) then that button is no longer in the right location. It strikes me as a limitation of using this sort of method, unless I have them re-find their position on any zoom in/out. But let's solve this first -

Why is my icon being offset oddly by the normalization process? Currently, I'm converting the World Position of the object via a SCREEN TO WORLD action, then plugging that X,Y into the object position of the GUI_Texture.

What is the more efficient way to approach this so that I don't need to always compute the position if I move the camera?

Thanks.
« Last Edit: November 20, 2013, 04:06:48 PM by mweyna »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Attach a GUItext to a game object
« Reply #7 on: November 20, 2013, 11:35:56 PM »
Hi,

 you need to start on a fresh project with a simple cube, and build up from there.

 the world to screen will give you the exact screen position of the pivot point of your gameObject target. Then it's up to you within your GUIText setup to define how the text is going to be positioned and aligned.

All the tests that I have done do not expose any such unwanted offset, so I guess something is fishy in the implementation itself.

-- Any live position matching between a 3d world and a 2d screen MUST be performed everyframe, there is no escape, and you should not be too worried about performances as this operation is a built in method within Unity, so it should be ok. have you run some profiling already on this?

 Bye,

 Jean

mweyna

  • Full Member
  • ***
  • Posts: 242
Re: Attach a GUItext to a game object
« Reply #8 on: November 21, 2013, 12:34:56 AM »
Other then keeping my eye on my Renderer and Main Thread times, I havent really profiled anything. To my understanding the unity profiler was a PRO feature (still using free). Is there a viable asset store profiler you'd recommend in absence of pro?

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Attach a GUItext to a game object
« Reply #9 on: November 28, 2013, 05:01:04 AM »
Hi,

 never used any third party profiler, you shoudl go with comments and rating to choose amongst the possible frameworks.

bye,

 Jean