Ejemplo n.º 1
0
        /// <summary>
        /// Creates a new editor for the supplied node type.
        /// <param name="nodeType">The type of the node to be inspected.</param>
        /// <returns>An editor for the supplied type.</returns>
        /// </summary>
        public static NodeEditor CreateEditor(Type nodeType)
        {
            // Create type
            if (s_CustomEditors == null)
            {
                s_CustomEditors = new Dictionary <Type, Type>();

                foreach (Type editorType in EditorTypeUtility.GetDerivedTypes(typeof(NodeEditor)))
                {
                    var customEditorAttr = AttributeUtility.GetAttribute <CustomNodeEditorAttribute>(editorType, true);
                    if (customEditorAttr != null)
                    {
                        if (s_CustomEditors.ContainsKey(customEditorAttr.inspectedType))
                        {
                            s_CustomEditors[customEditorAttr.inspectedType] = editorType;
                        }
                        else
                        {
                            s_CustomEditors.Add(customEditorAttr.inspectedType, editorType);
                        }

                        // Add derived types?
                        if (customEditorAttr.editorForChildClasses)
                        {
                            foreach (var childType in TypeUtility.GetDerivedTypes(customEditorAttr.inspectedType))
                            {
                                if (!s_CustomEditors.ContainsKey(childType))
                                {
                                    s_CustomEditors.Add(childType, editorType);
                                }
                            }
                        }
                    }
                }
            }

            // Try get custom nodeeditor attribute
            Type customEditorType = null;

            s_CustomEditors.TryGetValue(nodeType, out customEditorType);
            if (customEditorType != null)
            {
                var nodeEditor = Activator.CreateInstance(customEditorType) as NodeEditor;
                if (nodeEditor != null)
                {
                    return(nodeEditor);
                }
            }

            return(new NodeEditor());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Returns the custom drawer for the supplied property attribute or null.
        /// <param name="attribute">The property attribute to search for a custom drawer.</param>
        /// <returns>The custom property drawer or null.</returns>
        /// </summary>
        public static NodePropertyDrawer GetDrawer(PropertyAttribute attribute)
        {
            if (attribute == null)
            {
                return(null);
            }

            // The custom drawer dictionary is not loaded?
            if (s_CustomDrawers == null)
            {
                s_CustomDrawers = new Dictionary <Type, Type>();
                foreach (Type t in EditorTypeUtility.GetDerivedTypes(typeof(NodePropertyDrawer)))
                {
                    var customDrawerAttr = AttributeUtility.GetAttribute <CustomNodePropertyDrawerAttribute>(t, false);
                    if (customDrawerAttr != null && !s_CustomDrawers.ContainsKey(customDrawerAttr.type))
                    {
                        s_CustomDrawers.Add(customDrawerAttr.type, t);
                    }
                }
            }

            // Try to get the type of the custom property drawer
            Type drawerType;

            s_CustomDrawers.TryGetValue(attribute.GetType(), out drawerType);
            if (drawerType != null)
            {
                // Create the drawer
                var drawer = Activator.CreateInstance(drawerType) as NodePropertyDrawer;
                if (drawer != null)
                {
                    // Set the attribute and return the drawer
                    drawer.attribute = attribute;
                    return(drawer);
                }
            }

            return(null);
        }