Util.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. namespace SRDebugger.Internal
  2. {
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Reflection;
  6. using SRF.Helpers;
  7. using UnityEngine;
  8. using UnityEngine.EventSystems;
  9. public static class SRDebuggerUtil
  10. {
  11. public static bool IsMobilePlatform
  12. {
  13. get
  14. {
  15. if (Application.isMobilePlatform)
  16. {
  17. return true;
  18. }
  19. switch (Application.platform)
  20. {
  21. #if UNITY_5 || UNITY_5_3_OR_NEWER
  22. case RuntimePlatform.WSAPlayerARM:
  23. case RuntimePlatform.WSAPlayerX64:
  24. case RuntimePlatform.WSAPlayerX86:
  25. #else
  26. case RuntimePlatform.MetroPlayerARM:
  27. case RuntimePlatform.MetroPlayerX64:
  28. case RuntimePlatform.MetroPlayerX86:
  29. #endif
  30. return true;
  31. default:
  32. return false;
  33. }
  34. }
  35. }
  36. /// <summary>
  37. /// If no event system exists, create one
  38. /// </summary>
  39. /// <returns>True if the event system was created as a result of this call</returns>
  40. public static bool EnsureEventSystemExists()
  41. {
  42. if (!Settings.Instance.EnableEventSystemGeneration)
  43. {
  44. return false;
  45. }
  46. if (EventSystem.current != null)
  47. {
  48. return false;
  49. }
  50. var e = Object.FindObjectOfType<EventSystem>();
  51. // Check if EventSystem is in the scene but not registered yet
  52. if (e != null && e.gameObject.activeSelf && e.enabled)
  53. {
  54. return false;
  55. }
  56. Debug.LogWarning("[SRDebugger] No EventSystem found in scene - creating a default one. Disable this behaviour in Window -> SRDebugger -> Settings Window -> Advanced)");
  57. CreateDefaultEventSystem();
  58. return true;
  59. }
  60. public static void CreateDefaultEventSystem()
  61. {
  62. var go = new GameObject("EventSystem (Created by SRDebugger, disable in Window -> SRDebugger -> Settings Window -> Advanced)");
  63. go.AddComponent<EventSystem>();
  64. #if ENABLE_INPUT_SYSTEM && ENABLE_LEGACY_INPUT_MANAGER
  65. switch (Settings.Instance.UIInputMode)
  66. {
  67. case Settings.UIModes.NewInputSystem:
  68. AddInputSystem(go);
  69. Debug.LogWarning("[SRDebugger] Automatically generated EventSystem is using Unity Input System (can be changed to use Legacy Input in Window -> SRDebugger -> Settings Window -> Advanced)");
  70. break;
  71. case Settings.UIModes.LegacyInputSystem:
  72. AddLegacyInputSystem(go);
  73. Debug.LogWarning("[SRDebugger] Automatically generated EventSystem is using Legacy Input (can be changed to use Unity Input System in Window -> SRDebugger -> Settings Window -> Advanced)");
  74. break;
  75. }
  76. #elif ENABLE_INPUT_SYSTEM
  77. AddInputSystem(go);
  78. #elif ENABLE_LEGACY_INPUT_MANAGER || (!ENABLE_INPUT_SYSTEM && !UNITY_2019_3_OR_NEWER)
  79. AddLegacyInputSystem(go);
  80. #endif
  81. }
  82. #if ENABLE_INPUT_SYSTEM
  83. private static void AddInputSystem(GameObject go)
  84. {
  85. go.AddComponent<UnityEngine.InputSystem.UI.InputSystemUIInputModule>();
  86. // Disable/re-enable to force some initialization.
  87. // fix for input not being recognized until component is toggled off then on
  88. go.SetActive(false);
  89. go.SetActive(true);
  90. }
  91. #endif
  92. #if ENABLE_LEGACY_INPUT_MANAGER || (!ENABLE_INPUT_SYSTEM && !UNITY_2019_3_OR_NEWER)
  93. private static void AddLegacyInputSystem(GameObject go)
  94. {
  95. go.AddComponent<StandaloneInputModule>();
  96. }
  97. #endif
  98. /// <summary>
  99. /// Scan <paramref name="obj" /> for valid options and return a collection of them.
  100. /// </summary>
  101. /// <param name="obj"></param>
  102. /// <returns></returns>
  103. public static List<OptionDefinition> ScanForOptions(object obj)
  104. {
  105. var options = new List<OptionDefinition>();
  106. #if NETFX_CORE
  107. var members = obj.GetType().GetTypeInfo().DeclaredMembers;
  108. #else
  109. var members =
  110. obj.GetType().GetMembers(BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty |
  111. BindingFlags.SetProperty | BindingFlags.InvokeMethod);
  112. #endif
  113. foreach (var memberInfo in members)
  114. {
  115. // Find user-specified category name from attribute
  116. var categoryAttribute = SRReflection.GetAttribute<CategoryAttribute>(memberInfo);
  117. var category = categoryAttribute == null ? "Default" : categoryAttribute.Category;
  118. // Find user-specified sorting priority from attribute
  119. var sortAttribute = SRReflection.GetAttribute<SortAttribute>(memberInfo);
  120. var sortPriority = sortAttribute == null ? 0 : sortAttribute.SortPriority;
  121. // Find user-specified display name from attribute
  122. var nameAttribute = SRReflection.GetAttribute<DisplayNameAttribute>(memberInfo);
  123. var name = nameAttribute == null ? memberInfo.Name : nameAttribute.DisplayName;
  124. if (memberInfo is PropertyInfo)
  125. {
  126. var propertyInfo = memberInfo as PropertyInfo;
  127. // Only allow properties with public read/write
  128. #if NETFX_CORE
  129. if(propertyInfo.GetMethod == null)
  130. continue;
  131. // Ignore static members
  132. if (propertyInfo.GetMethod.IsStatic)
  133. continue;
  134. #else
  135. if (propertyInfo.GetGetMethod() == null)
  136. {
  137. continue;
  138. }
  139. // Ignore static members
  140. if ((propertyInfo.GetGetMethod().Attributes & MethodAttributes.Static) != 0)
  141. {
  142. continue;
  143. }
  144. #endif
  145. options.Add(new OptionDefinition(name, category, sortPriority,
  146. new SRF.Helpers.PropertyReference(obj, propertyInfo)));
  147. }
  148. else if (memberInfo is MethodInfo)
  149. {
  150. var methodInfo = memberInfo as MethodInfo;
  151. if (methodInfo.IsStatic)
  152. {
  153. continue;
  154. }
  155. // Skip methods with parameters or non-void return type
  156. if (methodInfo.ReturnType != typeof (void) || methodInfo.GetParameters().Length > 0)
  157. {
  158. continue;
  159. }
  160. options.Add(new OptionDefinition(name, category, sortPriority,
  161. new SRF.Helpers.MethodReference(obj, methodInfo)));
  162. }
  163. }
  164. return options;
  165. }
  166. public static string GetNumberString(int value, int max, string exceedsMaxString)
  167. {
  168. if (value >= max)
  169. {
  170. return exceedsMaxString;
  171. }
  172. return value.ToString();
  173. }
  174. public static void ConfigureCanvas(Canvas canvas)
  175. {
  176. if (Settings.Instance.UseDebugCamera)
  177. {
  178. canvas.worldCamera = Service.DebugCamera.Camera;
  179. canvas.renderMode = RenderMode.ScreenSpaceCamera;
  180. }
  181. }
  182. }
  183. }