PhotonEditorUtils.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. // ----------------------------------------------------------------------------
  2. // <copyright file="PhotonEditorUtils.cs" company="Exit Games GmbH">
  3. // PhotonNetwork Framework for Unity - Copyright (C) 2018 Exit Games GmbH
  4. // </copyright>
  5. // <summary>
  6. // Unity Editor Utils
  7. // </summary>
  8. // <author>developer@exitgames.com</author>
  9. // ----------------------------------------------------------------------------
  10. #pragma warning disable 618 // Deprecation warnings
  11. #if UNITY_2017_4_OR_NEWER
  12. #define SUPPORTED_UNITY
  13. #endif
  14. #if UNITY_EDITOR
  15. namespace Photon.Realtime
  16. {
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Linq;
  20. using UnityEditor;
  21. using UnityEngine;
  22. using System.IO;
  23. using System.Text;
  24. using UnityEngine.Networking;
  25. [InitializeOnLoad]
  26. public static class PhotonEditorUtils
  27. {
  28. /// <summary>Stores a flag which tells Editor scripts if the PhotonEditor.OnProjectChanged got called since initialization.</summary>
  29. /// <remarks>If not, the AssetDatabase is likely not usable yet and instances of ScriptableObject can't be loaded.</remarks>
  30. [Obsolete("Directly check EditorApplication.isUpdating to figure out if assets are being imported at the given time.")]
  31. public static bool ProjectChangedWasCalled
  32. {
  33. get
  34. {
  35. return UnityEditor.EditorApplication.isUpdating;
  36. }
  37. }
  38. /// <summary>True if the ChatClient of the Photon Chat API is available. If so, the editor may (e.g.) show additional options in settings.</summary>
  39. public static bool HasChat;
  40. /// <summary>True if the VoiceClient of the Photon Voice API is available. If so, the editor may (e.g.) show additional options in settings.</summary>
  41. public static bool HasVoice;
  42. /// <summary>True if PUN is in the project.</summary>
  43. public static bool HasPun;
  44. /// <summary>True if Photon Fusion is available in the project (and enabled).</summary>
  45. public static bool HasFusion;
  46. /// <summary>True if the PhotonEditorUtils checked the available products / APIs. If so, the editor may (e.g.) show additional options in settings.</summary>
  47. public static bool HasCheckedProducts;
  48. static PhotonEditorUtils()
  49. {
  50. HasVoice = Type.GetType("Photon.Voice.VoiceClient, Assembly-CSharp") != null || Type.GetType("Photon.Voice.VoiceClient, Assembly-CSharp-firstpass") != null || Type.GetType("Photon.Voice.VoiceClient, PhotonVoice.API") != null;
  51. HasChat = Type.GetType("Photon.Chat.ChatClient, Assembly-CSharp") != null || Type.GetType("Photon.Chat.ChatClient, Assembly-CSharp-firstpass") != null || Type.GetType("Photon.Chat.ChatClient, PhotonChat") != null;
  52. HasPun = Type.GetType("Photon.Pun.PhotonNetwork, Assembly-CSharp") != null || Type.GetType("Photon.Pun.PhotonNetwork, Assembly-CSharp-firstpass") != null || Type.GetType("Photon.Pun.PhotonNetwork, PhotonUnityNetworking") != null;
  53. #if FUSION_WEAVER
  54. HasFusion = true;
  55. #endif
  56. PhotonEditorUtils.HasCheckedProducts = true;
  57. if (EditorPrefs.HasKey("DisablePun") && EditorPrefs.GetBool("DisablePun"))
  58. {
  59. HasPun = false;
  60. }
  61. if (HasPun)
  62. {
  63. // MOUNTING SYMBOLS
  64. #if !PHOTON_UNITY_NETWORKING
  65. AddScriptingDefineSymbolToAllBuildTargetGroups("PHOTON_UNITY_NETWORKING");
  66. #endif
  67. #if !PUN_2_0_OR_NEWER
  68. AddScriptingDefineSymbolToAllBuildTargetGroups("PUN_2_0_OR_NEWER");
  69. #endif
  70. #if !PUN_2_OR_NEWER
  71. AddScriptingDefineSymbolToAllBuildTargetGroups("PUN_2_OR_NEWER");
  72. #endif
  73. #if !PUN_2_19_OR_NEWER
  74. AddScriptingDefineSymbolToAllBuildTargetGroups("PUN_2_19_OR_NEWER");
  75. #endif
  76. }
  77. }
  78. /// <summary>
  79. /// Adds a given scripting define symbol to all build target groups
  80. /// You can see all scripting define symbols ( not the internal ones, only the one for this project), in the PlayerSettings inspector
  81. /// </summary>
  82. /// <param name="defineSymbol">Define symbol.</param>
  83. public static void AddScriptingDefineSymbolToAllBuildTargetGroups(string defineSymbol)
  84. {
  85. foreach (BuildTarget target in Enum.GetValues(typeof(BuildTarget)))
  86. {
  87. BuildTargetGroup group = BuildPipeline.GetBuildTargetGroup(target);
  88. if (group == BuildTargetGroup.Unknown)
  89. {
  90. continue;
  91. }
  92. var defineSymbols = PlayerSettings.GetScriptingDefineSymbolsForGroup(group).Split(';').Select(d => d.Trim()).ToList();
  93. if (!defineSymbols.Contains(defineSymbol))
  94. {
  95. defineSymbols.Add(defineSymbol);
  96. try
  97. {
  98. PlayerSettings.SetScriptingDefineSymbolsForGroup(group, string.Join(";", defineSymbols.ToArray()));
  99. }
  100. catch (Exception e)
  101. {
  102. Debug.Log("Could not set Photon " + defineSymbol + " defines for build target: " + target + " group: " + group + " " + e);
  103. }
  104. }
  105. }
  106. }
  107. /// <summary>
  108. /// Removes PUN2's Script Define Symbols from project
  109. /// </summary>
  110. public static void CleanUpPunDefineSymbols()
  111. {
  112. foreach (BuildTarget target in Enum.GetValues(typeof(BuildTarget)))
  113. {
  114. BuildTargetGroup group = BuildPipeline.GetBuildTargetGroup(target);
  115. if (group == BuildTargetGroup.Unknown)
  116. {
  117. continue;
  118. }
  119. var defineSymbols = PlayerSettings.GetScriptingDefineSymbolsForGroup(group)
  120. .Split(';')
  121. .Select(d => d.Trim())
  122. .ToList();
  123. List<string> newDefineSymbols = new List<string>();
  124. foreach (var symbol in defineSymbols)
  125. {
  126. if ("PHOTON_UNITY_NETWORKING".Equals(symbol) || symbol.StartsWith("PUN_2_"))
  127. {
  128. continue;
  129. }
  130. newDefineSymbols.Add(symbol);
  131. }
  132. try
  133. {
  134. PlayerSettings.SetScriptingDefineSymbolsForGroup(group, string.Join(";", newDefineSymbols.ToArray()));
  135. }
  136. catch (Exception e)
  137. {
  138. Debug.LogErrorFormat("Could not set clean up PUN2's define symbols for build target: {0} group: {1}, {2}", target, group, e);
  139. }
  140. }
  141. }
  142. /// <summary>
  143. /// Gets the parent directory of a path. Recursive Function, will return null if parentName not found
  144. /// </summary>
  145. /// <returns>The parent directory</returns>
  146. /// <param name="path">Path.</param>
  147. /// <param name="parentName">Parent name.</param>
  148. public static string GetParent(string path, string parentName)
  149. {
  150. var dir = new DirectoryInfo(path);
  151. if (dir.Parent == null)
  152. {
  153. return null;
  154. }
  155. if (string.IsNullOrEmpty(parentName))
  156. {
  157. return dir.Parent.FullName;
  158. }
  159. if (dir.Parent.Name == parentName)
  160. {
  161. return dir.Parent.FullName;
  162. }
  163. return GetParent(dir.Parent.FullName, parentName);
  164. }
  165. /// <summary>
  166. /// Check if a GameObject is a prefab asset or part of a prefab asset, as opposed to an instance in the scene hierarchy
  167. /// </summary>
  168. /// <returns><c>true</c>, if a prefab asset or part of it, <c>false</c> otherwise.</returns>
  169. /// <param name="go">The GameObject to check</param>
  170. public static bool IsPrefab(GameObject go)
  171. {
  172. #if UNITY_2021_2_OR_NEWER
  173. return UnityEditor.SceneManagement.PrefabStageUtility.GetPrefabStage(go) != null || EditorUtility.IsPersistent(go);
  174. #elif UNITY_2018_3_OR_NEWER
  175. return UnityEditor.Experimental.SceneManagement.PrefabStageUtility.GetPrefabStage(go) != null || EditorUtility.IsPersistent(go);
  176. #else
  177. return EditorUtility.IsPersistent(go);
  178. #endif
  179. }
  180. //https://forum.unity.com/threads/using-unitywebrequest-in-editor-tools.397466/#post-4485181
  181. public static void StartCoroutine(System.Collections.IEnumerator update)
  182. {
  183. EditorApplication.CallbackFunction closureCallback = null;
  184. closureCallback = () =>
  185. {
  186. try
  187. {
  188. if (update.MoveNext() == false)
  189. {
  190. EditorApplication.update -= closureCallback;
  191. }
  192. }
  193. catch (Exception ex)
  194. {
  195. Debug.LogException(ex);
  196. EditorApplication.update -= closureCallback;
  197. }
  198. };
  199. EditorApplication.update += closureCallback;
  200. }
  201. public static System.Collections.IEnumerator HttpPost(string url, Dictionary<string, string> headers, byte[] payload, Action<string> successCallback, Action<string> errorCallback)
  202. {
  203. using (UnityWebRequest w = new UnityWebRequest(url, "POST"))
  204. {
  205. if (payload != null)
  206. {
  207. w.uploadHandler = new UploadHandlerRaw(payload);
  208. }
  209. w.downloadHandler = new DownloadHandlerBuffer();
  210. if (headers != null)
  211. {
  212. foreach (var header in headers)
  213. {
  214. w.SetRequestHeader(header.Key, header.Value);
  215. }
  216. }
  217. #if UNITY_2017_2_OR_NEWER
  218. yield return w.SendWebRequest();
  219. #else
  220. yield return w.Send();
  221. #endif
  222. while (w.isDone == false)
  223. yield return null;
  224. #if UNITY_2020_2_OR_NEWER
  225. if (w.result == UnityWebRequest.Result.ProtocolError || w.result == UnityWebRequest.Result.ConnectionError || w.result == UnityWebRequest.Result.DataProcessingError)
  226. #elif UNITY_2017_1_OR_NEWER
  227. if (w.isNetworkError || w.isHttpError)
  228. #endif
  229. {
  230. if (errorCallback != null)
  231. {
  232. errorCallback(w.error);
  233. }
  234. }
  235. else
  236. {
  237. if (successCallback != null)
  238. {
  239. successCallback(w.downloadHandler.text);
  240. }
  241. }
  242. }
  243. }
  244. /// <summary>
  245. /// Creates a Foldout using a toggle with (GUIStyle)"Foldout") and a separate label. This is a workaround for 2019.3 foldout arrows not working.
  246. /// </summary>
  247. /// <param name="isExpanded"></param>
  248. /// <param name="label"></param>
  249. /// <returns>Returns the new isExpanded value.</returns>
  250. public static bool Foldout(this SerializedProperty isExpanded, GUIContent label)
  251. {
  252. var rect = EditorGUILayout.GetControlRect();
  253. bool newvalue = EditorGUI.Toggle(new Rect(rect) { xMin = rect.xMin + 2 }, GUIContent.none, isExpanded.boolValue, (GUIStyle)"Foldout");
  254. EditorGUI.LabelField(new Rect(rect) { xMin = rect.xMin + 15 }, label);
  255. if (newvalue != isExpanded.boolValue)
  256. {
  257. isExpanded.boolValue = newvalue;
  258. isExpanded.serializedObject.ApplyModifiedProperties();
  259. }
  260. return newvalue;
  261. }
  262. /// <summary>
  263. /// Creates a Foldout using a toggle with (GUIStyle)"Foldout") and a separate label. This is a workaround for 2019.3 foldout arrows not working.
  264. /// </summary>
  265. /// <param name="isExpanded"></param>
  266. /// <param name="label"></param>
  267. /// <returns>Returns the new isExpanded value.</returns>
  268. public static bool Foldout(this bool isExpanded, GUIContent label)
  269. {
  270. var rect = EditorGUILayout.GetControlRect();
  271. bool newvalue = EditorGUI.Toggle(new Rect(rect) { xMin = rect.xMin + 2 }, GUIContent.none, isExpanded, (GUIStyle)"Foldout");
  272. EditorGUI.LabelField(new Rect(rect) { xMin = rect.xMin + 15 }, label);
  273. return newvalue;
  274. }
  275. }
  276. public class CleanUpDefinesOnPunDelete : UnityEditor.AssetModificationProcessor
  277. {
  278. public static AssetDeleteResult OnWillDeleteAsset(string assetPath, RemoveAssetOptions rao)
  279. {
  280. if ("Assets/Photon/PhotonUnityNetworking".Equals(assetPath))
  281. {
  282. PhotonEditorUtils.CleanUpPunDefineSymbols();
  283. }
  284. return AssetDeleteResult.DidNotDelete;
  285. }
  286. }
  287. }
  288. #endif