版权声明:本文为 ls9512 原创文章,转载请注明出处! https://blog.csdn.net/ls9512/article/details/79705877
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)
{
item.transform.SetParent(obj2.transform);
}
obj2 = item;
}
list[0].transform.SetParent(gameObject.transform);
return list.ToArray();
}
- 获取当前节点的完整路径
public static string Path(this GameObject gameObject)
{
var path = "/" + gameObject.name;
while (gameObject.transform.parent != null)
{
gameObject = gameObject.transform.parent.gameObject;
path = "/" + gameObject.name + path;
}
return path;
}
- 获取根节点
public static GameObject Root(this GameObject go)
{
var current = go;
GameObject result;
do
{
var trans = current.transform.parent;
if (trans != null)
{
result = trans.gameObject;
current = trans.gameObject;
} else
{
result = current;
current = null;
}
} while (current != null);
return result;
}
- 获取节点层级深度
public static int Depth(this GameObject go)
{
var depth = 0;
var current = go.transform;
do
{
current = current.transform.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 in gameObject.transform)
{
SetLayerRecursion(child.gameObject, layer);
}
}
- 按索引递归设置当前和所有子节点的层
public static void SetLyaerRecursion(this GameObject gameObject, int layerIndex)
{
gameObject.layer = layerIndex;
foreach (Transform child in gameObject.transform)
{
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 in gameObject.transform)
{
SetTagRecursion(child.gameObject, tag);
}
}