MixedRealityToolkit-Unity icon indicating copy to clipboard operation
MixedRealityToolkit-Unity copied to clipboard

Revisit MRTK Editor Design

Open Railboy opened this issue 8 years ago • 6 comments

Overview

MRTKEditor is a lightweight, attribute-based custom editor generator. It was designed to do the following:

  • Create custom editors that clarify / enhance the functionality of scripts while keeping all related logic inside the class being edited (as opposed to a separate script).
  • Reduce the time it takes to create / maintain custom editors.
  • Make it possible to inherit custom editor functionality from base classes.
  • Incorporate existing editor attributes (Eg Header, ToolTip, Range) into custom editors seamlessly.
  • Make it easy to edit private members without serializedProperties or editor-only get / set accessors.
  • Make it easy to turn custom editors OFF if they're not desired.

Examples of MRTKEditor can be seen in the InteractableObjectExample scene.

Defining an MRTK Editor

Editors are defined inside scripts as follows:

public class Foo : Monobehaviour {

     // class members

#if UNITY_EDITOR
     [CustomEditor(typeof(Foo))]
     private class Editor : MRTKEditor { }
#endif
}

Defining the class inside the target class enables access to the target's private members. Making it private helps to avoid a proliferation of public 'Editor' classes.

Overriding DrawCustomFooter is possible if you want to further customize the editor, but it's not necessary.

Using MRTK Editor Attributes

Editor features are activated with attributes, like so:

public class Foo : Monobehaviour {

     [ShowIfBool("BoolOption", false)]
     public int OnlyRelevantIfBoolOptionIsTrue;
     public bool BoolOption;

     [DropDownComponent]
     public Boo BooComponent;

#if UNITY_EDITOR
     [UnityEditor.CustomEditor(typeof(Foo))]
     private class Editor : MRTKEditor {}
#endif
}

The ShowIfBool attribute automatically hides the OnlyRelevantIfBoolOptionIsTrue member if BoolOption is false.

The DropDownComponent attribute creates an Object field that must be populated with an object of type Boo from the gameObject that Foo is attached to.

Other examples of MRTKEditor attributes can be found in Assets/HoloToolkit/Utilities/Scripts/Attributes

Inheriting from MRTK Editor base classes

MRTKEditor attributes from a base class' editor can be inherited like so:

public class Goo : Foo {

     [Header("New Members")]
     public int NewMember;

#if UNITY_EDITOR
     [UnityEditor.CustomEditor(typeof(Goo))]
     private new class Editor : MRTKEditor {}
#endif
}

Issues

MRTKEditor solves a lot of problems but it's also not considered industry-standard. This issue is an invitation to discuss ways to make the system better.

Railboy avatar Jan 05 '18 19:01 Railboy

I think my biggest point of contention for this is the fact that editor code, and the custom editor class are in the final build of any project.

StephenHodgson avatar Jan 05 '18 23:01 StephenHodgson

Having Editor code embedded within a production class for Unity is just wrong and is very bad design. A high focus should be kept on what code finally makes it in to a users project and editor code should NEVER be in production code.

The recommended practice with Editor code, is to keep it in separate class files and strongly named.

I agree about having a base MRTK_EDITOR class, as it promotes good editor functions within the MRTK, however we should not re recommending to break good Unity development practice by coalescing production and editor code.

The only area that is "supported" is gizmo code but that is only because it's embedded within the current MonoBehaviour class.

SimonDarksideJ avatar Jan 07 '18 18:01 SimonDarksideJ

I get where you're both coming from. For my part I'm a pragmatist - standard practice is a means to an end, not an end in itself, and if it kneecaps the greater goal of accessibility then I'm comfortable ignoring it.

That said, I don't like mixing editor / production code either, so I'm not really interested in dying on this hill.

So our goal is to find some way to check off the bullet points in the overview while keeping editor code quarantined.

We should probably start by deciding where we're comfortable drawing the line. Eg when are we comfortable using preprocessor directives, and when do we feel it's necessary to keep code in Editor folders? Should attributes follow the same rules as classes, or do they have more flexibility since there are fewer opportunities for accidental cross-talk? Stuff like that.

Railboy avatar Jan 08 '18 18:01 Railboy

Just one of the other things that we should consider is making sure the custom editor is responsive to size and layout. I know I have lots of issues with the inspectors not fitting completely into view (having a scroll bar at the bottom) and most of the inspector is hidden off screen until I pull it out a large distance, typically half of my monitors width, and even then the scroll bar isn't going away or handled properly.

image

StephenHodgson avatar Jan 12 '18 20:01 StephenHodgson

I think in general the MRTK custom editors do add a lot of value, but another concern is the fact that it adds another dependency to the scripts being imported into people's projects.

StephenHodgson avatar Jan 23 '18 19:01 StephenHodgson

Closing issues older than 180 days. If this is still an issue, please reactivate with recent information.

wiwei avatar Apr 11 '19 20:04 wiwei