UnityUtility.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. namespace kt.Utility
  6. {
  7. public static class UnityUtility
  8. {
  9. public static GameObject GetChildObject(Transform transform, string hierarchyPath)
  10. {
  11. if (transform == null)
  12. {
  13. return GameObject.Find(hierarchyPath);
  14. }
  15. return (!transform.Find(hierarchyPath)) ? null : transform.Find(hierarchyPath).gameObject;
  16. }
  17. public static GameObject GetChildObject(GameObject obj, string hierarchyPath)
  18. {
  19. return UnityUtility.GetChildObject((!obj) ? null : obj.transform, hierarchyPath);
  20. }
  21. public static GameObject GetChildObject(string hierarchyPath)
  22. {
  23. return UnityUtility.GetChildObject(null, hierarchyPath);
  24. }
  25. public static T GetChildObject<T>(Transform transform, string hierarchyPath) where T : Component
  26. {
  27. GameObject childObject = UnityUtility.GetChildObject(transform, hierarchyPath);
  28. return (!childObject) ? ((T)((object)null)) : childObject.GetComponent<T>();
  29. }
  30. public static T GetChildObject<T>(GameObject obj, string hierarchyPath) where T : Component
  31. {
  32. return UnityUtility.GetChildObject<T>((!obj) ? null : obj.transform, hierarchyPath);
  33. }
  34. public static List<Transform> GetAllChildren(Transform transform, bool include_self = false)
  35. {
  36. List<Transform> list = new List<Transform>();
  37. if (include_self)
  38. {
  39. list.Add(transform);
  40. }
  41. for (int i = 0; i < transform.childCount; i++)
  42. {
  43. Transform child = transform.GetChild(i);
  44. list.Add(child);
  45. list.AddRange(UnityUtility.GetAllChildren(child, false));
  46. }
  47. return list;
  48. }
  49. public static Transform SearchChild(this Transform transform, Predicate<Transform> match)
  50. {
  51. if (match(transform))
  52. {
  53. return transform;
  54. }
  55. for (int i = 0; i < transform.childCount; i++)
  56. {
  57. Transform transform2 = transform.GetChild(i).SearchChild(match);
  58. if (transform2)
  59. {
  60. return transform2;
  61. }
  62. }
  63. return null;
  64. }
  65. public static Transform SearchChildByName(this Transform transform, string search_name)
  66. {
  67. return transform.SearchChild((Transform ch) => ch.name == search_name);
  68. }
  69. public static string GetObjectTreePath(GameObject obj)
  70. {
  71. string text = string.Empty;
  72. if (obj == null)
  73. {
  74. return text;
  75. }
  76. text += obj.name;
  77. while (obj.transform.parent != null)
  78. {
  79. obj = obj.transform.parent.gameObject;
  80. text = obj.name + "/" + text;
  81. }
  82. return text;
  83. }
  84. public static string GetHierarchyPath(Transform trans, Transform root_obj = null, bool include_rootobj_path = true)
  85. {
  86. if (trans == root_obj)
  87. {
  88. return string.Empty;
  89. }
  90. string text = trans.gameObject.name;
  91. Transform parent = trans.parent;
  92. while (parent != root_obj && parent != null)
  93. {
  94. text = parent.name + "/" + text;
  95. parent = parent.parent;
  96. }
  97. if (include_rootobj_path)
  98. {
  99. text = root_obj.name + "/" + text;
  100. }
  101. return text;
  102. }
  103. public static bool IsChild(Transform parent, Transform child)
  104. {
  105. Func<Transform, Transform, Transform> Search = null;
  106. Search = delegate(Transform checkParent, Transform target)
  107. {
  108. if (checkParent == target)
  109. {
  110. return target;
  111. }
  112. for (int i = 0; i < checkParent.childCount; i++)
  113. {
  114. Transform transform = Search(checkParent.GetChild(i), checkParent);
  115. if (transform != null)
  116. {
  117. return transform;
  118. }
  119. }
  120. return null;
  121. };
  122. return Search(parent, child) == child;
  123. }
  124. public static string SaveImage(Texture f_texSrc, string f_strFileName = null)
  125. {
  126. if (f_texSrc is Texture2D)
  127. {
  128. return UnityUtility.SaveImage(f_texSrc as Texture2D, f_strFileName);
  129. }
  130. if (f_texSrc is RenderTexture)
  131. {
  132. return UnityUtility.SaveImage(f_texSrc as RenderTexture, f_strFileName, TextureFormat.ARGB32);
  133. }
  134. return string.Empty;
  135. }
  136. public static string SaveImage(RenderTexture f_rtSrc, string f_strFileName = null, TextureFormat f_TexFormat = TextureFormat.ARGB32)
  137. {
  138. if (f_rtSrc == null)
  139. {
  140. return string.Empty;
  141. }
  142. string text = f_strFileName;
  143. if (string.IsNullOrEmpty(text))
  144. {
  145. text = "img" + DateTime.Now.ToString("yyyyMMddHHmmss.fff") + ".png";
  146. }
  147. Texture2D texture2D = new Texture2D(f_rtSrc.width, f_rtSrc.height, f_TexFormat, false);
  148. RenderTexture active = RenderTexture.active;
  149. RenderTexture.active = f_rtSrc;
  150. texture2D.ReadPixels(new Rect(0f, 0f, (float)f_rtSrc.width, (float)f_rtSrc.height), 0, 0);
  151. RenderTexture.active = active;
  152. byte[] bytes = texture2D.EncodeToPNG();
  153. File.WriteAllBytes(text, bytes);
  154. UnityEngine.Object.Destroy(texture2D);
  155. return text;
  156. }
  157. public static string SaveImage(Texture2D f_texSrc, string f_strFileName = null)
  158. {
  159. if (f_texSrc == null)
  160. {
  161. return string.Empty;
  162. }
  163. RenderTexture renderTexture = new RenderTexture(f_texSrc.width, f_texSrc.height, 0, RenderTextureFormat.ARGB32);
  164. RenderTexture active = RenderTexture.active;
  165. Graphics.Blit(f_texSrc, renderTexture);
  166. RenderTexture.active = active;
  167. string result = UnityUtility.SaveImage(renderTexture, f_strFileName, TextureFormat.ARGB32);
  168. UnityEngine.Object.Destroy(renderTexture);
  169. return result;
  170. }
  171. public static Sprite CreateSpriteFromImageFile(string pngFilePath)
  172. {
  173. if (string.IsNullOrEmpty(pngFilePath))
  174. {
  175. return null;
  176. }
  177. if (!File.Exists(pngFilePath))
  178. {
  179. return null;
  180. }
  181. Texture2D texture2D = null;
  182. try
  183. {
  184. byte[] array = File.ReadAllBytes(pngFilePath);
  185. if (array == null || array.Length == 0)
  186. {
  187. return null;
  188. }
  189. texture2D = new Texture2D(32, 32);
  190. texture2D.LoadImage(array);
  191. }
  192. catch (Exception ex)
  193. {
  194. Debug.LogError(ex.Message + "\n\n" + ex.StackTrace);
  195. return null;
  196. }
  197. if (texture2D == null || texture2D.width == 0 || texture2D.height == 0)
  198. {
  199. return null;
  200. }
  201. Sprite sprite = Sprite.Create(texture2D, new Rect(0f, 0f, (float)texture2D.width, (float)texture2D.height), default(Vector2));
  202. sprite.name = Path.GetFileName(pngFilePath);
  203. return sprite;
  204. }
  205. public static Texture2D CreateTexture2DFromRenderTexture(RenderTexture render_tex)
  206. {
  207. Texture2D texture2D = new Texture2D(render_tex.width, render_tex.height, TextureFormat.ARGB32, false);
  208. RenderTexture active = RenderTexture.active;
  209. RenderTexture.active = render_tex;
  210. texture2D.ReadPixels(new Rect(0f, 0f, (float)render_tex.width, (float)render_tex.height), 0, 0);
  211. texture2D.Apply();
  212. RenderTexture.active = active;
  213. return texture2D;
  214. }
  215. public static Color SetColorR(Color target, float r)
  216. {
  217. Color result = target;
  218. result.r = r;
  219. return result;
  220. }
  221. public static Color SetColorG(Color target, float g)
  222. {
  223. Color result = target;
  224. result.g = g;
  225. return result;
  226. }
  227. public static Color SetColorB(Color target, float b)
  228. {
  229. Color result = target;
  230. result.b = b;
  231. return result;
  232. }
  233. public static Color SetAlpha(Color target, float alpha)
  234. {
  235. Color result = target;
  236. result.a = alpha;
  237. return result;
  238. }
  239. public static Quaternion InverseTransformRotation(this Transform transform, Quaternion rot)
  240. {
  241. return Quaternion.Inverse(transform.rotation) * rot;
  242. }
  243. public static void RemoveAllComponents<T>(this GameObject gameObject) where T : MonoBehaviour
  244. {
  245. T[] components = gameObject.GetComponents<T>();
  246. if (components != null)
  247. {
  248. foreach (T t in components)
  249. {
  250. UnityEngine.Object.DestroyImmediate(t);
  251. }
  252. }
  253. }
  254. public class PosRotScale
  255. {
  256. public PosRotScale()
  257. {
  258. }
  259. public PosRotScale(UnityUtility.PosRotScale prs)
  260. {
  261. this.position = prs.position;
  262. this.scale = prs.scale;
  263. this.rotation = prs.rotation;
  264. }
  265. public Vector3 position;
  266. public Vector3 scale;
  267. public Quaternion rotation;
  268. }
  269. }
  270. }