UnityUtil.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Uniject;
  5. namespace UnityEngine.Purchasing.Extension
  6. {
  7. [HideInInspector]
  8. [AddComponentMenu("")]
  9. internal class UnityUtil : MonoBehaviour, IUtil
  10. {
  11. private static List<Action> s_Callbacks = new List<Action>();
  12. private static volatile bool s_CallbacksPending;
  13. private static List<RuntimePlatform> s_PcControlledPlatforms = new List<RuntimePlatform>
  14. {
  15. RuntimePlatform.LinuxPlayer,
  16. RuntimePlatform.OSXEditor,
  17. RuntimePlatform.OSXPlayer,
  18. RuntimePlatform.WindowsEditor,
  19. RuntimePlatform.WindowsPlayer,
  20. };
  21. public T[] GetAnyComponentsOfType<T>() where T : class
  22. {
  23. GameObject[] objects = (GameObject[])FindObjectsOfType(typeof(GameObject));
  24. List<T> result = new List<T>();
  25. foreach (GameObject o in objects)
  26. {
  27. foreach (MonoBehaviour mono in o.GetComponents<MonoBehaviour>())
  28. {
  29. if (mono is T)
  30. result.Add(mono as T);
  31. }
  32. }
  33. return result.ToArray();
  34. }
  35. public DateTime currentTime
  36. {
  37. get { return DateTime.Now; }
  38. }
  39. public string persistentDataPath
  40. {
  41. get { return Application.persistentDataPath; }
  42. }
  43. /// <summary>
  44. /// WARNING: Reading from this may require special application privileges.
  45. /// </summary>
  46. public string deviceUniqueIdentifier
  47. {
  48. get { return SystemInfo.deviceUniqueIdentifier; }
  49. }
  50. public string unityVersion
  51. {
  52. get { return Application.unityVersion; }
  53. }
  54. public string cloudProjectId
  55. {
  56. get { return Application.cloudProjectId; }
  57. }
  58. public string userId
  59. {
  60. get { return PlayerPrefs.GetString("unity.cloud_userid", String.Empty); }
  61. }
  62. public string gameVersion
  63. {
  64. get { return Application.version; }
  65. }
  66. public UInt64 sessionId
  67. {
  68. get { return UInt64.Parse(PlayerPrefs.GetString("unity.player_sessionid", "0")); }
  69. }
  70. public RuntimePlatform platform
  71. {
  72. get { return Application.platform; }
  73. }
  74. public bool isEditor
  75. {
  76. get { return Application.isEditor; }
  77. }
  78. public string deviceModel
  79. {
  80. get { return SystemInfo.deviceModel; }
  81. }
  82. public string deviceName
  83. {
  84. get { return SystemInfo.deviceName; }
  85. }
  86. public DeviceType deviceType
  87. {
  88. get { return SystemInfo.deviceType; }
  89. }
  90. public string operatingSystem
  91. {
  92. get { return SystemInfo.operatingSystem; }
  93. }
  94. public int screenWidth
  95. {
  96. get { return Screen.width; }
  97. }
  98. public int screenHeight
  99. {
  100. get { return Screen.height; }
  101. }
  102. public float screenDpi
  103. {
  104. get { return Screen.dpi; }
  105. }
  106. public string screenOrientation
  107. {
  108. get { return Screen.orientation.ToString(); }
  109. }
  110. object IUtil.InitiateCoroutine(IEnumerator start)
  111. {
  112. return StartCoroutine(start);
  113. }
  114. void IUtil.InitiateCoroutine(IEnumerator start, int delay)
  115. {
  116. DelayedCoroutine(start, delay);
  117. }
  118. public void RunOnMainThread(Action runnable)
  119. {
  120. lock (s_Callbacks)
  121. {
  122. s_Callbacks.Add(runnable);
  123. s_CallbacksPending = true;
  124. }
  125. }
  126. public object GetWaitForSeconds(int seconds)
  127. {
  128. return new WaitForSeconds(seconds);
  129. }
  130. private void Start()
  131. {
  132. DontDestroyOnLoad(gameObject);
  133. }
  134. public static T FindInstanceOfType<T>() where T : MonoBehaviour
  135. {
  136. return (T)FindObjectOfType(typeof(T));
  137. }
  138. public static T LoadResourceInstanceOfType<T>() where T : MonoBehaviour
  139. {
  140. return ((GameObject)Instantiate(Resources.Load(typeof(T).ToString()))).GetComponent<T>();
  141. }
  142. public static bool PcPlatform()
  143. {
  144. return s_PcControlledPlatforms.Contains(Application.platform);
  145. }
  146. private IEnumerator DelayedCoroutine(IEnumerator coroutine, int delay)
  147. {
  148. yield return new WaitForSeconds(delay);
  149. StartCoroutine(coroutine);
  150. }
  151. private void Update()
  152. {
  153. if (!s_CallbacksPending)
  154. return;
  155. // We copy our actions to another array to avoid
  156. // locking the queue whilst we process them.
  157. Action[] copy;
  158. lock (s_Callbacks)
  159. {
  160. if (s_Callbacks.Count == 0)
  161. return;
  162. copy = new Action[s_Callbacks.Count];
  163. s_Callbacks.CopyTo(copy);
  164. s_Callbacks.Clear();
  165. s_CallbacksPending = false;
  166. }
  167. foreach (var action in copy)
  168. action();
  169. }
  170. private List<Action<bool>> pauseListeners = new List<Action<bool>>();
  171. public void AddPauseListener(Action<bool> runnable)
  172. {
  173. pauseListeners.Add(runnable);
  174. }
  175. public void OnApplicationPause(bool paused)
  176. {
  177. foreach (var listener in pauseListeners)
  178. {
  179. listener(paused);
  180. }
  181. }
  182. public bool IsClassOrSubclass(Type potentialBase, Type potentialDescendant)
  183. {
  184. return potentialDescendant.IsSubclassOf(potentialBase) || potentialDescendant == potentialBase;
  185. }
  186. }
  187. }