Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Display GUI to the side of Scene Object. (EditorExtension)

Discussion in 'Immediate Mode GUI (IMGUI)' started by instruct9r, Nov 18, 2015.

  1. instruct9r

    instruct9r

    Joined:
    Aug 1, 2012
    Posts:
    148
    Hello.

    I am trying to display a GUI, to the side of an object, that is selected.

    Everything works pretty well, except that i am having trouble to position the GUI, to be allways at the same side of the object, when i move the Scene Camera.

    Example. I need a helpBox to be drawn, below a cube. I am using the CUbe's position and subtract it's size, so the GUI is drawn below the cube. All works well..

    GUIBelowCube_01.jpg

    The problem comes, when i view the cube, from the Top, which looks like this:
    GUIBelowCube_02.jpg

    1: I need the GUI to be allways below the cube.
    2: I need to be able to move the GUI at the left, right, above, etc the cube.

    So i was wondering if i can create something like a virtual sphere with a radius of the size of the Selected.gameObject and then attach the GUI, at the side, that i want of the cube. Then i'll make that virtual sphere to allways be oriented to the scene camera... Or this is what comes to my mind at first :)..

    Here's the current Code..

    Code (CSharp):
    1. void OnSceneGuiTest(SceneView sceneTest)
    2.     {
    3.         Vector3 sceneCamPos = sceneTest.camera.transform.position;    // Get Scene Camera Position
    4.         GameObject[] sels = Selection.gameObjects;    // Get Selected Objects
    5.  
    6.         if (sels.Length > 0)
    7.         {
    8.             Bounds objBounds = sels[0].GetComponent<MeshRenderer>().bounds;
    9.          
    10.             Vector3 goPos = new Vector3(sels[0].transform.position.x, sels[0].transform.position.y - objBounds.size.y, sels[0].transform.position.z);
    11.          
    12.             Rect guiRect = HandleUtility.WorldPointToSizedRect(goPos, new GUIContent(), guiStyle);    // Convert the GameObject's position to screen Size Rect
    13.             guiRect.width = 150;    // Add size to the Rect
    14.             guiRect.height = 100;
    15.  
    16.             Handles.BeginGUI();
    17.             GUILayout.BeginArea(guiRect);
    18.             {
    19.                 EditorGUILayout.HelpBox("Test Message", MessageType.Error);    // Add helpBox below the SelectedGameObject
    20.             }
    21.             GUILayout.EndArea();
    22.             Handles.EndGUI();
    23.         }
    24.     }

    I suppose, that i need to use the sceneCamPos (As current camera position) and add it somewhere, but can't figure out where to use it..

    thanks :)
     
    Last edited: Nov 18, 2015