123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEditor;
- namespace Funly.SkyStudio
- {
- public class SkyStudioAssetPostProcessor : AssetPostprocessor
- {
- public static string migrationVersionKey = "SkyStudio-Migration-Version";
- public static int migrationVersion = 1;
- public static string migrationUnityVersionKey = "SkyStudio-Migration-Unity-Version";
- static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
- {
- if (importedAssets == null || importedAssets.Length == 0) {
- return;
- }
-
- // Certain asset paths force trigger migration, others we check using a version.
- if (!FileMigrationTriggers(importedAssets) && !ShouldRunFullMigration()) {
- return;
- }
- RunFullMigration();
- }
-
- // Check for certain paths that when imported indicate a need to migrate.
- static bool FileMigrationTriggers(string[] assets) {
- if (assets == null) {
- return false;
- }
- foreach (string asset in assets) {
- if (asset.Contains("SkyStudio3DStandard-GlobalKeywords")) {
- return true;
- }
- }
- return false;
- }
- static bool ShouldRunFullMigration() {
- // Check if migration is already current to this version.
- int lastVersion = EditorPrefs.GetInt(migrationVersionKey, 0);
- string lastUnityVersion = EditorPrefs.GetString(migrationUnityVersionKey, "");
- if (lastVersion == migrationVersion && lastUnityVersion == Application.version) {
- return false;
- }
- return true;
- }
- static void RunFullMigration() {
- EditorPrefs.SetInt(migrationVersionKey, migrationVersion);
- EditorPrefs.SetString(migrationUnityVersionKey, Application.version);
-
- // No migration necessary since we're on an older version of Unity.
- if (!SkyEditorUtility.SupportsLocalKeywords()) {
- return;
- }
- // Upgrade all the skybox materials in the project.
- string[] guids = AssetDatabase.FindAssets("t:material");
- foreach (string guid in guids) {
- string assetPath = AssetDatabase.GUIDToAssetPath(guid);
- if (assetPath == null) {
- continue;
- }
- // Upgrade the material if it's a legacy sky studio material.
- CheckAndRepairSkyStudioMaterial(assetPath);
- }
- // Delete the legacy shader since it's no longer being used by materials.
- CheckAndDeleteGlobalShaderKeywordsFile();
- }
- // Delete the version of the sky studio shader that uses global keywords if the
- // platform supports using local keywords.
- static void CheckAndDeleteGlobalShaderKeywordsFile() {
- if (!SkyEditorUtility.SupportsLocalKeywords()) {
- return;
- }
-
- Shader globalShader = Shader.Find(SkyProfile.DefaultLegacyShaderName);
-
- // Check if it's already been deleted.
- if (globalShader == null) {
- return;
- }
- string assetPath = AssetDatabase.GetAssetPath(globalShader);
- if (assetPath == null) {
- return;
- }
-
- if (!AssetDatabase.DeleteAsset(assetPath)) {
- Debug.LogWarning("Failed to delete legacy Sky Studio shader.");
- }
- AssetDatabase.SaveAssets();
- }
- static bool IsLegacySkyboxMaterial(Material material) {
- if (material == null || material.shader == null || material.shader.name == null) {
- return false;
- }
- return material.shader.name == SkyProfile.DefaultLegacyShaderName;
- }
- static bool UpgradeLegacySkyboxMaterial(Material material) {
- if (material == null) {
- return false;
- }
- material.shader = Shader.Find(SkyProfile.DefaultShaderName);
- EditorUtility.SetDirty(material);
- AssetDatabase.SaveAssets();
- return true;
- }
- // Update sky studio maerials to use to correct shader depending on Unity versions and features.
- static void CheckAndRepairSkyStudioMaterial(string assetPath) {
- Material material = AssetDatabase.LoadAssetAtPath<Material>(assetPath);
- if (material == null) {
- return;
- }
- if (!IsLegacySkyboxMaterial(material)) {
- return;
- }
-
- if (!UpgradeLegacySkyboxMaterial(material)) {
- Debug.LogWarning("Failed to upgrade legacy skybox material at path: " + assetPath);
- return;
- }
- }
- }
- }
|