Ejemplo n.º 1
0
        private void CreateEditor()
        {
            if (m_editor != null)
            {
                Destroy(m_editor);
            }

            if (Editor.Selection.activeObject == null)
            {
                return;
            }

            UnityObject[] selectedObjects = Editor.Selection.objects.Where(o => o != null).ToArray();
            if (selectedObjects.Length != 1)
            {
                return;
            }

            Type objType = selectedObjects[0].GetType();

            for (int i = 1; i < selectedObjects.Length; ++i)
            {
                if (objType != selectedObjects[i].GetType())
                {
                    return;
                }
            }

            GameObject editorPrefab;

#if !UNITY_WEBGL && PROC_MATERIAL
            if (objType == typeof(Material) || objType == typeof(ProceduralMaterial))
#else
            if (objType == typeof(Material))
#endif

            {
                Material mat = selectedObjects[0] as Material;
                if (mat.shader == null)
                {
                    return;
                }

                editorPrefab = EditorsMap.GetMaterialEditor(mat.shader);
            }
            else
            {
                if (!EditorsMap.IsObjectEditorEnabled(objType))
                {
                    return;
                }
                editorPrefab = EditorsMap.GetObjectEditor(objType);
            }

            if (editorPrefab != null)
            {
                m_editor = Instantiate(editorPrefab);
                m_editor.transform.SetParent(Panel, false);
            }
        }
        private EditorDescriptor GetScriptEditorDescriptor(Type t, EditorsMap editorsMap)
        {
            bool       isPropertyEditorType = typeof(PropertyEditor).IsAssignableFrom(t);
            bool       isEditorEnabled      = false;
            GameObject editor = null;

            if (m_map != null)
            {
                if (isPropertyEditorType)
                {
                    isEditorEnabled = editorsMap.IsPropertyEditorEnabled(t, true);
                    editor          = editorsMap.GetPropertyEditor(t, true);
                }
                else
                {
                    isEditorEnabled = editorsMap.IsObjectEditorEnabled(t);
                    editor          = editorsMap.GetObjectEditor(t, true);
                }
            }
            return(new EditorDescriptor(t, isEditorEnabled, editor, isPropertyEditorType));
        }
        private void Init()
        {
            m_map = Resources.Load <EditorsMapStorage>(EditorsMapStorage.EditorsMapPrefabName);
            if (m_map == null)
            {
                m_map = Resources.Load <EditorsMapStorage>(EditorsMapStorage.EditorsMapTemplateName);
            }

            GameObject editorsMapGo = new GameObject();
            EditorsMap editorsMap   = editorsMapGo.AddComponent <EditorsMap>();

            editorsMap.LoadMap();

            Assembly[] allAssemblies;
            Type[]     types;
            GetUOAssembliesAndTypes(out allAssemblies, out types);

            Assembly[] unityAssemblies = allAssemblies.Where(a => a != null && a.FullName != null && a.FullName.Contains("UnityEngine")).ToArray();
            Assembly[] otherAssemblies = allAssemblies.Where(a => a != null && a.FullName != null && !a.FullName.Contains("UnityEngine")).ToArray();

            m_objectEditorDescriptors = new[] { typeof(GameObject) }
            .Where(t => t.IsPublic && !t.IsGenericType)
            .Select(t => new EditorDescriptor(t, m_map != null && editorsMap.IsObjectEditorEnabled(t), m_map != null ? editorsMap.GetObjectEditor(t, true) : null, false)).ToArray();
            m_propertyEditorDescriptors = new[] { typeof(object), typeof(UnityEngine.Object), typeof(bool), typeof(Enum), typeof(List <>), typeof(Array), typeof(string), typeof(int), typeof(float), typeof(Range), typeof(Vector2), typeof(Vector3), typeof(Vector4), typeof(Quaternion), typeof(Color), typeof(Bounds), typeof(RangeInt), typeof(RangeOptions), typeof(HeaderText), typeof(MethodInfo) }
            .Where(t => t.IsPublic)
            .Select(t => new EditorDescriptor(t, m_map != null && editorsMap.IsPropertyEditorEnabled(t), m_map != null ? editorsMap.GetPropertyEditor(t, true) : null, true)).ToArray();
            m_stdComponentEditorDescriptors = unityAssemblies.SelectMany(a => a.GetTypes())
                                              .Where(t => typeof(Component).IsAssignableFrom(t) && t.IsPublic && !t.IsGenericType)
                                              .OrderBy(t => (t == typeof(Component)) ? string.Empty : t.Name)
                                              .Select(t => new EditorDescriptor(t, m_map != null && editorsMap.IsObjectEditorEnabled(t), m_map != null ? editorsMap.GetObjectEditor(t, true) : null, false)).ToArray();
            m_scriptEditorDescriptors = otherAssemblies.SelectMany(a => a.GetTypes())
                                        .Where(t => typeof(MonoBehaviour).IsAssignableFrom(t) && t.IsPublic && !t.IsGenericType)
                                        .OrderBy(t => t.FullName)
                                        .Select(t => GetScriptEditorDescriptor(t, editorsMap)).ToArray();

            List <Material> materials = new List <Material>();

            string[] assets = AssetDatabase.GetAllAssetPaths();
            foreach (string asset in assets)
            {
                if (!asset.EndsWith(".mat"))
                {
                    continue;
                }
                Material material = AssetDatabase.LoadAssetAtPath <Material>(asset);

                if (material == null ||
                    (material.hideFlags & HideFlags.DontSaveInBuild) != 0 ||
                    material.shader == null ||
                    (material.shader.hideFlags & HideFlags.DontSaveInBuild) != 0)
                {
                    continue;
                }
                materials.Add(material);
            }
            MaterialEditorDescriptor[] defaultDescriptors  = new[] { new MaterialEditorDescriptor(null, m_map != null && m_map.IsDefaultMaterialEditorEnabled, m_map != null ? m_map.DefaultMaterialEditor : null) };
            MaterialEditorDescriptor[] materialDescriptors = materials.Where(m => m.shader != null && !m.shader.name.StartsWith("Hidden/"))
                                                             .Select(m => m.shader).Distinct()
                                                             .OrderBy(s => s.name.StartsWith("Standard") ? string.Empty : s.name)
                                                             .Select(s => new MaterialEditorDescriptor(s, m_map != null && editorsMap.IsMaterialEditorEnabled(s), m_map != null ? editorsMap.GetMaterialEditor(s, true) : null)).ToArray();

            m_materialDescriptors = defaultDescriptors.Union(materialDescriptors).ToArray();

            DestroyImmediate(editorsMapGo);
        }
        private void Start()
        {
            m_editor = IOC.Resolve <IRTE>();

            GameObject          go               = m_editor.Selection.activeGameObject;
            ExposeToEditor      exposeToEditor   = go.GetComponent <ExposeToEditor>();
            HierarchyItem       hierarchyItem    = go.GetComponent <HierarchyItem>();
            HashSet <Component> ignoreComponents = new HashSet <Component>();

            if (exposeToEditor != null)
            {
                if (exposeToEditor.Colliders != null)
                {
                    for (int i = 0; i < exposeToEditor.Colliders.Length; ++i)
                    {
                        Collider collider = exposeToEditor.Colliders[i];
                        if (!ignoreComponents.Contains(collider))
                        {
                            ignoreComponents.Add(collider);
                        }
                    }
                }

                ignoreComponents.Add(exposeToEditor);
            }

            if (hierarchyItem != null)
            {
                ignoreComponents.Add(hierarchyItem);
            }

            InputName.text        = go.name;
            TogEnableDisable.isOn = go.activeSelf;

            InputName.onEndEdit.AddListener(OnEndEditName);
            TogEnableDisable.onValueChanged.AddListener(OnEnableDisable);
            Component[] components = go.GetComponents <Component>();
            for (int i = 0; i < components.Length; ++i)
            {
                Component component = components[i];
                if (component == null)
                {
                    continue;
                }

                if (ignoreComponents.Contains(component))
                {
                    continue;
                }

                if ((component.hideFlags & HideFlags.HideInInspector) != 0)
                {
                    continue;
                }

                if (EditorsMap.IsObjectEditorEnabled(component.GetType()))
                {
                    GameObject editorPrefab = EditorsMap.GetObjectEditor(component.GetType());
                    if (editorPrefab != null)
                    {
                        ComponentEditor componentEditorPrefab = editorPrefab.GetComponent <ComponentEditor>();
                        if (componentEditorPrefab != null)
                        {
                            ComponentEditor editor = Instantiate(componentEditorPrefab);
                            editor.EndEditCallback = () =>
                            {
                                m_editor.IsDirty = true;
                            };
                            editor.transform.SetParent(ComponentsPanel, false);
                            editor.Component = component;
                        }
                        else
                        {
                            Debug.LogErrorFormat("editor prefab {0} does not have ComponentEditor script", editorPrefab.name);
                        }
                    }
                }
            }
        }
        private void Init()
        {
            m_map = Resources.Load <EditorsMapStorage>(EditorsMapStorage.EditorsMapPrefabName);
            EditorsMap.LoadMap();

            Assembly unityAssembly       = typeof(GameObject).Assembly;
            Assembly unityEditorAssembly = typeof(Editor).Assembly;
            Assembly unityUI             = typeof(UnityEngine.UI.Button).Assembly;
            Assembly unityNet            = typeof(UnityEngine.Networking.NetworkBehaviour).Assembly;
            Assembly yetAnotherAssembly  = typeof(NetworkScenePostProcess).Assembly;

            Assembly[] otherAssemblies = AppDomain.CurrentDomain.GetAssemblies().Except(new[] { unityAssembly, unityEditorAssembly, unityUI, unityNet, yetAnotherAssembly }).ToArray();

            m_objectEditorDescriptors = new[] { typeof(GameObject) }
            .Where(t => t.IsPublic && !t.IsGenericType)
            .Select(t => new EditorDescriptor(t, m_map != null && EditorsMap.IsObjectEditorEnabled(t), m_map != null ? EditorsMap.GetObjectEditor(t, true) : null, false)).ToArray();
            m_propertyEditorDescriptors = new[] { typeof(object), typeof(UnityEngine.Object), typeof(bool), typeof(Enum), typeof(List <>), typeof(Array), typeof(string), typeof(int), typeof(float), typeof(Range), typeof(Vector2), typeof(Vector3), typeof(Vector4), typeof(Quaternion), typeof(Color), typeof(Bounds) }
            .Where(t => t.IsPublic)
            .Select(t => new EditorDescriptor(t, m_map != null && EditorsMap.IsPropertyEditorEnabled(t), m_map != null ? EditorsMap.GetPropertyEditor(t, true) : null, true)).ToArray();
            m_stdComponentEditorDescriptors = unityAssembly.GetTypes()
                                              .Where(t => typeof(Component).IsAssignableFrom(t) && t.IsPublic && !t.IsGenericType)
                                              .OrderBy(t => (t == typeof(Component)) ? string.Empty : t.Name)
                                              .Select(t => new EditorDescriptor(t, m_map != null && EditorsMap.IsObjectEditorEnabled(t), m_map != null ? EditorsMap.GetObjectEditor(t, true) : null, false)).ToArray();
            m_scriptEditorDescriptors = otherAssemblies.SelectMany(a => a.GetTypes())
                                        .Where(t => typeof(MonoBehaviour).IsAssignableFrom(t) && t.IsPublic && !t.IsGenericType)
                                        .OrderBy(t => t.FullName)
                                        .Select(t => new EditorDescriptor(t, m_map != null && EditorsMap.IsObjectEditorEnabled(t), m_map != null ? EditorsMap.GetObjectEditor(t, true) : null, false)).ToArray();

            List <Material> materials = new List <Material>();

            string[] assets = AssetDatabase.GetAllAssetPaths();
            foreach (string asset in assets)
            {
                if (!asset.EndsWith(".mat"))
                {
                    continue;
                }
                Material material = AssetDatabase.LoadAssetAtPath <Material>(asset);

                if (material == null ||
                    (material.hideFlags & HideFlags.DontSaveInBuild) != 0 ||
                    material.shader == null ||
                    (material.shader.hideFlags & HideFlags.DontSaveInBuild) != 0)
                {
                    continue;
                }
                materials.Add(material);
            }
            MaterialEditorDescriptor[] defaultDescriptors  = new[] { new MaterialEditorDescriptor(null, m_map != null && m_map.IsDefaultMaterialEditorEnabled, m_map != null ? m_map.DefaultMaterialEditor : null) };
            MaterialEditorDescriptor[] materialDescriptors = materials.Where(m => m.shader != null && !m.shader.name.StartsWith("Hidden/"))
                                                             .Select(m => m.shader).Distinct()
                                                             .OrderBy(s => s.name.StartsWith("Standard") ? string.Empty : s.name)
                                                             .Select(s => new MaterialEditorDescriptor(s, m_map != null && EditorsMap.IsMaterialEditorEnabled(s), m_map != null ? EditorsMap.GetMaterialEditor(s, true) : null)).ToArray();

            m_materialDescriptors = defaultDescriptors.Union(materialDescriptors).ToArray();
        }