Unity添加自定义拓展方法

本文转自:

原文:http://unitypatterns.com/extension-methods/

译文:http://blog.csdn.net/shepherdog/article/details/22944147

有个人部分修改,请点击连接查看原文,尊重楼主版权。


Unity添加自定义拓展方法


通常你会发现你不能修改正在使用的那些类,无论它是基础的数据类型还是已有框架的一部分,它提供的方法让你困苦不堪。不过。。C# 提供了一种巧妙的方式来让你扩充已有的类,也就是我们今天要讲的扩展方法。

扩展方法由于很方便而被经常使用到,我们更愿意叫他语法糖豆(syntactic sugar),一个实用样例是Unity的Transform类,

比如:你只想设置Transform.position中Vector3的x轴,而其它两个轴保持不变。

using UnityEngine;  
using System.Collections;  
   
public class Player : MonoBehaviour   
{  
    void Update ()   
    {  
        //Set new x position to 5  
        transform.position = new Vector3(5f, transform.position.y, transform.position.z);  
    }  
}  
在这个样例中,如果你只设置Transform.position的x轴变量,编译会报错,所以你不得不把其余的y轴和z轴加上组成一个完整的Vector3。于是一个拓展方法比如SetPositionX() 就可以添加到Transform类中,从而让代码的可读性更好。


为了创建拓展方法,你需要创建一个静态类。此外,一个拓展方法的声明前缀也必须加上静态修饰符static,同时该方法的第一个参数为所操作的类型,且必须以this关键字修饰

using UnityEngine;  
using System.Collections;  
   
//Must be in a static class   
public static class Extensions  
{  
    //Function must be static  
    //First parameter has "this" in front of type  
    public static void SetPositionX(this Transform t, float newX)  
    {  
        t.position = new Vector3(newX, t.position.y, t.position.z);  
    }  
}  
现在你可以在其它脚本里用这个新的拓展方法替换掉旧的代码。

using UnityEngine;  
using System.Collections;  
   
public class Player : MonoBehaviour   
{  
    void Update ()   
    {  
        //Set new x position to 5  
        transform.SetPositionX(5f);  
    }  
}  
提示:拓展方法只能在实例中被调用,而不能在类本身内部使用。

这里有一些本人常用拓展方法来帮你更好理解拓展方法,以作参考:

using UnityEngine;

public static class MyExtensions
{
    public static T GetOrCreate<T>(this GameObject go) where T : Component
    {
        T t = go.GetComponent<T>();
        if (t)
        {
            return t;
        }

        return go.AddComponent<T>();
    }

    public static GameObject GetParent(this GameObject go)
    {
        return go.transform.parent ? go.transform.parent.gameObject : null;
    }

    public static void AttachChild(this GameObject go, GameObject child)
    {
        child.transform.parent = go.transform;
        child.transform.localPosition = Vector3.zero;
        child.transform.localRotation = Quaternion.identity;
     //   child.transform.localScale = Vector3.one;
    }

    public static void DetachChild(this GameObject go, GameObject child)
    {
        if (child.GetParent() == go)
        {
            child.transform.parent = null;
        }
    }

    public static GameObject FindChild(this GameObject go, string name)
    {
        Transform t = GetChild(go, name);
        return t == null ? null : t.gameObject;
    }

    public static Transform GetChild(this GameObject go, string name)
    {
        Transform child = go.transform.FindChild(name);
        if (child != null)
        {
            return child;
        }

        return GetChildRecurse(go.transform, name);
    }

    public static Transform GetChild(this Component comp, string name)
    {
        Transform child = comp.transform.FindChild(name);
        if (child != null)
        {
            return child;
        }

        return GetChildRecurse(comp.transform, name);
    }

    public static Transform GetChildRecurse(Transform trans, string name)
    {
        if (trans.name == name)
        {
            return trans;
        }

        foreach (Transform child in trans.transform)
        {
            Transform t = GetChildRecurse(child, name);
            if (t != null)
            {
                return t;
            }
        }
        return null;
    }

    static public void SetLayer(this Transform t, int layer)
    {
        t.gameObject.layer = layer;
        for (int i = 0; i < t.childCount; ++i)
        {
            Transform child = t.GetChild(i);
            child.gameObject.layer = layer;
            SetLayer(child, layer);
        }
    }

    static public void SetLayer(this Transform t, int layer, int exceptLayerMask)
    {
        int oLayerMask = 1 << t.gameObject.layer;
        if ((oLayerMask & exceptLayerMask) == 0)        
            t.gameObject.layer = layer;
        for (int i = 0; i < t.childCount; ++i)
        {
            Transform child = t.GetChild(i);            
            SetLayer(child, layer, exceptLayerMask);
        }
    }

    public static Color WithA(this Color color, float alpha)
    {
        return new Color(color.r, color.g, color.b, alpha);
    }

    public static Vector3 GetVector3(this Color color)
    {
        return new Vector3(color.r, color.g, color.b);
    }

    public static bool StringEndsWith(string a, string b)
    {
        int ap = a.Length - 1;
        int bp = b.Length - 1;

        while (ap >= 0 && bp >= 0 && a[ap] == b[bp])
        {
            ap--;
            bp--;
        }

        return (bp < 0 && a.Length >= b.Length);
    }

    public static bool StringStartsWith(string a, string b)
    {
        int aLen = a.Length;
        int bLen = b.Length;
        int ap = 0; int bp = 0;

        while (ap < aLen && bp < bLen && a[ap] == b[bp])
        {
            ap++;
            bp++;
        }
        return (bp == bLen && aLen >= bLen);
    }
}

注意:

1.静态类不能拓展MonoBehaviour类;

2.如果你是在某个命名空间内部定义的拓展方法,那么在调用时,你必须添加using指令以包含这个命名空间。。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值