【Unity3DExtension】扩展⽅法02——扩展GameObject
Unity 扩展⽅法系列
GameObject的常⽤扩展
GameObject的很多接⼝与Transform是重合的,本⽂略去了⼀些与Transform完全相同的扩展,如有需要可以参考系列博⽂第⼀篇。本⽂中有些接⼝也许作为Transform的扩展会更合适⼀些,看个⼈喜好和实际需要。也可以把同⼀接⼝在GameObejct和Transform中各实现⼀遍。
获取组件,不存在则添加,省去⼀次if判断
public static T GetOrAddComponent<T>(this GameObject gameObject) where T : Component
{
return gameObject.GetComponent<T>() ?? gameObject.AddComponent<T>();
}
是存在Rigidbody组件
public static bool HasRigidbody(this GameObject gameObject)
{
return gameObject.GetComponent<Rigidbody>() != null;
}
可以类推写出其他接⼝,例如HasAnimation、HasAnimator等,但需要注意效率问题,避免频繁调⽤。
⼦节点相关扩展
搜索指定名字和类型的节点
public static T SearchComponent<T>(this GameObject gameObject, string searchName) where T : Component
{
var gos = gameObject.GetComponentsInChildren<T>(true);
var length = gos.Length;
for (var i = 0; i < length ; i++)
{
var local = gos[i];
if (searchName == local.name)
{
return local;
}
}
return null;
}
创建指定路径的⼦节点
public static GameObject[] CreateChild(this GameObject gameObject, string pathName) {
GameObject obj2 = null;
var list = new List<GameObject>();
var separator = new char[] { '/' };
for (var i = 0; i < pathName.Split(separator).Length; i++)
{
var str = pathName.Split(separator)[i];
var item = new GameObject(str);
list.Add(item);
if (obj2 != null)
{
}
obj2 = item;
}
list[0].transform.ansform);
return list.ToArray();
}
获取当前节点的完整路径
public static string Path(this GameObject gameObject)
{
var path = "/" + gameObject.name;
while (ansform.parent != null)
{
gameObject = ansform.parent.gameObject;
path = "/" + gameObject.name + path;
}
return path;
}
获取根节点
public static GameObject Root(this GameObject go)
{
var current = go;
GameObject result;
do
{
var trans = ansform.parent;
if (trans != null)
{
result = trans.gameObject;
current = trans.gameObject;
} else
{
result = current;
current = null;
}
} while (current != null);
return result;
}
unity3d animation获取节点层级深度
public static int Depth(this GameObject go)
{
var depth = 0;
var current = go.transform;
do
{
current = ansform.parent;
if (current != null)
{
depth++;
}
} while (current != null);
return depth;
}
Layer 层相关扩展
设置当前节点的层
public static void SetLayer(this GameObject gameObject, LayerMask layer)
{
gameObject.layer = layer.GetLayerIndex();
}
递归设置当前和所有⼦节点的层
public static void SetLayerRecursion(this GameObject gameObject, LayerMask layer) {
gameObject.layer = layer.GetLayerIndex();
foreach (Transform child ansform)
{
SetLayerRecursion(child.gameObject, layer);
}
}
按索引递归设置当前和所有⼦节点的层
public static void SetLyaerRecursion(this GameObject gameObject, int layerIndex)
{
gameObject.layer = layerIndex;
foreach (Transform child ansform)
{
SetLyaerRecursion(child.gameObject, layerIndex);
}
}
Tag 标签相关扩展
设置标签
public static void SetTag(this GameObject gameObject, string tag)
{
gameObject.tag = tag;
}
递归设置标签
public static void SetTagRecursion(this GameObject gameObject, string tag) {
gameObject.tag = tag;
foreach (Transform child ansform)
{
SetTagRecursion(child.gameObject, tag);
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论