MiraCastEditor.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using EZXR.Glass.Core;
  2. using EZXR.Glass.Rendering;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEditor;
  6. using UnityEditor.PackageManager;
  7. using UnityEditor.PackageManager.Requests;
  8. using UnityEngine;
  9. namespace EZXR.Glass.MiraCast
  10. {
  11. public class MiraCastEditor : EditorWindow
  12. {
  13. static AddRequest addRequest;
  14. static RemoveRequest removeRequest;
  15. static Queue<string> packageNameQueue;
  16. [MenuItem("GameObject/XR Abilities/Additional.../MiraCast/Enable", false, 100)]
  17. public static void EnableMiraCast()
  18. {
  19. var symbols = PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android);
  20. if (!symbols.Contains("ARMiracast"))
  21. {
  22. if (EditorUtility.DisplayDialog("Importing Package", "Start Import Packages: \n1) com.unity.renderstreaming@3.1.0-exp.2\n2) com.unity.inputsystem@1.0.2\n3) com.unity.webrtc@2.4.0-exp.4\n\n\nPlease make sure that this will not make conflicts!", "Yes"))
  23. {
  24. EditorApplication.update += PackageAddProgress;
  25. EditorUtility.DisplayProgressBar("Importing Package", "Is importing package, please wait...", 0);
  26. addRequest = Client.Add("com.unity.renderstreaming@3.1.0-exp.2");
  27. }
  28. }
  29. else
  30. {
  31. //AddMiracastManager();
  32. //AddMiracastUI();
  33. }
  34. UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene());
  35. AssetDatabase.SaveAssets();
  36. }
  37. //[MenuItem("GameObject/XR Abilities/Additional.../MiraCast/Enable", true, 100)]
  38. //static bool ValidateEnableMiraCast()
  39. //{
  40. // return !PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android).Contains("ARMiracast");
  41. //}
  42. #region 移除Miracast
  43. [MenuItem("GameObject/XR Abilities/Additional.../MiraCast/Disable", false, 100)]
  44. public static void DisableMiraCast()
  45. {
  46. //如果场景中存在MiracastManager,则删除
  47. MiracastManager miracastManager = FindObjectOfType<MiracastManager>();
  48. if (miracastManager != null)
  49. {
  50. DestroyImmediate(miracastManager.gameObject);
  51. }
  52. //如果场景中存在PhoneScreenUI/Canvas/MiracastUI,则删除
  53. PhoneUIRenderer phoneScreenUI = FindObjectOfType<PhoneUIRenderer>();
  54. if (phoneScreenUI != null)
  55. {
  56. Transform miracastUI = phoneScreenUI.transform.Find("Canvas/MiracastUI");
  57. if (miracastUI != null)
  58. {
  59. DestroyImmediate(miracastUI.gameObject);
  60. }
  61. }
  62. UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene());
  63. AssetDatabase.SaveAssets();
  64. if (EditorUtility.DisplayDialog("MiraCast相关资源移除确认", "是否移除MiraCast导入的PackageManager包?", "OK", "No"))
  65. {
  66. RemoveMiracastFromProject();
  67. }
  68. }
  69. [MenuItem("GameObject/XR Abilities/Additional.../MiraCast/Disable", true, 100)]
  70. static bool ValidateDisableMiraCast()
  71. {
  72. return PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android).Contains("ARMiracast");
  73. }
  74. /// <summary>
  75. /// 从工程中彻底移除Miracast带来的改变,包括packagemanager中引入的package和ScriptingDefineSymbols改变
  76. /// </summary>
  77. static void RemoveMiracastFromProject()
  78. {
  79. //去掉ScriptingDefineSymbols中的ARMiracast,避免移除package的时候会因为引用丢失而报错
  80. string symbols = PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android);
  81. if (symbols.Contains("ARMiracast;"))
  82. {
  83. symbols = symbols.Replace("ARMiracast;", "");
  84. PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android, symbols);
  85. }
  86. else if (symbols.Contains("ARMiracast"))
  87. {
  88. symbols = symbols.Replace("ARMiracast", "");
  89. PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android, symbols);
  90. }
  91. else
  92. {
  93. return;
  94. }
  95. // callback for every frame in the editor
  96. EditorApplication.update += PackageRemovalProgress;
  97. EditorApplication.LockReloadAssemblies();
  98. EditorUtility.DisplayProgressBar("Removing Package", "Is removing package, please wait...", 0);
  99. removeRequest = Client.Remove("com.unity.renderstreaming");
  100. }
  101. static void PackageRemovalProgress()
  102. {
  103. if (removeRequest.IsCompleted)
  104. {
  105. switch (removeRequest.Status)
  106. {
  107. case StatusCode.Failure: // couldn't remove package
  108. EditorUtility.ClearProgressBar();
  109. Debug.LogError("Couldn't remove package '" + removeRequest.PackageIdOrName + "': " + removeRequest.Error.message);
  110. break;
  111. case StatusCode.InProgress:
  112. break;
  113. case StatusCode.Success:
  114. EditorUtility.ClearProgressBar();
  115. Debug.Log("Removed package: " + removeRequest.PackageIdOrName);
  116. break;
  117. }
  118. EditorApplication.update -= PackageRemovalProgress;
  119. EditorApplication.UnlockReloadAssemblies();
  120. }
  121. return;
  122. }
  123. #endregion
  124. static void PackageAddProgress()
  125. {
  126. if (addRequest != null && addRequest.IsCompleted)
  127. {
  128. switch (addRequest.Status)
  129. {
  130. case StatusCode.Failure:
  131. EditorUtility.ClearProgressBar();
  132. Debug.LogError("Couldn't add package '" + "': " + addRequest.Error.message);
  133. break;
  134. case StatusCode.InProgress:
  135. break;
  136. case StatusCode.Success:
  137. EditorUtility.ClearProgressBar();
  138. Debug.Log("Added package: " + addRequest.Result.name);
  139. //向ScriptingDefineSymbols添加ARMiracast
  140. var symbols = PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android);
  141. if (!symbols.Contains("ARMiracast"))
  142. {
  143. symbols += ";ARMiracast";
  144. PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android, symbols);
  145. }
  146. //AddMiracastManager();
  147. //AddMiracastUI();
  148. UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene());
  149. AssetDatabase.SaveAssets();
  150. break;
  151. }
  152. EditorApplication.update -= PackageAddProgress;
  153. }
  154. }
  155. static void AddMiracastManager()
  156. {
  157. //如果场景中不存在MiracastManager的话需要实例化一个
  158. if (FindObjectOfType<MiracastManager>() == null)
  159. {
  160. Core.PrefabUtility.InstantiatePrefabWithUndoAndSelection(ResourcesManager.Load<GameObject>("MiracastManager"));
  161. }
  162. }
  163. static void AddMiracastUI()
  164. {
  165. //如果场景中不存在PhoneScreenUI的话需要实例化一个,并且向其下的Canvas添加MiracastUI
  166. PhoneUIRenderer phoneScreenUI = FindObjectOfType<PhoneUIRenderer>();
  167. if (phoneScreenUI == null)
  168. {
  169. phoneScreenUI = (Core.PrefabUtility.InstantiatePrefabWithUndoAndSelection(ResourcesManager.Load<GameObject>("PhoneScreenUI")) as GameObject).GetComponent<PhoneUIRenderer>();
  170. }
  171. if (phoneScreenUI != null)
  172. {
  173. if (phoneScreenUI.transform.Find("Canvas/MiracastUI") == null)
  174. {
  175. Core.PrefabUtility.InstantiatePrefabWithUndoAndSelection(ResourcesManager.Load<GameObject>("MiracastUI"), phoneScreenUI.transform.Find("Canvas")).name = "MiracastUI";
  176. }
  177. }
  178. }
  179. }
  180. }