Unity 框架

GlobalTool.cs 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Reflection;
  6. using System.Runtime.InteropServices;
  7. using System.Runtime.Serialization.Formatters.Binary;
  8. using System.Text;
  9. using UnityEngine;
  10. using UnityEngine.EventSystems;
  11. namespace TFramework
  12. {
  13. public static class GlobalTool
  14. {
  15. /// <summary>
  16. /// 当前鼠标是否停留在UGUI控件上
  17. /// </summary>
  18. /// <returns>是否</returns>
  19. public static bool IsPointerOverUGUI()
  20. {
  21. if (EventSystem.current)
  22. {
  23. #if UNITY_ANDROID && !UNITY_EDITOR
  24. if (Input.touchCount > 0)
  25. {
  26. return EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId);
  27. }
  28. else
  29. {
  30. return false;
  31. }
  32. #else
  33. return EventSystem.current.IsPointerOverGameObject();
  34. #endif
  35. }
  36. else
  37. {
  38. return false;
  39. }
  40. }
  41. #region 反射
  42. private static HashSet<string> RunTimeAssemblies = new HashSet<string>()
  43. {
  44. "Assembly-CSharp","UnityEngine", "UnityEngine.CoreModule", "UnityEngine.UI", "UnityEngine.PhysicsModule","UnityEngine.EventSystems","TMPro",
  45. "UnityEngine.Events","TFramework.RunTime","KeMingVR"
  46. };
  47. /// <summary>
  48. /// 从当前程序域实例化对象
  49. /// </summary>
  50. /// <typeparam name="T"></typeparam>
  51. /// <param name="fullName"></param>
  52. /// <returns></returns>
  53. public static T CretaInstanceToCurrentDomain<T>(string fullName)where T:class
  54. {
  55. foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
  56. foreach (var type in assembly.GetTypes())
  57. if (typeof(T).IsAssignableFrom(type))//判断type是否是其派生类
  58. if (type.IsClass && type.FullName == fullName)
  59. return assembly.CreateInstance(type.FullName) as T;
  60. return null;
  61. }
  62. /// <summary>
  63. /// 从当前程序域获取指定名Type
  64. /// </summary>
  65. /// <param name="typeFullName"></param>
  66. /// <returns></returns>
  67. public static Type GetTypeToCurrentDomain(string typeFullName)
  68. {
  69. Type type = null;
  70. foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
  71. {
  72. type = assembly.GetType(typeFullName)?? assembly.GetTypes().Find(p=>p.Name==typeFullName);
  73. if (type != null)
  74. return type;
  75. }
  76. return type;
  77. }
  78. /// <summary>
  79. /// 获取运行时当前程序域所有类型
  80. /// </summary>
  81. /// <returns></returns>
  82. public static List<Type> GetTypesInRunTimeAssemblies()
  83. {
  84. List<Type> types = new List<Type>();
  85. Assembly[] assemblys = AppDomain.CurrentDomain.GetAssemblies();
  86. for (int i = 0; i < assemblys.Length; i++)
  87. {
  88. types.AddRange(assemblys[i].GetTypes());
  89. }
  90. return types;
  91. }
  92. /// <summary>
  93. /// 获取运行时当前程序域所有满足条件类型
  94. /// </summary>
  95. /// <param name="filter">筛选条件</param>
  96. /// <returns></returns>
  97. public static List<Type> GetTypesInRunTimeAssemblies(TFunc<Type, bool> filter)
  98. {
  99. List<Type> types = new List<Type>();
  100. Assembly[] assemblys = AppDomain.CurrentDomain.GetAssemblies();
  101. for (int i = 0; i < assemblys.Length; i++)
  102. {
  103. Type[] ts = assemblys[i].GetTypes();
  104. foreach (var t in ts)
  105. {
  106. if (filter(t))
  107. {
  108. types.Add(t);
  109. }
  110. }
  111. }
  112. return types;
  113. }
  114. #endregion
  115. #region byte[]转换
  116. /// <summary>
  117. /// 将对象转换为byte数组
  118. /// </summary>
  119. /// <param name="obj">被转换对象</param>
  120. /// <returns>转换后byte数组</returns>
  121. public static byte[] Object2Bytes(object obj)
  122. {
  123. byte[] buff = obj != null ? new byte[Marshal.SizeOf(obj)] : new byte[0];
  124. if(obj!=null)
  125. {
  126. IntPtr ptr = Marshal.UnsafeAddrOfPinnedArrayElement(buff, 0);
  127. Marshal.StructureToPtr(obj, ptr, true);
  128. }
  129. return buff;
  130. }
  131. /// <summary>
  132. /// 将byte数组转换成对象
  133. /// </summary>
  134. /// <param name="buff">被转换byte数组</param>
  135. /// <param name="typ">转换成的类名</param>
  136. /// <returns>转换完成后的对象</returns>
  137. public static object Bytes2Object(byte[] buff, Type type)
  138. {
  139. IntPtr ptr = Marshal.UnsafeAddrOfPinnedArrayElement(buff, 0);
  140. return Marshal.PtrToStructure(ptr, type);
  141. }
  142. public static Texture2D Bytes2Texture2D(byte[] buff)
  143. {
  144. Texture2D texture2D = new Texture2D(512, 512);
  145. texture2D.LoadImage(buff);
  146. return texture2D;
  147. }
  148. /// <summary>
  149. /// 将字节数转换为最合适的存储单位表示
  150. /// </summary>
  151. /// <param name="bytes">要转换的字节数</param>
  152. /// <returns>带有单位的字符串表示,如"1KB"、"2.5MB"等</returns>
  153. public static string CalculateStorageSize(long bytes)
  154. {
  155. // 定义单位换算系数
  156. const long kb = 1024;
  157. const long mb = kb * 1024;
  158. const long gb = mb * 1024;
  159. // 根据数值大小选择合适的单位
  160. if (bytes >= gb)
  161. {
  162. double value = (double)bytes / gb;
  163. // 如果是整数则不显示小数
  164. return value % 1 == 0 ? $"{(long)value}G" : $"{value:F1}G";
  165. }
  166. else if (bytes >= mb)
  167. {
  168. double value = (double)bytes / mb;
  169. return value % 1 == 0 ? $"{(long)value}M" : $"{value:F1}M";
  170. }
  171. else if (bytes >= kb)
  172. {
  173. double value = (double)bytes / kb;
  174. return value % 1 == 0 ? $"{(long)value}K" : $"{value:F1}K";
  175. }
  176. else
  177. {
  178. // 小于1KB的情况直接返回字节数
  179. return $"{bytes}B";
  180. }
  181. }
  182. #endregion
  183. #region 截屏
  184. /// <summary>
  185. /// 截取全屏
  186. /// </summary>
  187. /// <param name="superSize">分辨率的增加倍数</param>
  188. /// <param name="onCaptureOverEvent">截取完成事件</param>
  189. public static void CaptureScreenshot(int superSize,TAction<Texture2D> onCaptureOverEvent)
  190. {
  191. Main.Instance.StartCoroutine(RecordFrame(superSize, onCaptureOverEvent));
  192. }
  193. private static IEnumerator RecordFrame(int superSize, TAction<Texture2D> onCaptureOverEvent)
  194. {
  195. yield return YieldWaitTool.YieldWaitForEndOfFrame();
  196. onCaptureOverEvent?.Invoke(ScreenCapture.CaptureScreenshotAsTexture(superSize));
  197. }
  198. /// <summary>
  199. /// 截取指定相机拍摄区域
  200. /// </summary>
  201. /// <param name="camera">截图相机</param>
  202. /// <param name="rect">截取区域</param>
  203. /// <returns></returns>
  204. public static Texture2D CameraCapture(Camera camera,Rect rect)
  205. {
  206. RenderTexture render = new RenderTexture((int)rect.width, (int)rect.height, -1);
  207. camera.targetTexture = render;
  208. camera.Render();
  209. RenderTexture currentRT = RenderTexture.active;
  210. RenderTexture.active = render;
  211. Texture2D tex = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.ARGB32, false);
  212. tex.ReadPixels(rect, 0, 0);
  213. tex.Apply();
  214. camera.targetTexture = null;
  215. RenderTexture.active = currentRT; ;
  216. GameObject.Destroy(render);
  217. return tex;
  218. }
  219. /// <summary>
  220. /// 截取屏幕指定区域
  221. /// </summary>
  222. /// <param name="rect">截取区域</param>
  223. /// <param name="onCaptureOverEvent">截取完毕事件</param>
  224. public static void ScreenCaptureRect(Rect rect, TAction<Texture2D> onCaptureOverEvent)
  225. {
  226. Main.Instance.StartCoroutine(RecordFrame(rect, onCaptureOverEvent));
  227. }
  228. private static IEnumerator RecordFrame(Rect rect, TAction<Texture2D> onCaptureOverEvent)
  229. {
  230. yield return YieldWaitTool.YieldWaitForEndOfFrame();
  231. Texture2D tex = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.ARGB32, false);
  232. tex.ReadPixels(rect, 0, 0);
  233. tex.Apply();
  234. onCaptureOverEvent?.Invoke(tex);
  235. }
  236. #endregion
  237. #region 贴图转换
  238. public static Sprite Texture2DToSprite(Texture2D texture2D) =>
  239. Sprite.Create(texture2D, new Rect(0, 0, texture2D.width, texture2D.height), Vector2.zero);
  240. public static Sprite TextureToSprite(Texture texture)
  241. {
  242. Texture2D texture2D = TextureToTexture2D(texture);
  243. return Texture2DToSprite(texture2D);
  244. }
  245. public static Texture2D TextureToTexture2D(Texture texture)
  246. {
  247. RenderTexture renderTexture = RenderTexture.GetTemporary(texture.width, texture.height, 32);
  248. Graphics.Blit(texture, renderTexture);
  249. Texture2D texture2D = RenderTextureToTexture2D(renderTexture);
  250. RenderTexture.ReleaseTemporary(renderTexture);
  251. return texture2D;
  252. }
  253. public static Texture2D RenderTextureToTexture2D(RenderTexture renderTexture)
  254. {
  255. RenderTexture currentRT = RenderTexture.active;
  256. RenderTexture.active = renderTexture;
  257. Rect rect = new Rect(0, 0, renderTexture.width, renderTexture.height);
  258. Texture2D texture2D = new Texture2D(renderTexture.width, renderTexture.height);
  259. texture2D.ReadPixels(rect, 0, 0);
  260. texture2D.Apply();
  261. RenderTexture.active = currentRT;
  262. return texture2D;
  263. }
  264. #endregion
  265. #region GameObject
  266. #endregion
  267. }
  268. }