UnityEngineUtils.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. using LuaInterface;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using UnityEngine.UI;
  6. namespace UnityEngine
  7. {
  8. public static class UnityEngineUtils
  9. {
  10. #region Instantiate
  11. public static T Instantiate<T>(this T original) where T : Object
  12. {
  13. return Object.Instantiate(original) as T;
  14. }
  15. public static GameObject Root(this GameObject gameObject)
  16. {
  17. return gameObject.transform.root.gameObject;
  18. }
  19. public static void SetSafeActive(this GameObject gameObject, bool bValue)
  20. {
  21. if (gameObject == null)
  22. {
  23. return;
  24. }
  25. gameObject.SetActive(bValue);
  26. }
  27. public static void SetSafeActive(this Transform transform, bool bValue)
  28. {
  29. if (transform == null)
  30. {
  31. return;
  32. }
  33. transform.gameObject.SetActive(bValue);
  34. }
  35. public static void SetVisible(this MonoBehaviour component, bool bValue)
  36. {
  37. if (component == null)
  38. {
  39. return;
  40. }
  41. component.gameObject.SetActive(bValue);
  42. }
  43. public static bool IsVisible(this MonoBehaviour component)
  44. {
  45. return !(component == null) && component.gameObject.activeSelf;
  46. }
  47. public static void SetSafeEnable(this MonoBehaviour component, bool bValue)
  48. {
  49. if (component == null)
  50. {
  51. return;
  52. }
  53. component.enabled = bValue;
  54. }
  55. public static void Destroy(this GameObject go)
  56. {
  57. if (go == null) return;
  58. ((Object)go).Destroy();
  59. }
  60. public static void DestroyComponent(this GameObject go, Type comName)
  61. {
  62. if (go == null) return;
  63. var com = go.GetComponent(comName.ToString());
  64. if (com == null) return;
  65. GameObject.DestroyImmediate(com);
  66. }
  67. #endregion
  68. #region
  69. public static GameObject getGameObject(this Transform tr, string name)
  70. {
  71. if (tr == null)
  72. {
  73. DebugHelper.LogError("No such Transform ");
  74. return null;
  75. }
  76. Transform trs = tr.Find(name);
  77. if (trs)
  78. {
  79. return trs.gameObject;
  80. }
  81. else
  82. {
  83. DebugHelper.LogError("No such item {0}" , name);
  84. return null;
  85. }
  86. }
  87. public static void RemoveAllChild(this Transform transform)
  88. {
  89. if (transform == null)
  90. {
  91. return;
  92. }
  93. int i = 0;
  94. int childCount = transform.childCount;
  95. while (i < childCount)
  96. {
  97. Transform child = transform.GetChild(i);
  98. child.gameObject.SetActive(false);
  99. GameObject.Destroy(child.gameObject);
  100. i++;
  101. }
  102. }
  103. [NoToLua]
  104. public static T GetComponentUpwards<T>(this GameObject _go) where T : Component
  105. {
  106. Transform parent = _go.transform.parent;
  107. while (parent != null)
  108. {
  109. T t = parent.GetComponent(typeof(T)) as T;
  110. if (t != null)
  111. {
  112. return t;
  113. }
  114. parent = parent.parent;
  115. }
  116. return (T)((object)null);
  117. }
  118. [NoToLua]
  119. public static T GetComponentInChildrenFast<T>(this GameObject go) where T : Component
  120. {
  121. Component component = go.GetComponent(typeof(T));
  122. if (component != null)
  123. {
  124. return component as T;
  125. }
  126. Transform transform = go.transform;
  127. if (transform != null)
  128. {
  129. int childCount = transform.childCount;
  130. for (int i = 0; i < childCount; i++)
  131. {
  132. Transform child = transform.GetChild(i);
  133. T componentInChildrenFast = child.gameObject.GetComponentInChildrenFast<T>();
  134. if (componentInChildrenFast != null)
  135. {
  136. return componentInChildrenFast;
  137. }
  138. }
  139. }
  140. return (T)((object)null);
  141. }
  142. [NoToLua]
  143. public static T GetComponentInChildrenFast<T>(this Component component) where T : Component
  144. {
  145. return component.gameObject.GetComponentInChildrenFast<T>();
  146. }
  147. [NoToLua]
  148. public static T GetComponent<T>(this GameObject go, bool create) where T : Component
  149. {
  150. T t = go.GetComponent(typeof(T)) as T;
  151. if (create && t == null)
  152. {
  153. t = (go.AddComponent(typeof(T)) as T);
  154. }
  155. return t;
  156. }
  157. public static Component GetOrAddComponent(this GameObject go, Type type)
  158. {
  159. Component comp = null;
  160. if (type != null)
  161. {
  162. comp = go.GetComponent(type);
  163. if (!comp)
  164. comp = go.AddComponent(type);
  165. }
  166. return comp;
  167. }
  168. /// <summary>
  169. /// Gets or add a component. Usage example:
  170. /// BoxCollider boxCollider = transform.GetOrAddComponent<BoxCollider>();
  171. /// </summary>
  172. [NoToLua]
  173. public static T GetOrAddComponent<T>(this Component child) where T : Component
  174. {
  175. T result = child.GetComponent<T>();
  176. if (result == null)
  177. {
  178. result = child.gameObject.AddComponent<T>();
  179. }
  180. return result;
  181. }
  182. [NoToLua]
  183. public static T GetComponent<T>(this Component component, bool create) where T : Component
  184. {
  185. T t = component.GetComponent(typeof(T)) as T;
  186. if (create && t == null)
  187. {
  188. t = (component.gameObject.AddComponent(typeof(T)) as T);
  189. }
  190. return t;
  191. }
  192. #endregion
  193. public static void SetLayer(GameObject go, string layerName)
  194. {
  195. if (go == null) return;
  196. LayerMask mask = LayerMask.NameToLayer(layerName);
  197. SetLayer(go.transform, mask);
  198. }
  199. private static void SetLayer(Transform tr, LayerMask mask)
  200. {
  201. if (tr == null)
  202. return;
  203. for (int i = 0; i < tr.childCount; ++i)
  204. {
  205. SetLayer(tr.GetChild(i), mask);
  206. }
  207. tr.gameObject.layer = mask;
  208. }
  209. public static Material GetMaterial(this Renderer render)
  210. {
  211. #if UNITY_EDITOR
  212. return render.material;
  213. #else
  214. return render.sharedMaterial;
  215. #endif
  216. }
  217. public static void SetMaterialBlack(this GameObject go, bool black = false)
  218. {
  219. if (go == null) return;
  220. Renderer[] renders = go.GetComponentsInChildren<Renderer>();
  221. if (renders != null)
  222. {
  223. string shaderName = "";
  224. for (int i = 0; i < renders.Length; ++i)
  225. {
  226. Material mMaterial;
  227. #if UNITY_EDITOR
  228. mMaterial = renders[i].material;
  229. #else
  230. mMaterial = renders[i].sharedMaterial;
  231. #endif
  232. if (mMaterial != null)
  233. {
  234. shaderName = mMaterial.shader.name;
  235. if (black)
  236. {
  237. if(shaderName.Contains("Particles"))
  238. {
  239. continue;
  240. }
  241. mMaterial.shader = Shader.Find("Diffuse");
  242. mMaterial.SetVector("_Color", new Vector4(0,0,0,1));
  243. //mMaterial.EnableKeyword("BLACKMODE");
  244. }
  245. else
  246. {
  247. if (shaderName.Contains("Diffuse"))
  248. {
  249. mMaterial.shader = Shader.Find("Mx_JS/HeroShow_RimTail");
  250. }
  251. }
  252. }
  253. shaderName = null;
  254. }
  255. }
  256. }
  257. public static void SetMaterialGray(this GameObject go, bool gray = false)
  258. {
  259. Renderer[] renders = go.GetComponentsInChildren<SkinnedMeshRenderer>();
  260. if (renders != null)
  261. {
  262. for (int i = 0; i < renders.Length; ++i)
  263. {
  264. Material mMaterial = renders[i].material;
  265. if (mMaterial != null && mMaterial.HasProperty("_Strength"))
  266. {
  267. if (gray)
  268. {
  269. mMaterial.SetFloat("_Strength", 0.314f);
  270. }
  271. else
  272. {
  273. mMaterial.SetFloat("_Strength", 1);
  274. }
  275. }
  276. }
  277. }
  278. }
  279. public static Color SetAlpha(this Color clr,float alpha)
  280. {
  281. clr.a = alpha;
  282. return clr;
  283. }
  284. public static void PauseEffectParticle(GameObject effectGameObject)
  285. {
  286. if (effectGameObject == null)
  287. {
  288. return;
  289. }
  290. ParticleSystem[] particals = effectGameObject.GetComponentsInChildren<ParticleSystem>(true);
  291. foreach (ParticleSystem p in particals)
  292. {
  293. p.Pause(false);
  294. }
  295. }
  296. public static void ResumeEffectParticle(GameObject effectGameObject)
  297. {
  298. if (effectGameObject == null)
  299. {
  300. return;
  301. }
  302. ParticleSystem[] particals = effectGameObject.GetComponentsInChildren<ParticleSystem>(true);
  303. foreach (ParticleSystem p in particals)
  304. {
  305. p.Play(false);
  306. }
  307. }
  308. public static void SetUnscalTimePartical(GameObject effectGameObject)
  309. {
  310. if (effectGameObject != null && effectGameObject.GetComponent<UnscaleTimeEffect>() == null)
  311. {
  312. effectGameObject.AddComponent<UnscaleTimeEffect>();
  313. }
  314. }
  315. public static void DestroyImmediate(this Object obj)
  316. {
  317. GameObject.DestroyImmediate(obj);
  318. }
  319. public static Transform GetTargetInChildens<T>(string targets, Transform transform) where T : Component
  320. {
  321. if (string.IsNullOrEmpty(targets))
  322. {
  323. return transform;
  324. }
  325. Transform[] componentsInChildren = transform.GetComponentsInChildren<Transform>(true);
  326. for (int i = 0; i < componentsInChildren.Length; i++)
  327. {
  328. if (componentsInChildren[i].name.Equals(targets))
  329. {
  330. return componentsInChildren[i];
  331. }
  332. }
  333. return transform;
  334. }
  335. /// <summary>
  336. /// 获取top物体path路径下物体的T组件
  337. /// </summary>
  338. /// <typeparam name="T"></typeparam>
  339. /// <param name="top"></param>
  340. /// <param name="path"></param>
  341. /// <param name="create"></param>
  342. /// <returns></returns>
  343. [NoToLua]
  344. public static T GetComponentByPath<T>(this GameObject top, string path, bool create = false) where T : Component
  345. {
  346. Transform t = top != null ? (string.IsNullOrEmpty(path) ? top.transform : top.transform.Find(path)) : null;
  347. if (t != null)
  348. {
  349. T c = t.GetComponent<T>();
  350. if (c == null && create)
  351. c = t.gameObject.AddComponent<T>();
  352. return c;
  353. }
  354. return null;
  355. }
  356. public static void Destroy(this Object obj)
  357. {
  358. GameObject.Destroy(obj);
  359. }
  360. public static string GetParentPath(this Transform tr, Transform root)
  361. {
  362. string transPath = string.Empty;
  363. if (tr != null && root != null && root != tr)
  364. {
  365. List<string> path = new List<string>();
  366. path.Add(tr.name);
  367. GetParentPathEX(tr.parent, ref path, root);
  368. if (path.Count > 0)
  369. {
  370. StringBuilder sb = new StringBuilder(1024);
  371. for (int i = path.Count - 1; i >= 0; --i)
  372. {
  373. sb.Append(path[i]);
  374. if (i != 0)
  375. sb.Append('/');
  376. }
  377. transPath = sb.ToString();
  378. }
  379. }
  380. else if (tr != null)
  381. {
  382. transPath = tr.name;
  383. }
  384. return transPath;
  385. }
  386. public static List<string> GetParentPathEX(Transform tr, ref List<string> path, Transform root)
  387. {
  388. if (tr != null && root != tr)
  389. {
  390. path.Add(tr.name);
  391. return GetParentPathEX(tr.parent, ref path, root);
  392. }
  393. return path;
  394. }
  395. public static void SetParentNormalize(this GameObject go, GameObject parent)
  396. {
  397. if (go == null || parent == null)
  398. {
  399. return;
  400. }
  401. go.transform.SetParentNormalize(parent.transform);
  402. }
  403. public static void SetParentNormalize(this GameObject go, Transform parent)
  404. {
  405. if (go == null)
  406. {
  407. return;
  408. }
  409. go.transform.SetParentNormalize(parent);
  410. }
  411. public static void SetParentNormalize(this Transform tr, Transform parent)
  412. {
  413. if (tr == null)
  414. {
  415. return;
  416. }
  417. tr.SetParent(parent);
  418. tr.ResetTransform();
  419. }
  420. public static void ResetTransform(this GameObject go)
  421. {
  422. if (go != null)
  423. go.transform.ResetTransform();
  424. }
  425. public static void ResetTransform(this Transform tr)
  426. {
  427. if (tr != null)
  428. {
  429. tr.localPosition = Vector3.zero;
  430. tr.localEulerAngles = Vector3.zero;
  431. tr.localScale = Vector3.one;
  432. }
  433. }
  434. public static Transform RecurisiveFindTransformChild(Transform t, string name)
  435. {
  436. if (t == null)
  437. return null;
  438. if (string.IsNullOrEmpty(name) || name.CompareTo(t.name) == 0)
  439. return t;
  440. for (int i = 0; i < t.childCount; i++)
  441. {
  442. Transform childT = RecurisiveFindTransformChild(t.GetChild(i), name);
  443. if (childT != null)
  444. return childT;
  445. }
  446. return null;
  447. }
  448. public static void DisplayMobileKeyboard(this InputField input, bool display)
  449. {
  450. input.keyboardType = display ? TouchScreenKeyboardType.Default :(TouchScreenKeyboardType)(-1);
  451. input.ActivateInputField();
  452. }
  453. public static bool CheckBoundIsInCamera(this Bounds bound, Camera camera)
  454. {
  455. System.Func<Vector4, int> ComputeOutCode = (projectionPos) =>
  456. {
  457. int _code = 0;
  458. if (projectionPos.x < -projectionPos.w) _code |= 1;
  459. if (projectionPos.x > projectionPos.w) _code |= 2;
  460. if (projectionPos.y < -projectionPos.w) _code |= 4;
  461. if (projectionPos.y > projectionPos.w) _code |= 8;
  462. if (projectionPos.z < -projectionPos.w) _code |= 16;
  463. if (projectionPos.z > projectionPos.w) _code |= 32;
  464. return _code;
  465. };
  466. Vector4 worldPos = Vector4.one;
  467. int code = 63;
  468. for (int i = -1; i <= 1; i += 2)
  469. {
  470. for (int j = -1; j <= 1; j += 2)
  471. {
  472. for (int k = -1; k <= 1; k += 2)
  473. {
  474. worldPos.x = bound.center.x + i * bound.extents.x;
  475. worldPos.y = bound.center.y + j * bound.extents.y;
  476. worldPos.z = bound.center.z + k * bound.extents.z;
  477. code &= ComputeOutCode(camera.projectionMatrix * camera.worldToCameraMatrix * worldPos);
  478. }
  479. }
  480. }
  481. return code == 0 ? true : false;
  482. }
  483. }
  484. }