暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UnityUtil.cs 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 readonly List<Action> s_Callbacks = new List<Action>();
  12. private static volatile bool s_CallbacksPending;
  13. private static readonly 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. var objects = (GameObject[])FindObjectsOfType(typeof(GameObject));
  24. var result = new List<T>();
  25. foreach (var o in objects)
  26. {
  27. foreach (var mono in o.GetComponents<MonoBehaviour>())
  28. {
  29. if (mono is T)
  30. {
  31. result.Add(mono as T);
  32. }
  33. }
  34. }
  35. return result.ToArray();
  36. }
  37. public DateTime currentTime => DateTime.Now;
  38. public string persistentDataPath => Application.persistentDataPath;
  39. /// <summary>
  40. /// WARNING: Reading from this may require special application privileges.
  41. /// </summary>
  42. public string deviceUniqueIdentifier => SystemInfo.deviceUniqueIdentifier;
  43. public string unityVersion => Application.unityVersion;
  44. public string cloudProjectId => Application.cloudProjectId;
  45. public string userId => PlayerPrefs.GetString("unity.cloud_userid", String.Empty);
  46. public string gameVersion => Application.version;
  47. public UInt64 sessionId => UInt64.Parse(PlayerPrefs.GetString("unity.player_sessionid", "0"));
  48. public RuntimePlatform platform => Application.platform;
  49. public bool isEditor => Application.isEditor;
  50. public string deviceModel => SystemInfo.deviceModel;
  51. public string deviceName => SystemInfo.deviceName;
  52. public DeviceType deviceType => SystemInfo.deviceType;
  53. public string operatingSystem => SystemInfo.operatingSystem;
  54. public int screenWidth => Screen.width;
  55. public int screenHeight => Screen.height;
  56. public float screenDpi => Screen.dpi;
  57. public string screenOrientation => Screen.orientation.ToString();
  58. object IUtil.InitiateCoroutine(IEnumerator start)
  59. {
  60. return StartCoroutine(start);
  61. }
  62. void IUtil.InitiateCoroutine(IEnumerator start, int delay)
  63. {
  64. DelayedCoroutine(start, delay);
  65. }
  66. public void RunOnMainThread(Action runnable)
  67. {
  68. lock (s_Callbacks)
  69. {
  70. s_Callbacks.Add(runnable);
  71. s_CallbacksPending = true;
  72. }
  73. }
  74. public object GetWaitForSeconds(int seconds)
  75. {
  76. return new WaitForSeconds(seconds);
  77. }
  78. private void Start()
  79. {
  80. DontDestroyOnLoad(gameObject);
  81. }
  82. public static T FindInstanceOfType<T>() where T : MonoBehaviour
  83. {
  84. return (T)FindObjectOfType(typeof(T));
  85. }
  86. public static T LoadResourceInstanceOfType<T>() where T : MonoBehaviour
  87. {
  88. return ((GameObject)Instantiate(Resources.Load(typeof(T).ToString()))).GetComponent<T>();
  89. }
  90. public static bool PcPlatform()
  91. {
  92. return s_PcControlledPlatforms.Contains(Application.platform);
  93. }
  94. private IEnumerator DelayedCoroutine(IEnumerator coroutine, int delay)
  95. {
  96. yield return new WaitForSeconds(delay);
  97. StartCoroutine(coroutine);
  98. }
  99. private void Update()
  100. {
  101. if (!s_CallbacksPending)
  102. {
  103. return;
  104. }
  105. // We copy our actions to another array to avoid
  106. // locking the queue whilst we process them.
  107. Action[] copy;
  108. lock (s_Callbacks)
  109. {
  110. if (s_Callbacks.Count == 0)
  111. {
  112. return;
  113. }
  114. copy = new Action[s_Callbacks.Count];
  115. s_Callbacks.CopyTo(copy);
  116. s_Callbacks.Clear();
  117. s_CallbacksPending = false;
  118. }
  119. foreach (var action in copy)
  120. {
  121. action();
  122. }
  123. }
  124. private readonly List<Action<bool>> pauseListeners = new List<Action<bool>>();
  125. public void AddPauseListener(Action<bool> runnable)
  126. {
  127. pauseListeners.Add(runnable);
  128. }
  129. public void OnApplicationPause(bool paused)
  130. {
  131. foreach (var listener in pauseListeners)
  132. {
  133. listener(paused);
  134. }
  135. }
  136. public bool IsClassOrSubclass(Type potentialBase, Type potentialDescendant)
  137. {
  138. return potentialDescendant.IsSubclassOf(potentialBase) || potentialDescendant == potentialBase;
  139. }
  140. }
  141. }