Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

World to Gui problem

Discussion in 'Immediate Mode GUI (IMGUI)' started by Zeritel, Apr 25, 2009.

  1. Zeritel

    Zeritel

    Joined:
    Apr 25, 2009
    Posts:
    10
    I'm trying to do a toggleable display for npc info in a game I'm working on. I went around the forums and it seems like most examples use some form of Camera.WorldToScreenPoint. I tried this and I'm having some wonky issues. The GUI only really appears in the proper place when it's in the center of the camera. I'm using Unity 2.5 on a Windows XP(32) machine, and C#

    In my scene I've got an empty game object parented under some enemies in my scene that is feeding in it's transforms to the following script:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Healthbar : MonoBehaviour
    6. {
    7.  
    8.     public Texture2D controlTexture;
    9.     public Transform anchorPos;
    10.     static bool allOn;
    11.     public bool active;
    12.  
    13.     void Start()
    14.     {
    15.         active=true;
    16.     }  
    17.        
    18.     void OnGUI ()
    19.     {
    20.         if(active || allOn)
    21.         {
    22.             Vector3 screenPos = Camera.main.WorldToScreenPoint(anchorPos.position);
    23.             GUI.BeginGroup (new Rect (screenPos.x, screenPos.y,120,50));
    24.                 GUI.Label(new Rect (0,0,    120, 50) ,controlTexture);
    25.                 GUI.Label(new Rect(10,5,120,50), "1000 m/s");
    26.                 GUI.Label(new Rect(80,5,120,50), "200m");
    27.             GUI.EndGroup ();
    28.         }
    29.     }
    30.  
    I've tried using ScreenToGUIPoint in the conversion too, and it didn't help. I've also tried WorldToViewportPoint which also didn't seem to resolve the problem...It's probably something really simple that I'm missing. any ideas?
     
  2. Zeritel

    Zeritel

    Joined:
    Apr 25, 2009
    Posts:
    10
    So I found the issue.

    When working in GUI space (0,0) is located in the upper left-hand corner.

    The WorldToScreenPoint() however returns the value as if (0,0) is located at the bottom left.


    So to resolve everything I just had to change one line from:

    Code (csharp):
    1.          
    2. GUI.BeginGroup (new Rect (screenPos.x, screenPos.y,120,50));
    3.  
    to:

    Code (csharp):
    1.          
    2. GUI.BeginGroup (new Rect (screenPos.x, Camera.main.pixelHeight-screenPos.y,120,50));
    3.  
    Hope this helps anyone who has a similar problem in the future =)
     
  3. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    Whoa, welcome to the forums fellow SF resident! I can't really add to this as you've sorted things out, I just thought I'd say hello. :)