C#泛型在unity3D中的运用...

 

泛型是什么?

 

这是摘自百度百科中对泛型的介绍:

 

泛型是c#2.0的一个新增加的特性,它为使用c#语言编写面向对象程序增加了极大的效力和灵活性。不会强行对值类型进行装箱和拆箱,或对引用类型进行向下强制类型转换,所以性能得到提高。通过知道使用泛型定义的变量的类型限制,编译器可以在一个高得多的程度上验证类型假设,所以泛型提高了程序的类型安全。它允许程序员将一个实际的数据类型的规约延迟至泛型的实例被创建时才确定。泛型为开发者提供了一种高性能的编程方式,能够提高代码的重用性,并允许开发者编写非常优雅的解决方案。

 

看过之后,会用的还是懂;不会用的,也还是不懂。

 

所以我们还是看看泛型具体在程序中的应用:

 

//泛型方法
//unity3D中用的最多的泛型方法应该是GetCompent<T>
//T就是占位符,当调用这个方法的你要告诉编译器,这个T的具体类型。
//另外,"T"只是一个标识,完全可以用其他代替,如"K","MyType"等,只是使用习惯。
//比如:
GameObject player;
Animator animator;
void Start()
    {
//在场景中找到名为MyPlayer物体
player = GameObject.Find("MyPlayer");
//获取该物体上的Animator组件
animator = player.GetComponent<Animator>();
//对于使用AddComponent<T>()、GetCompent<T>()这两个泛型方法来说,需要了解的就是:T就是你想要的具体的组件类型。
//对于泛型方法来说,泛型的作用就是占位和约束的作用。
    }       
//下面来说声明泛型函数
    /// <summary>
    /// 比较等级;
    /// </summary>
    /// <returns>
    /// 若t1>=t2的等级,则返回true;否则返回false
    /// </returns>
    /// where T : IRole where K : IRole的作用是约束传入的两个参数类型必须要实现IRole这个接口;
    /// 这样就定义好了一个泛型方法
    public bool CompareLevel<T,K>(T t1,    K t2) where T : IRole where K : IRole
    {
        //因为泛型t1,t2都被约束需要实现接口,所以我们可以强制转换到IRole来获取level比较
        return ((IRole)t1).level >= ((IRole)t2).level;
    }
    //那么怎么使用呢?
    //接下来看:
    public void Test()
    {
        //先定义三个测试用的类型
        MyNPC npc =new MyNPC();
        MyPlayer player =new MyPlayer();
        MyMonster monster =new MyMonster();
        //对各个类型的level赋值
        npc.level =1;
        player.level =2;
        monster.level =3;
        //比较npc和player的level就很简单了,只需要这样调用即可
        bool b1 = CompareLevel<MyNPC,MyPlayer>(npc,player); //npc?payer//false                    
        bool b2 = CompareLevel<MyNPC,MyMonster>(npc,monster);//npc?monster//false
        bool b3 = CompareLevel<MyPlayer,MyMonster>(player,monster);//payer?monster//false
    }
    
    public interface IRole 
    {
        int level{get;set;}
    }
    public class MyPlayer:IRole
    {
        public int level{get;set;}
    }
    public class MyNPC:IRole
    {
        public int level{get;set;}
    }
    public class MyMonster:IRole
    {
        public int level{get;set;}
    }

 

这只介绍了泛型方法的使用。还有泛型类,泛型接口等,在此就不多说了。

 

用C#开发unity3D游戏用的最多的泛型应该还是泛型集合List<T>之类的,这些就靠平时慢慢熟悉了。

 

 使用泛型最大的好处就是代码复用,比如写一段程序要统计"水"的一些属性(信息),如这是什么样的水(矿泉水/自来水/污水.etc),或者水的温度是多少.可以这样写(使用结构体):

  1. public class NewBehaviourScript : MonoBehaviour  
  2. {  
  3.     // Use this for initialization  
  4.     void Start()  
  5.     {  
  6.         Water test = new Water();  
  7.         test.name = "KuangQuanShui";  
  8.         test.temperature = 100;  
  9.         Debug.Log("水的名字: " + test.name + "   水的温度: " + test.temperature);  
  10.     }  
  11. }  
  12. public struct Water  
  13. {  
  14.     public string name;//水的名字  
  15.     public int temperature;//水的温度  
  16. }  
复制代码



下面是使用泛型来重写上面的这段代码,如下:

  1. public class fanxing : MonoBehaviour  
  2. {  
  3.     // Use this for initialization  
  4.     void Start()  
  5.     {  
  6.         Water<string> test1 = new Water<string>();  
  7.         test1.info = "KuangQuanShui";//在此传入水的名字是"矿泉水"(可以将"T"看作是string类型)  
  8.   
  9.         Water<int> test2 = new Water<int>();  
  10.         test2.info = 100;//在此传入水的温度是 100(可以将"T"看作是int类型)  
  11.         Debug.Log("水的名字: " + test1.info + "   水的温度: " + test2.info);  
  12.     }  
  13. }  
  14. public class Water<T>  
  15. {  
  16.     public T info;//水的信息(属性)  
  17. }  
复制代码


上面的"T"可以看作是任意数据类型,(对于为何不使用基类型object及涉及的装箱/拆箱,网上介绍的很详细了,在这就不过多叙述了)通过上面的两段代码可能还看不出泛型有多大的好处,但是想想如果是使用泛型来初始化单例,是不是方便很多.

对于泛型的约束:如果我只允许"T"接收某一个数据类型(比如自定义了一个构造函数,只接收这个类型)如下:

  1. public class fanxing : MonoBehaviour  
  2. {  
  3.     // Use this for initialization  
  4.     void Start()  
  5.     {  
  6.         WaterBase waterInfo = new WaterBase();  
  7.         Water<WaterBase> test3 = new Water<WaterBase>();//Water<限定只能为WaterBase类型>  可以将"T"看作是WaterBase类型  
  8.         test3.info = waterInfo;  
  9.         test3.info.name = "KuangQuanShui";  
  10.         test3.info.temperature = 100;  
  11.         Debug.Log("水的名字是: " + test3.info.name + "   水的温度是: " + test3.info.temperature);  
  12.     }  
  13. }  
  14. public class Water<T> where T : WaterBase  
  15. {  
  16.     public T info;//水的信息(属性) 可看作"T"的类型是WaterBase  
  17. }  
  18. public class WaterBase  
  19. {  
  20.     public string name;  
  21.     public int temperature;  
  22. }  
复制代码

 

泛型的约束,官网规定如下:

 

20130610180310984.png

 

如果是多个占位符的泛型(两个),示例代码如下:

  1. public class fanxing : MonoBehaviour  
  2. {  
  3.     // Use this for initialization  
  4.     void Start()  
  5.     {  
  6.         //从A产线出来的生产日期是string类型的"20130610",水是矿泉水,温度是20,  
  7.         Water<string, WaterBase> test1 = new Water<string, WaterBase>();//Water<任意类型,直接收WaterBase类型> 在此"T"相当于string类型  
  8.         test1.data = "20130610";  
  9.         test1.info.name = "KuangQuanShui";  
  10.         test1.info.temperature = 20;  
  11.         //从B产线出来的生产日期是int类型的20130610,水是纯净水,温度是20,  
  12.         Water<int, WaterBase> test2 = new Water<int, WaterBase>();//Water<任意类型,直接收WaterBase类型> 在此"T"相当于int类型  
  13.         test2.data = 20130610;  
  14.         test2.info.name = "ChunJingShui";  
  15.         test2.info.temperature = 20;  
  16.     }  
  17. }  
  18. public class Water<T, U> where U : WaterBase //限定"U"只能接收WaterBase类型  
  19. {  
  20.     public T data;//出厂日期(可接受int型的20130610,或者string类型的"20130610");  
  21.     public U info;//水的具体信息(矿泉水/纯净水...温度)  
  22. }  
  23. public class WaterBase  
  24. {  
  25.     public string name;  
  26.     public int temperature;  
  27. }  
复制代码

 

接下来是泛型的继承,下面分别是一个占位符/多个占位符的继承示例,以及泛型继承的写法:

  1. public class fanxing : MonoBehaviour  
  2. {  
  3.     // Use this for initialization  
  4.     void Start()  
  5.     {  
  6.         TestChild1 test = new TestChild1();  
  7.         test.data = "KuangQuanShui";  
  8.         Testchild2 test2 = new Testchild2();  
  9.         test2.data = 100;  
  10.   
  11.         Son1 son1 = new Son1();  
  12.         son1.data1 = "KuangQuanShui";  
  13.         son1.data2 = 100;  
  14.     }  
  15. }  
  16. #region 一个占位符  
  17. public class Test<T>  
  18. {  
  19.     public T data;  
  20. }  
  21. public class TestChild1 : Test<string> { }  
  22. //或者: public class TestChild1<T>: Test<string> { }  
  23. //或者: public class TestChild1<T>: Test<T> { }  
  24. public class Testchild2 : Test<int> { }  
  25. #endregion  
  26. #region 两个占位符  
  27. public class Fater<T, U>  
  28. {  
  29.     public T data1;  
  30.     public U data2;  
  31. }  
  32. public class Son1 : Fater<string, int> { }  
  33. //或者: public class Son1<T,U> : Fater<string, int> { }  
  34. //或者: public class Son1<T,U> : Fater<T, U> { }  
复制代码

 

下面说一下泛型方法,感觉和泛型差不多(也是允许传入类型任意)就不过多叙述了,示例如下:

  1. public class fanxing : MonoBehaviour  
  2. {  
  3.     // Use this for initialization  
  4.     void Start()  
  5.     {  
  6.         Test test = new Test();  
  7.         Debug.Log(test.Fanxing<string>("KuangQuanShui"));  
  8.         Debug.Log(test.Fanxing<int>(100));  
  9.     }  
  10. }  
  11. public class Test  
  12. {  
  13.     public T Fanxing<T>(T t)  
  14.     {  
  15.         return t;  
  16.     }  
  17. }  
复制代码
posted @ 2014-01-07 15:52  若愚Shawn  阅读(2864)  评论(0编辑  收藏  举报