Installer.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using System;
  2. using System.IO;
  3. using UnityEngine;
  4. #if UNITY_EDITOR
  5. using UnityEditor;
  6. using UnityEditor.Build;
  7. using UnityEditor.SceneManagement;
  8. #endif
  9. namespace Kamgam.SkyClouds.URP
  10. {
  11. public class Installer
  12. #if UNITY_EDITOR
  13. : IActiveBuildTargetChanged
  14. #endif
  15. {
  16. public const string AssetName = "Sky Clouds URP";
  17. public const string Version = "1.1.0";
  18. public const string Define = "KAMGAM_SKY_CLOUDS_URP";
  19. public const string ManualUrl = "https://kamgam.com/unity/SkyCloudsURPManual.pdf";
  20. public const string AssetLink = "https://assetstore.unity.com/packages/slug/288138";
  21. public static string AssetRootPath = "Assets/Kamgam/SkyCloudsURP/";
  22. public static string ExamplePath = AssetRootPath + "Examples/SkyCloudsDemo.unity";
  23. public static Version GetVersion() => new Version(Version);
  24. #if UNITY_EDITOR
  25. [UnityEditor.Callbacks.DidReloadScripts(998001)]
  26. public static void InstallIfNeeded()
  27. {
  28. bool versionChanged = VersionHelper.UpgradeVersion(GetVersion, out Version oldVersion, out Version newVersion);
  29. if (versionChanged)
  30. {
  31. if (versionChanged)
  32. {
  33. Debug.Log(AssetName + " version changed from " + oldVersion + " to " + newVersion);
  34. if (AddDefineSymbol())
  35. {
  36. CrossCompileCallbacks.RegisterCallback(showWelcomeMessage);
  37. }
  38. else
  39. {
  40. showWelcomeMessage();
  41. }
  42. }
  43. }
  44. }
  45. public int callbackOrder => 0;
  46. public void OnActiveBuildTargetChanged(BuildTarget previousTarget, BuildTarget newTarget)
  47. {
  48. Logger.LogMessage($"Build target changed from {previousTarget} to {newTarget}. Refreshing define symbols.");
  49. AddDefineSymbol();
  50. }
  51. [MenuItem("Tools/" + AssetName + "/Debug/Add Defines", priority = 501)]
  52. private static void AddDefineSymbolMenu()
  53. {
  54. AddDefineSymbol();
  55. }
  56. private static bool AddDefineSymbol()
  57. {
  58. bool didChange = false;
  59. foreach (BuildTargetGroup targetGroup in System.Enum.GetValues(typeof(BuildTargetGroup)))
  60. {
  61. if (isObsolete(targetGroup))
  62. continue;
  63. if (targetGroup == BuildTargetGroup.Unknown)
  64. continue;
  65. #if UNITY_2023_1_OR_NEWER
  66. string currentDefineSymbols = PlayerSettings.GetScriptingDefineSymbols(NamedBuildTarget.FromBuildTargetGroup(targetGroup));
  67. #else
  68. string currentDefineSymbols = PlayerSettings.GetScriptingDefineSymbolsForGroup(targetGroup);
  69. #endif
  70. if (currentDefineSymbols.Contains(Define))
  71. continue;
  72. #if UNITY_2023_1_OR_NEWER
  73. PlayerSettings.SetScriptingDefineSymbols(NamedBuildTarget.FromBuildTargetGroup(targetGroup), currentDefineSymbols + ";" + Define);
  74. #else
  75. PlayerSettings.SetScriptingDefineSymbolsForGroup(targetGroup, currentDefineSymbols + ";" + Define);
  76. #endif
  77. // Logger.LogMessage($"{Define} symbol has been added for {targetGroup}.");
  78. didChange = true;
  79. }
  80. return didChange;
  81. }
  82. [MenuItem("Tools/" + AssetName + "/Debug/Remove Defines", priority = 502)]
  83. private static void RemoveDefineSymbol()
  84. {
  85. foreach (BuildTargetGroup targetGroup in System.Enum.GetValues(typeof(BuildTargetGroup)))
  86. {
  87. if (isObsolete(targetGroup))
  88. continue;
  89. #if UNITY_2023_1_OR_NEWER
  90. string currentDefineSymbols = PlayerSettings.GetScriptingDefineSymbols(NamedBuildTarget.FromBuildTargetGroup(targetGroup));
  91. #else
  92. string currentDefineSymbols = PlayerSettings.GetScriptingDefineSymbolsForGroup(targetGroup);
  93. #endif
  94. if (currentDefineSymbols.Contains(Define))
  95. {
  96. currentDefineSymbols = currentDefineSymbols.Replace(";" + Define, "");
  97. #if UNITY_2023_1_OR_NEWER
  98. PlayerSettings.SetScriptingDefineSymbols(NamedBuildTarget.FromBuildTargetGroup(targetGroup), currentDefineSymbols);
  99. #else
  100. PlayerSettings.SetScriptingDefineSymbolsForGroup(targetGroup, currentDefineSymbols);
  101. #endif
  102. Logger.LogMessage($"{Define} symbol has been removed for {targetGroup}.");
  103. }
  104. }
  105. }
  106. private static bool isObsolete(Enum value)
  107. {
  108. var fi = value.GetType().GetField(value.ToString());
  109. var attributes = (ObsoleteAttribute[]) fi.GetCustomAttributes(typeof(ObsoleteAttribute), inherit: false);
  110. return (attributes != null && attributes.Length > 0);
  111. }
  112. static void showWelcomeMessage()
  113. {
  114. bool openExample = EditorUtility.DisplayDialog(
  115. AssetName,
  116. "Thank you for choosing " + AssetName + ".\n\n" +
  117. "Please start by reading the manual.\n\n" +
  118. "If you can find the time I would appreciate your feedback in the form of a review.\n\n" +
  119. "I have prepared some examples for you.",
  120. "Open Example", "Open manual (web)"
  121. );
  122. if (openExample)
  123. OpenExample();
  124. else
  125. OpenManual();
  126. }
  127. [MenuItem("Tools/" + AssetName + "/Manual", priority = 101)]
  128. public static void OpenManual()
  129. {
  130. Application.OpenURL(ManualUrl);
  131. }
  132. [MenuItem("Tools/" + AssetName + "/Open Example Scene", priority = 103)]
  133. public static void OpenExample()
  134. {
  135. EditorApplication.delayCall += () =>
  136. {
  137. var scene = AssetDatabase.LoadAssetAtPath<SceneAsset>(ExamplePath);
  138. EditorGUIUtility.PingObject(scene);
  139. EditorSceneManager.OpenScene(ExamplePath);
  140. };
  141. }
  142. [MenuItem("Tools/" + AssetName + "/Please leave a review :-)", priority = 510)]
  143. public static void LeaveReview()
  144. {
  145. Application.OpenURL(AssetLink);
  146. }
  147. [MenuItem("Tools/" + AssetName + "/More Assets by KAMGAM", priority = 511)]
  148. public static void MoreAssets()
  149. {
  150. Application.OpenURL("https://kamgam.com/unity?ref=asset");
  151. }
  152. [MenuItem("Tools/" + AssetName + "/Version " + Version, priority = 512)]
  153. public static void LogVersion()
  154. {
  155. Debug.Log(AssetName + " v" + Version);
  156. }
  157. #endif
  158. }
  159. }