using Newtonsoft.Json; using System.IO; using UnityEngine; /// /// Transform组件助手类 /// public static class TransformHelper { /// /// 面向目标方向 /// /// 目标方向 /// 需要转向的对象 /// 转向速度 public static void LookAtTarget(Vector3 targetDirection, Transform transform, float rotationSpeed) { if (targetDirection != Vector3.zero) { var targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up); transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, rotationSpeed); } } public static string LoadJsonFromConfing(string pathname) { string json = null; string path = Path.Combine(Application.streamingAssetsPath, pathname); if (File.Exists(path)) { json = File.ReadAllText(path); } return JsonConvert.DeserializeObject(json); } /// /// 查找子物体(递归查找) /// /// 父物体 /// 子物体的名称 /// 找到的相应子物体 public static Transform FindChild(Transform trans, string goName) { if (goName == trans.name) return trans; Transform child = trans.Find(goName); if (child != null) return child; Transform go = null; for (int i = 0; i < trans.childCount; i++) { child = trans.GetChild(i); go = FindChild(child, goName); if (go != null) return go; } return null; } /// 获取所有的孩子 /// 父物体位置 /// 所有子物体数组 public static Transform[] GetChilds(Transform parent) { Transform[] childs = new Transform[parent.childCount]; for (int i = 0; i < childs.Length; i++) childs[i] = parent.GetChild(i); return childs; } /// /// 删除所有孩子 /// /// 父物体位置 public static void DelChilds(Transform parent) { if (parent.childCount > 0) for (int i = 0; i < parent.childCount; i++) { if (parent.GetChild(i)) Object.Destroy(parent.GetChild(i).gameObject); } } /// /// 设置所有孩子Active /// /// 父物体位置 public static void SetActiveChilds(Transform parent, bool isActive) { for (int i = 0; i < parent.childCount; i++) parent.GetChild(i).gameObject.SetActive(isActive); } /// /// 查找父对象 /// /// 子物体位置 /// /// 父物体名称 public static Transform FindParentByName(Transform trans, string goName) { Transform pTrans = null; if (trans.parent) { Transform pobj = trans.parent; if (pobj.name == goName) { pTrans = pobj; } else { pTrans = FindParentByName(pobj, goName); } } return pTrans; } /// /// 查找父对象 /// /// 子物体位置 /// /// 父物体名称 public static Transform FindParentByTag(Transform trans, string tagName) { Transform pTrans = null; if (trans.parent) { Transform pobj = trans.parent; if (pobj.tag == tagName) { pTrans = pobj; } else { pTrans = FindParentByTag(pobj, tagName); } } return pTrans; } /// /// 设置对象包含子对象的Layer /// /// 对象 /// 新的Layer public static void SetLayerRecursively(GameObject obj, int newLayer) { obj.layer = newLayer; foreach (Transform child in obj.transform) { SetLayerRecursively(child.gameObject, newLayer); } } public static void SetRenderer(GameObject obj, bool enable) { if (obj.gameObject.GetComponentInParent()) { return; } var renderer = obj.GetComponent(); if (renderer) { renderer.enabled = enable; } foreach (Transform child in obj.transform) { SetRenderer(child.gameObject, enable); } } /// /// 获取父对象绑定的某个脚本,直到获取到或者父对象为空 /// /// /// /// public static Transform GetParentByComponent(Transform trans, string component) { Transform pTrans = null; if (trans.parent) { Transform pobj = trans.parent; if (pobj.GetComponent(component)) { pTrans = pobj; } else { pTrans = GetParentByComponent(pobj, component); } } return pTrans; } public static Quaternion ConvertToQuaternion(MyRotation MyRotation) { return new Quaternion(MyRotation.x, MyRotation.y, MyRotation.z, MyRotation.w); } public static Vector3 ConvertToVector3(MyPosition MyPosition) { return new Vector3(MyPosition.x, MyPosition.y, MyPosition.z); } public static Vector3 ConvertToScale(MyScale MyScale) { return new Vector3(MyScale.x, MyScale.y, MyScale.z); } public static MyRotation ConvertToMyRotation(Quaternion rotation) { MyRotation MyRotation = new MyRotation { x = rotation.x, y = rotation.y, z = rotation.z, w = rotation.w }; return MyRotation; } public static MyPosition ConvertToMyPosition(Vector3 position) { MyPosition MyPosition = new MyPosition { x = position.x, y = position.y, z = position.z, }; return MyPosition; } public static MyScale ConvertToMyScale(MyScale scale) { MyScale MyScale = new MyScale { x = scale.x, y = scale.y, z = scale.z, }; return MyScale; } }