上海杨浦大连路地铁站单机版电子沙盘
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.
 
 
 
 

232 lines
6.8 KiB

using Newtonsoft.Json;
using System.IO;
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);
}
}
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<string>(json);
}
/// <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)
{
if (parent.childCount > 0)
for (int i = 0; i < parent.childCount; i++)
{
if (parent.GetChild(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)
{
if (obj.gameObject.GetComponentInParent<NotHide>())
{
return;
}
var renderer = obj.GetComponent<Renderer>();
if (renderer)
{
renderer.enabled = enable;
}
foreach (Transform child in obj.transform)
{
SetRenderer(child.gameObject, enable);
}
}
/// <summary>
/// 获取父对象绑定的某个脚本,直到获取到或者父对象为空
/// </summary>
/// <param name="trans"></param>
/// <param name="component"></param>
/// <returns></returns>
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;
}
}