Ejemplo n.º 1
0
        static Material DoRevertMaterial(Material material, MaterialInfo matInfo)
        {
            Material backupMaterial = null;
            bool     splitMaterial  = false;

            if (material.name.EndsWith(kStaticSuffix) || material.name.EndsWith(kDynmcSuffix))
            {
                string path    = GetMaterialPath(material);
                string matName = material.name.Split(new string[] { kStaticSuffix, kDynmcSuffix }, System.StringSplitOptions.None)[0];
                backupMaterial = AssetDatabase.LoadAssetAtPath <Material>(path + "/" + matName + ".mat");
                splitMaterial  = true;
            }
            else
            {
                backupMaterial = s_materialHistory.m_backupMaterials.Find(delegate(Material entry)
                {
                    return(entry != null && (kBackupPrefix + material.name) == entry.name);
                });
            }

            // If a backup was found restore the material and cleanup the backup file
            if (backupMaterial != null)
            {
                if (splitMaterial)
                {
                    // update all static target sites
                    HashSet <Renderer> .Enumerator iter = matInfo.GetTargets().GetEnumerator();

                    while (iter.MoveNext())
                    {
                        // find the split material source and replace it with the split material
                        Material[] mats = iter.Current.sharedMaterials;
                        for (int m = 0, k = mats.Length; m < k; ++m)
                        {
                            if (mats[m].name == material.name)
                            {
                                mats[m] = backupMaterial;
                            }
                        }

                        iter.Current.sharedMaterials = mats;

                        // dirty the mesh renderer
                        EditorUtility.SetDirty(iter.Current);
                    }

                    // delete the old split asset
                    AssetDatabase.DeleteAsset(GetMaterialPath(material) + "/" + material.name + ".mat");
                    material = backupMaterial;
                }
                else
                {
                    material.shader = backupMaterial.shader;
                    material.CopyPropertiesFromMaterial(backupMaterial);
                    EditorUtility.SetDirty(material);
                    AssetDatabase.SaveAssets();

                    // delete backup
                    DestroyImmediate(backupMaterial, true);
                }
            }

            return(material);
        }
Ejemplo n.º 2
0
        static void DoConvertMaterial(uint state, Material material, MaterialInfo matInfo)
        {
            // force repaint
            m_repaint = true;

            // --------------------------------------------//
            // Not in the scene
            if (state == 0)
            {
                MakeMaterialBackup(material);
                ConvertToDynamicLighting(material);
            }
            // --------------------------------------------//
            // Static lit
            else if (state == StaticSceneState.kStatic)
            {
                MakeMaterialBackup(material);
                ConvertToStaticLighting(material);
            }
            // --------------------------------------------//
            // Dynamic lit
            else if (state == StaticSceneState.kNotStatic)
            {
                MakeMaterialBackup(material);
                ConvertToDynamicLighting(material);
            }
            // --------------------------------------------//
            // Mixed between static and dynamic lit
            else if (state == StaticSceneState.kMixed)
            {
                // duplicate material and convert the duplicate
                Material staticLitMat = null;
                Material dynmcLitMat  = null;
                MakeOrLoadSplitMaterial(material, out staticLitMat, out dynmcLitMat);

                staticLitMat = ConvertToStaticLighting(staticLitMat);
                dynmcLitMat  = ConvertToDynamicLighting(dynmcLitMat);

                // update all static target sites
                HashSet <Renderer> .Enumerator iter = matInfo.GetTargets().GetEnumerator();

                while (iter.MoveNext())
                {
                    // find the split material source and replace it with the split material
                    Material[] mats    = iter.Current.sharedMaterials;
                    Material[] newMats = new Material[mats.Length];
                    for (int m = 0, k = mats.Length; m < k; ++m)
                    {
                        if (mats[m].name == material.name)
                        {
                            if (IsStaticLit(iter.Current))
                            {
                                newMats[m] = staticLitMat;
                            }
                            else
                            {
                                newMats[m] = dynmcLitMat;
                            }
                        }
                        else
                        {
                            newMats[m] = mats[m];
                        }
                    }

                    iter.Current.sharedMaterials = newMats;

                    // dirty the mesh renderer
                    EditorUtility.SetDirty(iter.Current);
                    EditorUtility.SetDirty(material);
                }
            }
        }