Search Unity

How to display text in an editor viewport

Discussion in 'Scripting' started by ZoltanErdokovy, Aug 27, 2012.

  1. ZoltanErdokovy

    ZoltanErdokovy

    Joined:
    Aug 23, 2012
    Posts:
    102
    What would be the simplest way to display a text in an editor viewport (not in-game)?
    Something like the one which indicates direction of the orthogonal views (left/top/front).
    I'd like to display the orthographicSize value in the corner of each view (if applicable).
     
  2. rob4097

    rob4097

    Joined:
    May 15, 2012
    Posts:
    50
    The editor GUI can be accessed via EditorGUI in the OnGUI() method.

    http://docs.unity3d.com/Documentation/ScriptReference/EditorGUI.html

    http://docs.unity3d.com/Documentation/Components/gui-ExtendingEditor.html

    You can display text/controls/whatever in the same way that you would do so in game on the GUI, simply using EditorGUI or EditorGUILayout.button/label/whatever instead.

    Edit: I'm not particularly sure if the info above applies if you're trying to put that stuff in the actual editor viewport, but if you're ok with using an editor window (like the inspector) you can do it there.
     
  3. ZoltanErdokovy

    ZoltanErdokovy

    Joined:
    Aug 23, 2012
    Posts:
    102
    Since I need to display viewport specific information, the text should be
    draw onto each one separately.
     
  4. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    UnityGUI has no concept of view ports. The UI acts globally on the whole screen

    you will need to find the camera covering that area, use screen.height / width + normalized view rect data and calculate the proper positioning through that way
     
  5. ZoltanErdokovy

    ZoltanErdokovy

    Joined:
    Aug 23, 2012
    Posts:
    102
    Sorry what I meant was that custom text on the inspector is of no use since
    the value could be different in each and every viewport. Custom handles are
    also out since it should work without any selection or game objects present
    in the scene.

    Actually, I do have a single GO, hidden from the inspector, maybe I could use
    Gizmos.DrawLine() in there to draw some text... but that sounds hacky.
     
  6. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    5,041
    Code (csharp):
    1.  
    2.     [MenuItem ("Window/MyWindow")]
    3.     static void Init () {
    4.       MyWindow window = (MyWindow)EditorWindow.GetWindow (typeof (MyWindow));
    5.       if(SceneView.onSceneGUIDelegate != window.OnSceneGUI)
    6.       {
    7.         SceneView.onSceneGUIDelegate += window.OnSceneGUI;
    8.       }
    9.     }
    10.  
    11.     public void OnSceneGUI(SceneView view){
    12.       Handles.Label(Vector3.zero, "Instance Id = " + view.GetInstanceID());
    13.     }
    14.  
    You will need to track which scene view is which (for example you could check that the scene view was of the type of your custom view).

    Use the process dreamora suggested to place the text in the right spot (view.camera will have the details of the camera).

    EDIT: Note it doesn't have to be in a window, its just one example.
     
    Last edited: Aug 28, 2012
  7. ZoltanErdokovy

    ZoltanErdokovy

    Joined:
    Aug 23, 2012
    Posts:
    102
    Excellent, thank you!
     
  8. unitrade

    unitrade

    Joined:
    Aug 27, 2012
    Posts:
    20
    well this is good information. i have been having trouble with the problem. display a text but should be a simple way. anyways thank you