Search code examples
unity-game-engineinspector

Create Custom Unity Inspector Layout


So I have seen a few of these scripts in unity and I am wondering how I can recreate it in my own scripts?

So I have a script the when a object is clicked, it will call a function on another script. I want to be able to pass arguments and much more but the Inspector got messy with a bunch of toggles, int fields etc.

Is there any way to recreate this sort of thing?

A box that has a field for a object, a function and arguments if need

Not as much the Event type but the little function box instead. If that makes sense :)

Thanks!


Solution

  • The box you showed in that screenshot is an Event Trigger. There isn't any way that I know of that lets you separate the UI object from this underlying type. So there isn't a way to just draw the delegate box.. However, you can make a class based off of EventTrigger and then you can extend EventSystemEditor.

    You can also make a class derived from UnityEvent in this fashion and it will show up with a function delegate box the way you wanted it to.

    However, again, that just won't get you a drawing method that draws the function/delegate box for you. I don't know of a single way of getting Unity3D to let you do that.

    You can check the entire list of GUI drawing methods:

    https://docs.unity3d.com/ScriptReference/EditorGUILayout.html

    https://docs.unity3d.com/ScriptReference/EditorGUI.html

    But it's just not there.


    I know that some people have managed to achieve similar effects with Inspector wizardy, recreating a similar aspect and functionality. I know FullInspector can do delegates in its inspector and you could check its source code to figure out how, but it could end up being a lot of work. Specifically serializing the delegate is hellish, so if you want it to serialize, I wouldn't even bother. I suggest finding a different path of minimal resistance.

    Here's an alternative:

    You can collect the methods in the class you're targeting via reflection and present them in a dropdown menu. Then once a method is selected you can again use reflection to see what parameters that method requires and use GUI functions to draw the appropriate fields required to receive those parameters from user input.

    It's definitely possible, but again, I think that will take quite a lot of time to elaborate and you're better off finding a way of satisfying your requirements without this level of Editor GUI shenanigans.