예제 #1
0
        static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
        {
            foreach (var asset in importedAssets)
            {
                if (!asset.ToLowerInvariant().EndsWith(".mat"))
                {
                    continue;
                }

                var material = (Material)AssetDatabase.LoadAssetAtPath(asset, typeof(Material));
                if (!HDShaderUtils.IsHDRPShader(material.shader, upgradable: true))
                {
                    continue;
                }

                HDShaderUtils.ShaderID id  = HDShaderUtils.GetShaderEnumFromShader(material.shader);
                var          latestVersion = k_Migrations.Length;
                var          wasUpgraded   = false;
                var          assetVersions = AssetDatabase.LoadAllAssetsAtPath(asset);
                AssetVersion assetVersion  = null;
                foreach (var subAsset in assetVersions)
                {
                    if (subAsset != null && subAsset.GetType() == typeof(AssetVersion))
                    {
                        assetVersion = subAsset as AssetVersion;
                        break;
                    }
                }

                //subasset not found
                if (!assetVersion)
                {
                    wasUpgraded            = true;
                    assetVersion           = ScriptableObject.CreateInstance <AssetVersion>();
                    assetVersion.hideFlags = HideFlags.HideInHierarchy | HideFlags.HideInInspector | HideFlags.NotEditable;
                    if (s_CreatedAssets.Contains(asset))
                    {
                        //just created
                        assetVersion.version = latestVersion;
                        s_CreatedAssets.Remove(asset);

                        //[TODO: remove comment once fixed]
                        //due to FB 1175514, this not work. It is being fixed though.
                        //delayed call of the following work in some case and cause infinite loop in other cases.
                        AssetDatabase.AddObjectToAsset(assetVersion, asset);
                    }
                    else
                    {
                        //asset exist prior migration
                        assetVersion.version = 0;
                        AssetDatabase.AddObjectToAsset(assetVersion, asset);
                    }
                }

                //upgrade
                while (assetVersion.version < latestVersion)
                {
                    k_Migrations[assetVersion.version](material, id);
                    assetVersion.version++;
                    wasUpgraded = true;
                }

                if (wasUpgraded)
                {
                    EditorUtility.SetDirty(assetVersion);
                    s_ImportedAssetThatNeedSaving.Add(asset);
                    s_NeedsSavingAssets = true;
                }
            }
        }