Esempio n. 1
0
        public void Process()
        {
            if (this.goal == null)
            {
                Log.Warning("[MaterialMapper] : No goal shader selected.");
                return;
            }
            ProxyEditor.CreateAsset(new Material(this.goal), "Assets/Temporary.mat");
            string goalHeader = File.Find("Temporary.mat").ReadText().Cut("%YAML", "m_SavedProperties");
            string goalName   = goalHeader.Parse("m_Name:", "\n");

            ProxyEditor.DeleteAsset("Assets/Temporary.mat");
            List <string>   guids    = new List <string>();
            List <FileData> matching = new List <FileData>();

            FileData[] allMaterials = File.FindAll("*.mat");
            foreach (var shader in this.shaders)
            {
                if (shader == null)
                {
                    continue;
                }
                var path = File.GetAssetPath(shader);
                guids.AddNew(ProxyEditor.GetGUID(path));
            }
            ProxyEditor.StartAssetEditing();
            foreach (FileData materialFile in allMaterials)
            {
                Material material = materialFile.GetAsset <Material>();
                string   text     = materialFile.ReadText();
                foreach (string guid in guids)
                {
                    string idLine = "guid: " + guid;
                    bool   repair = material.shader.name.Contains("Hidden/InternalErrorShader");
                    if (repair || text.Contains(idLine))
                    {
                        string header = text.Cut("%YAML", "m_SavedProperties");
                        string name   = header.Parse("m_Name:", "\n");
                        foreach (ShaderInfo info in this.keywordMap)
                        {
                            if (!info.mapTo.ContainsAny("[Ignore]", "[No Matching]"))
                            {
                                text = text.Replace(" " + info.name, " " + info.mapTo);
                            }
                        }
                        text = text.Replace(header, goalHeader);
                        text = text.Replace(goalName, name);
                        materialFile.Write(text);
                        matching.AddNew(materialFile);
                        break;
                    }
                }
            }
            ProxyEditor.StopAssetEditing();
            MaterialCleaner.Clean(matching.ToArray());
            Log.Show("[MaterialMapper] : " + matching.Count + " materials modified.");
        }