123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- using UnityEngine;
- using System.Collections;
- using CommonAIClient.Unity.Battle;
- using System.ComponentModel;
- namespace CommonAIClient.Unity.Utils
- {
- public static class Extensions
- {
- public static GameObject FindChild(this GameObject go, string name)
- {
- if (go.name == name)
- return go;
- for (int i = 0; i < go.transform.childCount; i++)
- {
- GameObject tmp = go.transform.GetChild(i).gameObject;
- tmp = FindChild(tmp, name);
- if (tmp != null)
- {
- return tmp;
- }
- }
- return null;
- }
- public static Vector3 Position(this GameObject go)
- {
- return go.transform.position;
- }
- public static Quaternion Rotation(this GameObject go)
- {
- return go.transform.rotation;
- }
- public static Vector3 Forward(this GameObject go)
- {
- return go.transform.forward;
- }
- public static void Position(this GameObject go, Vector3 position)
- {
- go.transform.position = position;
- }
- public static void Rotation(this GameObject go, Quaternion rotation)
- {
- go.transform.rotation = rotation;
- }
- public static void Forward(this GameObject go, Vector3 forward)
- {
- if (forward != Vector3.zero)
- go.transform.forward = forward;
- }
- public static void Parent(this GameObject go, GameObject parent)
- {
- go.transform.parent = parent != null ? parent.transform : null;
- }
- public static void ParentRoot(this GameObject go, GameObject parent)
- {
- go.transform.parent = parent.transform;
- go.transform.localPosition = Vector3.zero;
- go.transform.localScale = Vector3.one;
- go.transform.localRotation = Quaternion.identity;
- }
- public static void LookAt(this GameObject go, GameObject target)
- {
- go.transform.LookAt(target.transform);
- }
- public static void LookAt(this GameObject go, Transform target)
- {
- go.transform.LookAt(target);
- }
- public static void LookAt(this GameObject go, Vector3 worldPosition, [DefaultValue("Vector3.up")]Vector3 worldUp)
- {
- go.transform.LookAt(worldPosition, worldUp);
- }
- public static void LookAt(this Transform go, GameObject target)
- {
- go.LookAt(target.transform);
- }
- public static void ZonePos2UnityPos(this GameObject go, float totalHeight
- , float x, float y, float z = 0)
- {
- go.Position(ZonePos2UnityPos(totalHeight, x, y, z));
- }
- public static void ZonePos2NavPos(this GameObject go, float totalHeight
- , float x, float y, float z = 0)
- {
- go.Position(ZonePos2NavPos(totalHeight, x, y, z));
- }
- public static void ZoneRot2UnityRot(this GameObject go, float direct)
- {
- go.Rotation(ZoneRot2UnityRot(direct));
- }
-
- public static bool NearlyZero(float v)
- {
- return System.Math.Abs(v) < 0.0001f;
- }
- public static bool NavRayHit(Vector3 pos, out Vector3 hit)
- {
- hit = pos;
- RaycastHit rayHit;
- if (Physics.Raycast(pos + Vector3.up * 100, Vector3.down, out rayHit
- , Mathf.Infinity, 1 << BattleFactroy.Instance.StageNavLay))
- {
- hit.y = rayHit.point.y;
- return true;
- }
- return false;
- }
-
- public static Vector3 ZonePos2UnityPos(float totalHeight, float x, float y, float z = 0)
- {
- Vector3 tmp = Vector3.zero;
- tmp.x = x;
- tmp.y = z;
- tmp.z = totalHeight - y;
- return tmp;
- }
-
- public static Vector3 ZonePos2NavPos(float totalHeight, float x, float y, float z = 0)
- {
- Vector3 tmp = ZonePos2UnityPos(totalHeight, x, y, z);
- NavRayHit(tmp, out tmp);
- return tmp;
- }
- public static Quaternion ZoneRot2UnityRot(float direct)
- {
- return Quaternion.AngleAxis(direct * Mathf.Rad2Deg + 90, Vector3.up);
- }
- }
- }
|