Skip to content

Unity plugin that allows you to make multiple editors that target the same type, and manage from the editor.

License

Notifications You must be signed in to change notification settings

favoyang/multiple-editors

 
 

Repository files navigation

Muffin Dev for Unity - Multiple Editors Manager

Allows you to create multiple editor extensions for the same target type through code. In the editor, you can use the manager's window to set these custom editor options : enable/disable them, change their order, etc.

Preview of the Multiple Editors Manager window

Inspector view when demos enabled

Architecture

The Multiple Editors system involve three classes: CustomEditorExtension, MultipleEditorsHandler and MultipleEditorsManager:

  • CustomEditorExtension represents an editor extension. Each additional editor inherits from that class
  • MultipleEditorsHandler allows you to create a regular Editor that will use the extensions (it's the editor actually drawn in the inspector)
  • MultipleEditorsManager binds extensions and multiple editors handlers. All extensions have to register with the MultipleEditorsManager, and so the handler can get the additional extensions that target the same type from the main manager

Demo

You can enable the Multiple Editors system demo by uncommenting the second line of the script at ./Editor/Demos/DemoCustomObjectEditor.cs.

This will add custom editor extensions to GameObject native inspector, Transform and Rigidbody components.

You can also change their order and settings from the Multiple Editors Manager window in Muffin Dev > Multiple Editors Manager.

Usage

In the following example, you'll see how to create a custom editor extension for GameObject:

using UnityEngine;
using UnityEditor;
using MuffinDev.EditorUtils.MultipleEditors;

// Use InitializeOnLoad attribute to ask for Unity to load this class after recompiling
[InitializeOnLoad]
public class GameObjectTestExtension : CustomEditorExtension<GameObject>
{
    // You must use the static constructor to register this custom editor extension
    static GameObjectTestExtension()
    {
        RegisterCustomEditor(() => { return new GameObjectTestExtension(); });
    }

    public override void OnHeaderGUI()
    {
        EditorGUILayout.HelpBox("Hello World!", MessageType.Info);
        EditorGUILayout.Space();
    }
}

CustomMultipleExtension usage result

There's some ready-to-use multiple editors managers in this module files, such as GameObjectMultipleEditors. This class uses [CustomEditor(typeof(GameObject))], meaning it customize the GameObject editor. Since that class is already defined, you just have to create your own extension using the code above, and register it with the MultipleEditorsManager using the RegisterCustomEditor() method.

Of course, this works also with your own Objects. Note that the CustomEditorExtension class is generic, and the type to specify defines the target type of your custom editor extension. Here we want to target GameObject, but you can use CustomEditorExtension<MyBehaviour> to target a custom type.

Note that register a custom editor extension must be done in the static constructor of your extensions.

Create your own multiple editors handler

First of all, create a custom MonoBehaviour:

using UnityEngine;

public class MyBehaviour : MonoBehaviour { }

Then, create the multiple editors handler for that specific type.

using UnityEngine;
using UnityEditor;
using MuffinDev.EditorUtils.MultipleEditors;

[CustomEditor(typeof(MyBehaviour))]
[CanEditMultipleObjects]
public class MyBehaviourMultipleEditorsHandler : MultipleEditorsHandler<MyBehaviour> { }

You must use CustomEditor attribute to tell Unity that this class is a custom editor of the target type (here MyBehaviour). The CanEditMultipleObject attribute is optional, but is needed if you want your custom editor extensions to deal with multiple selected objects.

Inheriting from MultipleEditorsHandler makes this class a user of the MultipleEditorsManager, so you can create custom extensions that target the type you specify (here MyBehaviour).

Note that you don't need any content in that class: it already works! Create your custom editor extensions by using CustomEditorExtension class. See the example above, or the CustomEditorExtension class documentation for more informations about creating extensions.

Note that native objects (like GameObject or Transform) can have a special behavior that prevents us to manipulate them as we can do for our custom types. See [NativeObjectMultipleEditorsHandler] for more informations about creating multiple editors handlers for native objects.

Classes

Data classes

Editor classes

Demos and presets

This package already contains multiple editors handlers for some built-in types. You can also find a script that contains several demo custom editor extensions:

  • DemoCustomObjectEditor.cs: Contains a bundle of custom editor extensions for GameObject, Transform and Rigidbody. Enable these demo classes by uncommenting the second line of the script
  • GameObjectMultipleEditors.cs: Implements NativeObjectMultipleEditorsHandler to create a multiple editors handler for GameObject
  • MonoScriptMultipleEditors.cs: Implements NativeObjectMultipleEditorsHandler to create a multiple editors handler for MonoScript
  • RigidbodyMultipleEditors.cs: Implements NativeObjectMultipleEditorsHandler to create a multiple editors handler for Rigidbody
  • TextAssetMultipleEditors.cs: Implements NativeObjectMultipleEditorsHandler to create a multiple editors handler for TextAsset
  • TransformMultipleEditors.cs: Implements NativeObjectMultipleEditorsHandler to create a multiple editors handler for Transform

License

This library uses the Creative Commons Attribution-ShareAlike 4.0 International license (CC BY-SA 4.0).

This means that you can use, share and modify the assets, code and other files as you want (even for commercial projects, but only under the following terms:

  • Give an "appropriate credit" (name the authors), and write a link to this package
  • If the source package is modified, you must distribute your contibutions under the same license (CC BY-SA 4.0)

Links

About

Unity plugin that allows you to make multiple editors that target the same type, and manage from the editor.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%