You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
146 lines
4.3 KiB
146 lines
4.3 KiB
using UnityEngine; |
|
|
|
/// <summary> |
|
/// Transform组件助手类 |
|
/// </summary> |
|
public static class TransformHelper |
|
{ |
|
/// <summary> |
|
/// 面向目标方向 |
|
/// </summary> |
|
/// <param name="targetDirection">目标方向</param> |
|
/// <param name="transform">需要转向的对象</param> |
|
/// <param name="rotationSpeed">转向速度</param> |
|
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); |
|
} |
|
} |
|
|
|
/// <summary> |
|
/// 查找子物体(递归查找) |
|
/// </summary> |
|
/// <param name="trans">父物体</param> |
|
/// <param name="goName">子物体的名称</param> |
|
/// <returns>找到的相应子物体</returns> |
|
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; |
|
} |
|
/// <summary>获取所有的孩子</summary> |
|
/// <param name="parent">父物体位置</param> |
|
/// <returns>所有子物体数组</returns> |
|
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; |
|
} |
|
/// <summary> |
|
/// 删除所有孩子 |
|
/// </summary> |
|
/// <param name="parent">父物体位置</param> |
|
public static void DelChilds(Transform parent) |
|
{ |
|
for (int i = 0; i < parent.childCount; i++) |
|
Object.Destroy(parent.GetChild(i).gameObject); |
|
} |
|
/// <summary> |
|
/// 设置所有孩子Active |
|
/// </summary> |
|
/// <param name="parent">父物体位置</param> |
|
public static void SetActiveChilds(Transform parent, bool isActive) |
|
{ |
|
for (int i = 0; i < parent.childCount; i++) |
|
parent.GetChild(i).gameObject.SetActive(isActive); |
|
} |
|
/// <summary> |
|
/// 查找父对象 |
|
/// </summary> |
|
/// <param name="trans">子物体位置</param> |
|
/// /// <param name="goName">父物体名称</param> |
|
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; |
|
} |
|
/// <summary> |
|
/// 查找父对象 |
|
/// </summary> |
|
/// <param name="trans">子物体位置</param> |
|
/// /// <param name="goName">父物体名称</param> |
|
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; |
|
} |
|
/// <summary> |
|
/// 设置对象包含子对象的Layer |
|
/// </summary> |
|
/// <param name="obj">对象</param> |
|
/// <param name="newLayer">新的Layer</param> |
|
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) |
|
{ |
|
var renderer = obj.GetComponent<Renderer>(); |
|
if (renderer) |
|
{ |
|
renderer.enabled = enable; |
|
} |
|
foreach (Transform child in obj.transform) |
|
{ |
|
SetRenderer(child.gameObject, enable); |
|
} |
|
} |
|
} |
|
|
|
|