• unity transform 常用操作


    1.寻找物体

    1.1 寻找满足条件的子物体

    `    public static Transform FindObj(Transform transform, Func<Transform, bool> condition, bool isGrandsonObj = false)
    {
        Transform[] allChilds = transform.GetComponentsInRealChildren<Transform>(isGrandsonObj);
    
        foreach (Transform child in allChilds)
        {
            if (condition(child))
            {
                return child;
            }
        }
    
        return transform;
    }
    
    public static Transform FindObj(Transform transform, string tag, bool isGrandsonObj = false)
    {
        return FindObj(transform, (t) => t.tag.Equals(tag), isGrandsonObj);
    }`
    

    1.2 寻找一组满足条件的子物体

    `    public static List<Transform> FindObjs(Transform transform, Func<Transform, bool> condition, bool isGrandsonObj = false)
    {
        Transform[] allChilds = transform.GetComponentsInRealChildren<Transform>(isGrandsonObj);
        List<Transform> list = new List<Transform>();
    
        foreach (Transform child in allChilds)
        {
            if (condition(child))
            {
                list.Add(child);
            }
        }
        return list;
    }
    
    public static List<Transform> FindObjs(Transform transform, string tag, bool isGrandsonObj = false)
    {
        return FindObjs(transform, (t) => t.tag.Equals(tag), isGrandsonObj);
    }`
    

    2. 操作一组物体

    2.1 对一组满足条件物体进行操作

    `    public static void AddActionObjects(Transform[] transform, Func<Transform, bool> condition, Action<Transform> action)
    {
        foreach (Transform child in transform)
        {
            if (condition(child))
            {
                action(child);
            }
        }
    }`
    

    2.2 常用扩展方法

    `    public static void AddActionObjects(Transform[] transform, Action<Transform> action)
    {
        AddActionObjects(transform, (t) => true, action);
    }
    
    public static void AddActionObjects(Transform transform, Func<Transform, bool> condition, Action<Transform> action, bool isGrandsonObj = false)
    {
        Transform[] allChilds = transform.GetComponentsInChildren<Transform>(isGrandsonObj);
        AddActionObjects(allChilds, condition, action);
    }
    
    public static void AddActionObjects(Transform transform, Action<Transform> action, bool isGrandsonObj = false)
    {
        AddActionObjects(transform, (t) => true, action, isGrandsonObj);
    }
    
    public static void AddActionObjects(Transform transform, string tag, Action<Transform> action, bool isGrandsonObj = false)
    {
        AddActionObjects(transform, (t) => t.tag.Equals(tag), action, isGrandsonObj);
    }
    

    `

  • 相关阅读:
    基于Memcached的tomcat集群session共享所用的jar及多个tomcat各种序列化策略配置
    response.getWriter().write()和 response.getWriter().print()的区别
    response.getWriter().write()与out.print()的区别
    跳转到页面后加载一个请求的方法
    【遍历集合】Java遍历List,Map,Vector,Set的几种方法
    Java中通过方法创建一个http连接并请求(服务器间进行通信)
    【tomcat】手动部署动态JavaWeb项目到tomcat
    【Eclipse】Eclipse中修改项目的映射名称与端口
    使用Cookie进行会话管理
    操作系统内核框架图整理
  • 原文地址:https://www.cnblogs.com/programmingAdorableNew/p/10348234.html
Copyright © 2020-2023  润新知