SkyStudioAssetPostProcessor.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. namespace Funly.SkyStudio
  6. {
  7. public class SkyStudioAssetPostProcessor : AssetPostprocessor
  8. {
  9. public static string migrationVersionKey = "SkyStudio-Migration-Version";
  10. public static int migrationVersion = 1;
  11. public static string migrationUnityVersionKey = "SkyStudio-Migration-Unity-Version";
  12. static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
  13. {
  14. if (importedAssets == null || importedAssets.Length == 0) {
  15. return;
  16. }
  17. // Certain asset paths force trigger migration, others we check using a version.
  18. if (!FileMigrationTriggers(importedAssets) && !ShouldRunFullMigration()) {
  19. return;
  20. }
  21. RunFullMigration();
  22. }
  23. // Check for certain paths that when imported indicate a need to migrate.
  24. static bool FileMigrationTriggers(string[] assets) {
  25. if (assets == null) {
  26. return false;
  27. }
  28. foreach (string asset in assets) {
  29. if (asset.Contains("SkyStudio3DStandard-GlobalKeywords")) {
  30. return true;
  31. }
  32. }
  33. return false;
  34. }
  35. static bool ShouldRunFullMigration() {
  36. // Check if migration is already current to this version.
  37. int lastVersion = EditorPrefs.GetInt(migrationVersionKey, 0);
  38. string lastUnityVersion = EditorPrefs.GetString(migrationUnityVersionKey, "");
  39. if (lastVersion == migrationVersion && lastUnityVersion == Application.version) {
  40. return false;
  41. }
  42. return true;
  43. }
  44. static void RunFullMigration() {
  45. EditorPrefs.SetInt(migrationVersionKey, migrationVersion);
  46. EditorPrefs.SetString(migrationUnityVersionKey, Application.version);
  47. // No migration necessary since we're on an older version of Unity.
  48. if (!SkyEditorUtility.SupportsLocalKeywords()) {
  49. return;
  50. }
  51. // Upgrade all the skybox materials in the project.
  52. string[] guids = AssetDatabase.FindAssets("t:material");
  53. foreach (string guid in guids) {
  54. string assetPath = AssetDatabase.GUIDToAssetPath(guid);
  55. if (assetPath == null) {
  56. continue;
  57. }
  58. // Upgrade the material if it's a legacy sky studio material.
  59. CheckAndRepairSkyStudioMaterial(assetPath);
  60. }
  61. // Delete the legacy shader since it's no longer being used by materials.
  62. CheckAndDeleteGlobalShaderKeywordsFile();
  63. }
  64. // Delete the version of the sky studio shader that uses global keywords if the
  65. // platform supports using local keywords.
  66. static void CheckAndDeleteGlobalShaderKeywordsFile() {
  67. if (!SkyEditorUtility.SupportsLocalKeywords()) {
  68. return;
  69. }
  70. Shader globalShader = Shader.Find(SkyProfile.DefaultLegacyShaderName);
  71. // Check if it's already been deleted.
  72. if (globalShader == null) {
  73. return;
  74. }
  75. string assetPath = AssetDatabase.GetAssetPath(globalShader);
  76. if (assetPath == null) {
  77. return;
  78. }
  79. if (!AssetDatabase.DeleteAsset(assetPath)) {
  80. Debug.LogWarning("Failed to delete legacy Sky Studio shader.");
  81. }
  82. AssetDatabase.SaveAssets();
  83. }
  84. static bool IsLegacySkyboxMaterial(Material material) {
  85. if (material == null || material.shader == null || material.shader.name == null) {
  86. return false;
  87. }
  88. return material.shader.name == SkyProfile.DefaultLegacyShaderName;
  89. }
  90. static bool UpgradeLegacySkyboxMaterial(Material material) {
  91. if (material == null) {
  92. return false;
  93. }
  94. material.shader = Shader.Find(SkyProfile.DefaultShaderName);
  95. EditorUtility.SetDirty(material);
  96. AssetDatabase.SaveAssets();
  97. return true;
  98. }
  99. // Update sky studio maerials to use to correct shader depending on Unity versions and features.
  100. static void CheckAndRepairSkyStudioMaterial(string assetPath) {
  101. Material material = AssetDatabase.LoadAssetAtPath<Material>(assetPath);
  102. if (material == null) {
  103. return;
  104. }
  105. if (!IsLegacySkyboxMaterial(material)) {
  106. return;
  107. }
  108. if (!UpgradeLegacySkyboxMaterial(material)) {
  109. Debug.LogWarning("Failed to upgrade legacy skybox material at path: " + assetPath);
  110. return;
  111. }
  112. }
  113. }
  114. }