【Unity3D / Extension】 扩展方法02 —— 扩展GameObject

Unity 扩展方法系列

Unity扩展方法01 —— 扩展Transfrom

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);
    }
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ls9512

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值