Search Unity

Extend EditorGUI

Discussion in 'Immediate Mode GUI (IMGUI)' started by AlexandreRivet, Apr 12, 2017.

  1. AlexandreRivet

    AlexandreRivet

    Joined:
    Jan 30, 2015
    Posts:
    3
    Hello everyone,

    I want to make my own field for a class like a Vector3 or a Color.
    For a color field, we have a rect with the color and an eye dropper. When you click on the colored rect, a color picker is opened.
    So I have a class X with some members and I want to customize my field.
    I've done the customization of the field analyzing how it's done with color. My field is a label and a button that opens a window to configure deeper my object.
    Currently, the opening of the window works. However, how can I make the communication between window opened and inspector ?
    For color, I saw they have a GUIView that they store when they open the window and they send an Event from the window to this GUIView stored (to update the field). I noticed GUIView is an internal class so I can't access it.
    Do you have any idea to make something like that ?
    Is it possible ? Or do you have a by pass or another way to do it ?

    Thanks :)

    Alexandre
     
  2. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,311
    I doubt Unity will ever make GUIView public, it would be SUPER useful though. I've needed this functionality so many times. The reality is, you gotta bite the bullet and use Reflection.
     
  3. AlexandreRivet

    AlexandreRivet

    Joined:
    Jan 30, 2015
    Posts:
    3
    Thanks for your answer.
    I see.
    Is there any possibility to extend their EditorGUI with a native plugin or something ?
    I never dealt with reflection.
    Do you have an example of what I'm trying to do to help me to understand ?
    I think I'm not the only one that I want to do things like that ^^ And the subject has probably been resolved but I didn't find a answer ;(
     
  4. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,311
    Without an Enterprise license I'd say no. You would need to compile the Unity C# source into a dll and enable an external library access using the InternalsVisibleTo attribute.

    I've attached a GUIView wrapper I use to send events between views.

    Code (CSharp):
    1. //record current GUIView
    2. object view = GUIView.Current;
    3.  
    4. //open your window, pass the view above and do some processing
    5.  
    6. //send an event to the saved view, in this case, the inspector
    7. GUIView.SendEvent(view, EditorGUIUtility.CommandEvent("MyCustomEvent"));
     

    Attached Files:

    Last edited: Apr 13, 2017
  5. AlexandreRivet

    AlexandreRivet

    Joined:
    Jan 30, 2015
    Posts:
    3
    Thanks a lot.
    It works :)

    Now, reflection is my friend ^^
     
  6. lokinwai

    lokinwai

    Joined:
    Feb 11, 2015
    Posts:
    174
    Using this code can get the EditorWindow of current GUIView, so no need to use reflection after getting the window.

    Code (CSharp):
    1. var type = typeof(Editor).Assembly.GetType("UnityEditor.GUIView");
    2. var current = type.GetProperty("current", BindingFlags.Public | BindingFlags.Static);
    3. type = typeof(Editor).Assembly.GetType("UnityEditor.HostView");
    4. var actualView = type.GetProperty("actualView", BindingFlags.NonPublic | BindingFlags.Instance);
    5. var window = (EditorWindow)actualView.GetValue(current.GetValue(null, null), null);