Extensions.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using UnityEngine;
  2. using System.Collections;
  3. using CommonAIClient.Unity.Battle;
  4. using System.ComponentModel;
  5. namespace CommonAIClient.Unity.Utils
  6. {
  7. public static class Extensions
  8. {
  9. public static GameObject FindChild(this GameObject go, string name)
  10. {
  11. if (go.name == name)
  12. return go;
  13. for (int i = 0; i < go.transform.childCount; i++)
  14. {
  15. GameObject tmp = go.transform.GetChild(i).gameObject;
  16. tmp = FindChild(tmp, name);
  17. if (tmp != null)
  18. {
  19. return tmp;
  20. }
  21. }
  22. return null;
  23. }
  24. public static Vector3 Position(this GameObject go)
  25. {
  26. return go.transform.position;
  27. }
  28. public static Quaternion Rotation(this GameObject go)
  29. {
  30. return go.transform.rotation;
  31. }
  32. public static Vector3 Forward(this GameObject go)
  33. {
  34. return go.transform.forward;
  35. }
  36. public static void Position(this GameObject go, Vector3 position)
  37. {
  38. go.transform.position = position;
  39. }
  40. public static void Rotation(this GameObject go, Quaternion rotation)
  41. {
  42. go.transform.rotation = rotation;
  43. }
  44. public static void Forward(this GameObject go, Vector3 forward)
  45. {
  46. if (forward != Vector3.zero)
  47. go.transform.forward = forward;
  48. }
  49. public static void Parent(this GameObject go, GameObject parent)
  50. {
  51. go.transform.parent = parent != null ? parent.transform : null;
  52. }
  53. public static void ParentRoot(this GameObject go, GameObject parent)
  54. {
  55. go.transform.parent = parent.transform;
  56. go.transform.localPosition = Vector3.zero;
  57. go.transform.localScale = Vector3.one;
  58. go.transform.localRotation = Quaternion.identity;
  59. }
  60. public static void LookAt(this GameObject go, GameObject target)
  61. {
  62. go.transform.LookAt(target.transform);
  63. }
  64. public static void LookAt(this GameObject go, Transform target)
  65. {
  66. go.transform.LookAt(target);
  67. }
  68. public static void LookAt(this GameObject go, Vector3 worldPosition, [DefaultValue("Vector3.up")]Vector3 worldUp)
  69. {
  70. go.transform.LookAt(worldPosition, worldUp);
  71. }
  72. public static void LookAt(this Transform go, GameObject target)
  73. {
  74. go.LookAt(target.transform);
  75. }
  76. public static void ZonePos2UnityPos(this GameObject go, float totalHeight
  77. , float x, float y, float z = 0)
  78. {
  79. go.Position(ZonePos2UnityPos(totalHeight, x, y, z));
  80. }
  81. public static void ZonePos2NavPos(this GameObject go, float totalHeight
  82. , float x, float y, float z = 0)
  83. {
  84. go.Position(ZonePos2NavPos(totalHeight, x, y, z));
  85. }
  86. public static void ZoneRot2UnityRot(this GameObject go, float direct)
  87. {
  88. go.Rotation(ZoneRot2UnityRot(direct));
  89. }
  90. public static bool NearlyZero(float v)
  91. {
  92. return System.Math.Abs(v) < 0.0001f;
  93. }
  94. public static bool NavRayHit(Vector3 pos, out Vector3 hit)
  95. {
  96. hit = pos;
  97. RaycastHit rayHit;
  98. if (Physics.Raycast(pos + Vector3.up * 100, Vector3.down, out rayHit
  99. , Mathf.Infinity, 1 << BattleFactroy.Instance.StageNavLay))
  100. {
  101. hit.y = rayHit.point.y;
  102. return true;
  103. }
  104. return false;
  105. }
  106. //找个地方保存地图信息 优化掉参数1
  107. public static Vector3 ZonePos2UnityPos(float totalHeight, float x, float y, float z = 0)
  108. {
  109. Vector3 tmp = Vector3.zero;
  110. tmp.x = x;
  111. tmp.y = z;
  112. tmp.z = totalHeight - y;
  113. return tmp;
  114. }
  115. //找个地方保存地图信息 优化掉参数1
  116. public static Vector3 ZonePos2NavPos(float totalHeight, float x, float y, float z = 0)
  117. {
  118. Vector3 tmp = ZonePos2UnityPos(totalHeight, x, y, z);
  119. NavRayHit(tmp, out tmp);
  120. return tmp;
  121. }
  122. public static Quaternion ZoneRot2UnityRot(float direct)
  123. {
  124. return Quaternion.AngleAxis(direct * Mathf.Rad2Deg + 90, Vector3.up);
  125. }
  126. }
  127. }