Unityutils

using UnityEngine;
using System.Collections;

public class UnityUtility
{
protected volatile static UnityUtility _instance;
protected static Object _LockHelper = new Object();
public static UnityUtility getInstance()
{
lock (_LockHelper)
{
if (_instance ==null)
{
_instance = new UnityUtility();
}
return _instance;
}
}
///
/// 克隆GameObject
///
/// 需要克隆的物体
/// 克隆物体的名字
///
public static GameObject CloneGameObject(GameObject oriObj ,string name)
{
GameObject obj = GameObject.Instantiate(oriObj);
obj.name = name;
return obj;
}
///
/// 创建一个GameObject
///
/// 创建GameObject的名字
/// GameObject的父级
///
public static GameObject CreateGameObject(string name ,GameObject parent)
{
GameObject obj = new GameObject(name);
if (parent != null)
{
obj.GetComponent().SetParent(parent.GetComponent());
}
else
{
Debug.LogError(“CreateGameObject parent is null ,that is error!”);
}
return obj;
}
///
///
/// 父级
/// resoruces 下的相对路径
/// 名字
/// 缩放
/// 旋转
/// 位置
///
public static GameObject InstantiatePrefab(GameObject parent ,string prefabPath,string name ,Vector3 scale ,Vector3 rot ,Vector3 pos)
{
GameObject prefab = Resources.Load(prefabPath);
if(prefab == null)
{
return null;
}
GameObject obj = CloneGameObject(prefab ,name);
obj.transform.localScale = scale;
obj.transform.localEulerAngles = rot;
obj.transform.localPosition = pos;
return obj;
}
//
///
/// 父级
/// resoruces 下的相对路径
/// 名字
public static GameObject InstantiatePrefab(GameObject parent, string prefabPath,string name)
{
GameObject prefab = Resources.Load(prefabPath);
if (prefab == null)
{
return null;
}
GameObject obj = CloneGameObject(prefab, name);
obj.transform.localScale = Vector3.one;
obj.transform.localEulerAngles = Vector3.one;
obj.transform.localPosition = Vector3.one;
return obj;
}
///
/// 设置GameObject的层级
///
///
///
public static void SetGameObjectLayer(GameObject obj ,string layerName)
{
int layer = LayerMask.NameToLayer(layerName);
if(layer < 0 || layer >= 32)
{
return;
}
SetGameObjectLayer(obj,layer);
}
///
/// 设置GameObject的层级
///
///
///
public static void SetGameObjectLayer(GameObject obj ,int layer)
{
obj.layer = layer;
Transform[] childeTransformList = obj.transform.GetComponentsInChildren(true);
foreach (Transform t in childeTransformList)
{
t.gameObject.layer = layer;
}
}
///
/// 查找GameObject
///
/// 父级
/// 名字
///
public GameObject GetGameObject(GameObject parent, string name)
{
GameObject go = null;
if (parent == null)
{
GameObject.Find(name);
}
else
{
Transform trans = parent.transform.Find(name);
if (trans != null)
{
go = trans.gameObject;
}
// 如果父节点的第一级子节点中找不到,就递归查找
else
{
int childCount = parent.transform.childCount;
for (int i = 0; i < childCount; ++i)
{
go = GetGameObject(parent.transform.GetChild(i).gameObject, name);
if (go != null)
{
break;
}
}
}
}
return go;
}
///
/// 查找物体的子类
///
///
///
public static Transform[] FindChildGameObj(GameObject obj)
{
return obj.transform.GetComponentsInChildren(true);
}
public static Transform GetTransfrom(Transform trans ,string name)
{
foreach (var item in trans.GetComponentsInChildren(true))
{
if(trans.name == name)
{
return item;
}
}
return null;
}
public static float GetDistance(Vector3 start ,Vector3 end)
{
float distance = Mathf.Abs(Vector3.Distance(start, end));
return distance;
}
public static bool isEqualDistanceIn(Vector3 start ,Vector3 end ,float distance)
{
return Mathf.Abs(Vector3.Distance(start, end)) <= distance;
}
public static bool HasContainsSpecialCharacter(char hchar)
{
if ((hchar >= ‘0’ && hchar <= ‘9’) || (hchar >= ‘a’ &&
hchar <= ‘z’) || (hchar >= ‘A’ && hchar < ‘Z’))
{
return true;
}
return false;
}
public static void RightReplaceLeft(ref string value)
{
value = value.Replace(’\’, ‘/’);
}
public static void LeftReplaceRight(ref string value)
{
value = value.Replace(’/’,’\’);
}
public static Transform[] GetTransformChildren(GameObject gameobj,bool mySelf)
{
Transform[] childTrans = gameobj.GetComponentsInChildren(true);
Transform[] childTransNoSelf = new Transform[gameobj.transform.childCount];
if (!mySelf)
{
int i = 0;
foreach (var item in childTrans)
{
if (item == gameobj.transform)
{
continue;
}
childTransNoSelf[i] = item;
i++;
}
return childTransNoSelf;
}
else
{
return childTrans;
}
}
}

你可能感兴趣的:(c#)