void OnDestroy()
            {
                //add itself
                bool   contains_itself = false;
                string self_guid       = UnityHelper.GetGUID((Material)materialProperty.targets[0]);

                for (int i = linked_materials.Count - 1; i >= 0; i--)
                {
                    if (UnityHelper.GetGUID(linked_materials[i]) == self_guid)
                    {
                        contains_itself = true;
                    }
                    if (linked_materials[i] == null)
                    {
                        linked_materials.RemoveAt(i);
                    }
                }
                if (linked_materials.Count > 0 && !contains_itself)
                {
                    linked_materials.Add((Material)materialProperty.targets[0]);
                }

                UpdateLinkList(linked_materials, materialProperty);
                Save();
            }
        private static void Save()
        {
            Dictionary <string, List <Material> > save_linked_materials = new Dictionary <string, List <Material> >(linked_materials);

            List <string[]>  save_structre          = new List <string[]>();
            HashSet <string> has_already_been_saved = new HashSet <string>();

            foreach (KeyValuePair <string, List <Material> > link in save_linked_materials)
            {
                if (has_already_been_saved.Contains(link.Key))
                {
                    continue;
                }
                string[] value = new string[link.Value.Count + 1];
                value[0] = System.Text.RegularExpressions.Regex.Split(link.Key, @"###")[1];
                int i = 1;
                foreach (Material m in link.Value)
                {
                    string guid = UnityHelper.GetGUID(m);
                    has_already_been_saved.Add(guid + "###" + value[0]);
                    value[i++] = guid;
                }
                save_structre.Add(value);
            }
            FileHelper.WriteStringToFile(Parser.ObjectToString(save_structre), PATH.LINKED_MATERIALS_FILE);
        }
        private static void BackupMaterialWithTests(Material m)
        {
            string guid = UnityHelper.GetGUID(m);
            string backedup_shader_name = FileHelper.LoadValueFromFile(guid, PATH.MATERIALS_BACKUP_FILE);

            if (m == null)
            {
                return;
            }
            if (MaterialShaderBroken(m))
            {
                return;
            }
            if (backedup_shader_name == m.shader.name)
            {
                return;
            }
            string default_shader_name = ShaderHelper.getDefaultShaderName(m.shader.name);

            if (default_shader_name.Equals(backedup_shader_name))
            {
                return;
            }
            FileHelper.SaveValueToFile(guid, default_shader_name, PATH.MATERIALS_BACKUP_FILE);
        }
 private static void BackupMaterial(Material m)
 {
     if (restoring_in_progress)
     {
         return;
     }
     if (!MaterialShaderBroken(m))
     {
         FileHelper.SaveValueToFile(UnityHelper.GetGUID(m), ShaderHelper.getDefaultShaderName(m.shader.name), PATH.MATERIALS_BACKUP_FILE);
     }
 }
            public void Init(List <Material> linked_materials, MaterialProperty p)
            {
                if (linked_materials == null)
                {
                    linked_materials = new List <Material>();
                }
                this.linked_materials = new List <Material>(linked_materials);

                string self_guid = UnityHelper.GetGUID((Material)p.targets[0]);

                for (int i = this.linked_materials.Count - 1; i >= 0; i--)
                {
                    if (UnityHelper.GetGUID(this.linked_materials[i]) == self_guid)
                    {
                        this.linked_materials.RemoveAt(i);
                    }
                }
                this.materialProperty = p;
            }
        public static void UnlinkAll(Material m)
        {
            string        guid        = UnityHelper.GetGUID(m);
            List <string> remove_keys = new List <string>();

            foreach (KeyValuePair <string, List <Material> > link_cloud in linked_materials)
            {
                if (link_cloud.Key.StartsWith(guid + "###"))
                {
                    link_cloud.Value.Remove(m);
                    remove_keys.Add(link_cloud.Key);
                }
            }
            foreach (string k in remove_keys)
            {
                linked_materials.Remove(k);
            }
            RemoveEmptyLinks();
            Save();
        }
 private static string GetKey(Material m, string p)
 {
     return(UnityHelper.GetGUID(m) + "###" + p);
 }