Search Unity

How do I draw in the inspector?

Discussion in 'Immediate Mode GUI (IMGUI)' started by steinbitglis, May 1, 2012.

  1. steinbitglis

    steinbitglis

    Joined:
    Sep 22, 2011
    Posts:
    254
    I want to highlight a part of an image in the inspector. So far I'm having a lot of trouble drawing anything properly at all.

    I have the following code:

    Code (csharp):
    1.  
    2. import UnityEngine
    3. import UnityEditor
    4.  
    5. [CustomEditor(Thing)]
    6. class ThingEditor (Editor):
    7.     preview as RenderTexture
    8.  
    9.     def OnInspectorGUI():
    10.         unless preview:
    11.             preview = RenderTexture(256.0f, 256.0f, 24, RenderTextureFormat.ARGB32)
    12.             preview.Create()
    13.  
    14.         if Event.current.type == EventType.MouseDown and Event.current.button == 0:
    15.             Graphics.SetRenderTarget(preview)
    16.  
    17.             GL.PushMatrix()
    18.             GL.LoadOrtho()
    19.             GL.Clear(true, true, Color(0,0,0,0))
    20.             GL.Color(Color.red)
    21.             GL.Begin(GL.TRIANGLES)
    22.             GL.Vertex3(0.25F, 0.3F, 0)
    23.             GL.Vertex3(0.25F, 0.1351F, 0)
    24.             GL.Vertex3(0.5F, 0.3F, 0)
    25.             GL.End()
    26.             GL.PopMatrix()
    27.  
    28.             newPreview = true
    29.  
    30.         GUI.DrawTexture(GUILayoutUtility.GetRect(64.0f, 64.0f), preview)
    31.  
    32.         Repaint() if newPreview
    The results are as follows, after clicking 0, 1 and 2 times on the inspector window:



    Why does it flip the second time I click in the inspector?
    I have tried a lot of things to make in flip on the first click, with no success.

    http://unity3d.qatohost.com/questions/246853/how-to-draw-in-the-inspector.html
     
  2. MrMatthias

    MrMatthias

    Joined:
    Sep 18, 2012
    Posts:
    191
    The behaviour seems to become undefined if the renderTarget is not reset
    Code (CSharp):
    1.  
    2. var oldTarget = RenderTexture.active; // always null?
    3. Graphics.SetRenderTarget (preview);
    4.  
    5. // ...
    6.  
    7. Graphics.SetRenderTarget (oldTarget);
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    The finest of necroposts!
     
  4. MrMatthias

    MrMatthias

    Joined:
    Sep 18, 2012
    Posts:
    191
    The answer is targeted at people (like me) who search for this problem and find this thread, which has a solution now.
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Well, it's also a bad answer. Maybe it'd have been good in 2012, but these days we have the Handles class to draw in the inspector.
     
  6. MrMatthias

    MrMatthias

    Joined:
    Sep 18, 2012
    Posts:
    191
    suggesting a less capable higher-level api doesn't answer the problem.
     
    Rs likes this.