internal static bool IsDefaultFieldEditor(FieldEditor editor)
 {
     return(editor == objectEditor);
 }
예제 #2
0
        /// <summary>
        /// Unity's OnEnable.
        /// </summary>
        protected virtual void OnEnable()
        {
            hideFlags = HideFlags.HideAndDontSave;
            FieldEditor.Gather();
            Gather();

            AdvancedInspectorAttribute.OnForceRefresh += DataChanged;
            AdvancedInspectorControl.SortingChanged   += Sort;

            if (targets == null || targets.Length == 0)
            {
                return;
            }

            Instances = targets;
            if (Instances[0] == null)
            {
                return;
            }

            advanced = InspectorPreferences.InspectDefaultItems;
            Type type = Instances[0].GetType();

            if (!advanced)
            {
                if (TestForDefaultInspector)
                {
                    object[] attributes = type.GetCustomAttributes(typeof(AdvancedInspectorAttribute), true);
                    if (attributes.Length != 0)
                    {
                        advanced = true;
                    }
                }
                else
                {
                    advanced = true;
                }
            }
            else if (type != null && type.Namespace != null)
            {
                if (type.Namespace.Contains("UnityEngine") && !InspectorEditorByTypes.ContainsKey(type))
                {
                    advanced = false;
                }
            }

            if (advanced)
            {
                foreach (object instance in Instances)
                {
                    if (instance is IDataChanged)
                    {
                        ((IDataChanged)instance).OnDataChanged += DataChanged;
                    }
                }

                InspectorField.OnDataChanged += DataChanged;
            }

            Repaint();
        }
        internal static void Gather()
        {
            if (fieldEditors.Count != 0)
            {
                return;
            }

            fieldEditors.Clear();
            fieldEditorByTypes.Clear();
            fieldEditorByNames.Clear();

            propertyDrawers.Clear();
            propertyDrawersByTypes.Clear();

            Type      propertyType = typeof(CustomPropertyDrawer);
            FieldInfo fieldType    = propertyType.GetField("m_Type", BindingFlags.Instance | BindingFlags.NonPublic);
            FieldInfo fieldDerived = propertyType.GetField("m_UseForChildren", BindingFlags.Instance | BindingFlags.NonPublic);

            foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                foreach (Type type in assembly.GetTypes())
                {
                    if (type.IsAbstract || type.IsGenericType)
                    {
                        continue;
                    }

                    if (typeof(FieldEditor).IsAssignableFrom(type))
                    {
                        FieldEditor editor = (FieldEditor)Activator.CreateInstance(type);
                        fieldEditors.Add(editor);
                        fieldEditorByNames.Add(type.Name, editor);

                        if (editor.EditedTypes.Length == 1 && editor.EditedTypes[0] == typeof(UnityEngine.Object))
                        {
                            objectEditor = editor;
                        }
                    }
                    else if (typeof(PropertyDrawer).IsAssignableFrom(type))
                    {
                        object[] attributes = type.GetCustomAttributes(typeof(CustomPropertyDrawer), true);
                        if (attributes.Length == 0)
                        {
                            continue;
                        }

                        propertyDrawers.Add(new PropertyDrawerWrapper(type, (Type)fieldType.GetValue(attributes[0]), (bool)fieldDerived.GetValue(attributes[0])));
                    }
                }
            }

            foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                foreach (Type type in assembly.GetTypes())
                {
                    Type drawer = GetDrawer(type, false);

                    if (drawer != null)
                    {
                        propertyDrawersByTypes.Add(type, drawer);
                        continue;
                    }

                    FieldEditor editor = GetEditor(type, false);

                    if (editor != null)
                    {
                        fieldEditorByTypes.Add(type, editor);
                    }
                }
            }

            foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                foreach (Type type in assembly.GetTypes())
                {
                    if (!typeof(FieldEditor).IsAssignableFrom(type) || type.IsAbstract)
                    {
                        continue;
                    }

                    FieldEditor drawer = Activator.CreateInstance(type) as FieldEditor;
                    if (drawer == null)
                    {
                        continue;
                    }

                    foreach (Type attribute in drawer.EditedTypes)
                    {
                        if (attributeEditors.ContainsKey(attribute))
                        {
                            Debug.LogError(string.Format("Two FieldDrawers - {0} and {1} - are linked to the same FieldAttribute type: {2}.", type.Name, attributeEditors[attribute].GetType().Name, attribute.Name));
                            continue;
                        }

                        attributeEditors.Add(attribute, drawer);
                    }
                }
            }
        }