Beispiel #1
0
        public static void LoadMap()
        {
            if (m_isLoaded)
            {
                return;
            }
            m_isLoaded = true;

            InitEditorsMap();

            EditorsMapStorage editorsMap = Resources.Load <EditorsMapStorage>(EditorsMapStorage.EditorsMapPrefabName);

            if (editorsMap != null)
            {
                m_editors = editorsMap.Editors;

                for (int i = 0; i < editorsMap.MaterialEditors.Length; ++i)
                {
                    GameObject materialEditor = editorsMap.MaterialEditors[i];
                    Shader     shader         = editorsMap.Shaders[i];
                    bool       enabled        = editorsMap.IsMaterialEditorEnabled[i];
                    if (!m_materialMap.ContainsKey(shader))
                    {
                        m_materialMap.Add(shader, new MaterialEditorDescriptor(materialEditor, enabled));
                    }
                    m_defaultMaterialEditor = editorsMap.DefaultMaterialEditor;
                }
            }
            else
            {
                Debug.LogError("Editors map is null");
            }
        }
Beispiel #2
0
        private void Init()
        {
            m_map = Resources.Load <EditorsMapStorage>(EditorsMapStorage.EditorsMapPrefabName);
            if (m_map == null)
            {
                m_map = Resources.Load <EditorsMapStorage>(EditorsMapStorage.EditorsMapTemplateName);
            }
            EditorsMap editorsMap = new EditorsMap();

            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) }
            .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 => 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();
        }
Beispiel #3
0
        public static void Generate(EditorDescriptor[] descriptors, MaterialEditorDescriptor[] materialDescriptors)
        {
            Dictionary <GameObject, int> editors = CreateComponentEditorMap(descriptors);

            if (!Directory.Exists(Path.GetFullPath(PrefabsPath + "/Resources")))
            {
                Directory.CreateDirectory(Path.GetFullPath(PrefabsPath + "/Resources"));
            }

            GameObject        go         = new GameObject();
            EditorsMapStorage editorsMap = go.AddComponent <EditorsMapStorage>();

            editorsMap.Editors = editors.OrderBy(k => k.Value).Select(k => k.Key).ToArray();

            MaterialEditorDescriptor defaultDescriptor = materialDescriptors.Where(d => d.Shader == null).FirstOrDefault();

            if (defaultDescriptor != null)
            {
                editorsMap.DefaultMaterialEditor          = defaultDescriptor.Editor;
                editorsMap.IsDefaultMaterialEditorEnabled = defaultDescriptor.Enabled;
            }
            else
            {
                editorsMap.DefaultMaterialEditor          = null;
                editorsMap.IsDefaultMaterialEditorEnabled = false;
            }

            materialDescriptors = materialDescriptors.Where(d => d != defaultDescriptor).ToArray();
            List <Shader>     shaders                = new List <Shader>();
            List <GameObject> materialEditors        = new List <GameObject>();
            List <bool>       materialEditorsEnabled = new List <bool>();

            for (int i = 0; i < materialDescriptors.Length; ++i)
            {
                MaterialEditorDescriptor descriptor = materialDescriptors[i];
                if (descriptor.Editor != null)
                {
                    shaders.Add(descriptor.Shader);
                    materialEditors.Add(descriptor.Editor);
                    materialEditorsEnabled.Add(descriptor.Enabled);
                }
            }

            editorsMap.Shaders                 = shaders.ToArray();
            editorsMap.MaterialEditors         = materialEditors.ToArray();
            editorsMap.IsMaterialEditorEnabled = materialEditorsEnabled.ToArray();

            string path = PrefabsPath + "/Resources/" + EditorsMapStorage.EditorsMapPrefabName + ".prefab";

            PrefabUtility.SaveAsPrefabAsset(go, path);
            UnityEngine.Object.DestroyImmediate(go);

            AssetDatabase.ImportAsset(ScriptsPath, ImportAssetOptions.ImportRecursive);
        }