• C#扩展函数&Unity子对象组件的定向获取


    C#扩展函数

    使用扩展函数,可以为无法修改源代码的对象添加新的方法,或者强制让对象支持某些方法,这些方法看起来就是对象本来就有的功能。

    限制条件

    1. 必须在static class中定义,所以必须使用static修饰;
    2. 需要在(只需要在)第一个参数前面使用this修饰;
    3. 扩展函数可以通过对象实例调用,也可以使用定义该函数的静态类直接调用。

    使用拓展方法的优点

    从简单程度上来说,使用扩展方法明显比使用传统方式要简单很多,不需要实例化,修改方法也不需要重新修改和编译。
    那为什么扩展方法没有得到很大的推广呢?(不足之处)

    1. 是静态方法,使用不当会造成“污染”;
    2. 不具有override的能力,不会重载原有的方法;
    3. 扩展方法会被扩展类的同名方法覆盖,所以实现扩展方法我们需要承担随时被覆盖的风险;
    4. 扩展方法不能访问被扩展类的私有成员;
    5. 扩展方法只能用实例来调用,不能像普通的静态方法一样使用类名调用;
    6. 只有引入扩展方法所在的命名空间后,扩展方法才可以使用。

    扩展方法的本质

    1. 将实例方法调用在编译期改变为静态类中的静态方法调用,实际上,它确实拥有静态方法所有具有的所有功能。
    2. 作用域是整个namespace可见的,并通过使用using namespace来导入其他命名空间中的扩展方法。
    3. 优先级:现有实例方法优先级最高,其次为最近的namespace下的静态类的静态方法,最后为较远的namespace下的静态类的静态方法。
    4. 是一种编译技术,注意与反射等运行时技术进行区别,并慎重使用。

    Unity子对象组件的定向获取

    该功能就是使用扩展函数来实现的,平时在项目中自己使用的,这里做个例子,来表现一下扩展函数的功能。

    using System;
    using UnityEngine;
    
    namespace my_code
    {
        ///  定向子节点组件 
        public static class ChildNodeComponent
        {
            ///  获取子节点组件 
            /// 父节点对象
            /// 组件类型
            /// 子节点名称路径
            /// 子节点组件
            public static Component GetComponentInChildren(this Transform parnet, Type type, string namepath = null)
            {
                Transform temp = parnet;
                if (!string.IsNullOrEmpty(namepath))
                {
                    string[] node = namepath.Split('/');
                    for (int i = 0; i < node.Length; i++)
                    {
                        temp = temp.Find(node[i]);
                    }
                }
                return temp.GetComponent(type);
            }
            ///  获取子节点组件 
            /// 组件类型
            /// 父节点对象
            /// 子节点名称路径
            /// 子节点组件
            public static T GetComponentInChildren<T>(this Transform parnet, string namepath = null) where T : Component
            {
                return GetComponentInChildren(parnet, typeof(T), namepath) as T;
            }
            ///  获取子节点组件 
            /// 父节点对象
            /// 组件类型
            /// 子节点名称路径
            /// 子节点组件
            public static Component GetComponentInChildren(this GameObject parnet, Type type, string namepath = null)
            {
                return GetComponentInChildren(parnet.transform, type, namepath);
            }
            ///  获取子节点组件 
            /// 组件类型
            /// 父节点对象
            /// 子节点名称路径
            /// 子节点组件
            public static T GetComponentInChildren<T>(this GameObject parnet, string namepath = null) where T : Component
            {
                return GetComponentInChildren(parnet, typeof(T), namepath) as T;
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    下面就是具体的使用方法:

    m_slider = statenode.GetComponentInChildren<Slider>("ProgressBarBG");
    m_tooltip = statenode.GetComponentInChildren<Transform>("Tooltip").gameObject;
    m_back = m_stateNode.GetComponentInChildren<Button>("TitleBG/Back");
    m_title = m_stateNode.GetComponentInChildren<Text>("TitleBG/Title");
    
    • 1
    • 2
    • 3
    • 4

    与Unity自带的GetComponentInChildren不一样的就是后面的参数是一个字符串(以各层子节点名称以及分隔符'/'组成)。

    参考链接

    1. https://blog.csdn.net/shuliuzh/article/details/45076677
    2. https://www.cnblogs.com/jiuyueBlog/p/9168323.html
  • 相关阅读:
    在Vue.js中,什么是mixins?它们的作用是什么?
    SUS系统可用性量表
    一面惨败网易经历,奋发图强一个月,终于成功上岸!
    【第0天】SQL快速入门-安装MYSQL环境(SQL 小虚竹)
    win10系统win+r找不到组策略编辑器gpedit.msc
    微信小程序 自定义tab不煽动
    python:多波段遥感影像分离成单波段影像
    【面试题】forEach能跳出循环吗?
    深度学习值过拟合和欠拟合
    分享一个基于SpringBoot+Vue的招生宣传管理系统源码 招生计划管理系统代码
  • 原文地址:https://blog.csdn.net/f_957995490/article/details/127447599