Generic Editor (Plugin)

This plugin allows users to easily create and use generic editor which can be changed of type on the fly while being incredibly versatile, and easy to use.

Note: This plugin is used inside the Time Rewizer plugin as well, and will be submitted to the unity assetstore SOON.

Links

Screenshots

Sample code

Here is some sample code of the reference manager, this part is handling all the creation of editor windows, aswell as the assembly search.

    using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using UnityEditor;
using UnityEngine;

namespace MarkOostveen.GenericEditor
{
    /// 
    ///     class managing generic inspector windows
    /// 
    [Author("Mark Oostveen", "markoostveen1@gmail.com", "http://markoostveen.nl/"), InitializeOnLoad]
    public static class GenericReferencedEditors
    {
        private struct TargetReference
        {
            public readonly Type EditorType;
            public readonly UnityEngine.Object Target;

            public TargetReference(Type editortype, UnityEngine.Object target)
            {
                EditorType = editortype;
                Target = target;
            }
        }

        private static Dictionary m_EditorReferences = new Dictionary();

        /// 
        /// Get all compatible types from generic Editor
        /// 
        public static Type[] CompatibleTypes
        {
            get
            {
                Type[] output = new Type[m_EditorReferences.Count];
                m_EditorReferences.Keys.CopyTo(output, 0);
                return output;
            }
        }

        private static Dictionary m_SharedEditors = new Dictionary();

        static GenericReferencedEditors()
        {
            Reload();
        }

        [MenuItem("MarkOostveen/GenericEditor/Reload Generic Editors")]
        private static void Reload()
        {
            m_EditorReferences = new Dictionary();
            m_SharedEditors = new Dictionary();

            Assembly[] active = AppDomain.CurrentDomain.GetAssemblies();

            for (int i = 0; i < active.Length; i++)
            {
                foreach (Type type in active[i].GetTypes())
                {
                    if (type.GetCustomAttribute(typeof(GenericEdtiorWindowAttribute)) is GenericEdtiorWindowAttribute
                        attribute)
                    {
                        m_EditorReferences.Add(attribute.m_Target, type);
                    }
                }
            }

            StringBuilder builder = new StringBuilder("Red all loaded Assemblies for new Generic Editor Definitions ");

            builder.Append(m_EditorReferences.Count);
            builder.Append(" results.\n");
            builder.Append("The following types can now be custom serialized. \n \n");

            List genericTypeEdtitors =
                new List(m_EditorReferences.Keys);

            string seperator1 = " From ";
            string seperator2 = " In ";
            string seperator3 = ", \n \n";

            for (int i = 0; i < m_EditorReferences.Count; i++)
            {
                builder.Append(genericTypeEdtitors[i].Name);
                builder.Append(seperator1);
                builder.Append(genericTypeEdtitors[i].Assembly.ManifestModule.Name);
                builder.Append(seperator2);
                builder.Append(genericTypeEdtitors[i].Namespace);
                builder.Append(seperator3);
            }

            Debug.Log(builder.ToString());
        }

        /// 
        /// Get Custom editor type for input type
        /// 
        /// 
        /// 
        public static Type GetEditorType(Type targetvariabletype)
        {
            m_EditorReferences.TryGetValue(targetvariabletype, out Type type);
            return type;
        }

        /// 
        ///     creates an generic editor window type based of type given
        /// 
        /// 
        /// 
        private static Editor GetEditor(Type editortargettype, UnityEngine.Object linkedunityobject, bool useshared)
        {
            if (useshared)
            {
                m_SharedEditors.TryGetValue(new TargetReference(editortargettype, linkedunityobject), out Editor output);
                if (output != null)
                    return output;
            }

            Editor editor = Editor.CreateEditor(linkedunityobject, editortargettype);

            if (useshared && editor != null)
                m_SharedEditors.Add(new TargetReference(editortargettype, linkedunityobject), editor);

            return editor;
        }

        /// 
        /// Search and create and return editor based on target type
        /// 
        /// 
        /// 
        public static DefaultGenericEditorType GetEditorFromEditorType(Type Editorvariabletype, UnityEngine.Object linkedobject, bool useshared = true)
        {
            return GetEditor(Editorvariabletype, linkedobject, useshared) as DefaultGenericEditorType;
        }

        /// 
        /// Search and create and return editor based on target type
        /// 
        /// 
        /// 
        public static DefaultGenericEditorType GetEditorFromTargetType(Type targetvariabletype, UnityEngine.Object linkedobject, bool useshared = true)
        {
            return GetEditor(GetEditorType(targetvariabletype), linkedobject, useshared) as DefaultGenericEditorType;
        }
    }
}