C# LINQ Unity 单例
C# LINQ
1. 自定义 Master,Kongfu 类
1 class Master 2 { 3 4 public int Id { get; set; } 5 public string Name { get; set; } 6 public int Age { get; set; } 7 public string MenPai { get; set; } 8 public string Kongfu { get; set; } 9 public int Level { get; set; } 10 11 public override string ToString() 12 { 13 return string.Format("Id:{0},Name:{1},Age:{2},MenPai:{3},Kongfu:{4},Level:{5}", Id, Name, Age,MenPai,Kongfu,Level); 14 } 15 }
1 class Kongfu 2 { 3 public int Id { get; set; } 4 public string Name { get; set; } 5 public int Power { get; set; } 6 7 public override string ToString() 8 { 9 return string.Format("Id:{0},Name:{1},Power:{2}", Id, Name, Power); 10 } 11 }
2. 测试代码
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 var master = new List<Master>() 6 { 7 new Master() { Id = 100, Age = 100, Kongfu = "九阳神功",Level=10,MenPai="华山",Name="李二狗" }, 8 new Master() { Id = 50, Age = 10, Kongfu = "九阳神功1" ,Level=5,MenPai="华山",Name="李大狗"}, 9 new Master() { Id = 200, Age = 60, Kongfu = "九阳神功2",Level=6,MenPai="华山",Name="李狗蛋" }, 10 }; 11 12 var kongfu = new List<Kongfu>() 13 { 14 new Kongfu(){Id=1,Name="打狗棒法",Power=90}, 15 new Kongfu(){Id=2,Name="打狗棒法1",Power=30}, 16 new Kongfu(){Id=3,Name="打狗棒法2",Power=20}, 17 }; 18 19 //使用LINQ做查询(表达式写法) 20 var res1 = from m in master //from后面设置查询的集合 21 where m.Level > 8 //where跟上条件 22 23 select m;//表示m的结果 24 //select m.Name; //只输出名字 25 26 27 //多个条件 28 var res3 = from m in master 29 30 where m.Level > 8 && m.MenPai == "丐帮" 31 32 select m; 33 34 var res4 = master.Where(m => m.Level > 8 && m.MenPai == "丐帮"); 35 36 //LINQ联合查询 37 var res5 = from m in master 38 from k in kongfu 39 where m.Kongfu==k.Name 40 select new { master = m, kongfu = k }; 41 //select m; 42 foreach (var temp in res) 43 { 44 Console.WriteLine(temp); 45 } 46 47 //join on 联合查询 48 var res12 = from m in master 49 join k in kongfu on m.Kongfu equals k.Name 50 where k.Power>90 51 select new { master = m, kongfu = k }; 52 53 54 //对查询结果升序排序 55 var res7 = from m in master 56 57 where m.Level > 8 && m.MenPai == "丐帮" 58 orderby m.Age 59 60 select m; 61 62 //对查询结果倒序排序 63 var res8 = from m in master 64 65 where m.Level > 8 && m.MenPai == "丐帮" 66 orderby m.Age descending 67 68 select m; 69 70 //对查询结果升序排序 71 var res9 = from m in master 72 73 where m.Level > 8 && m.MenPai == "丐帮" 74 orderby m.Age,m.Level //按照多个字段进行排序,如果字段的属性相同,就按照第二个属性排序 75 76 select m; 77 78 var res10 = master.Where(m => m.Level > 8 && m.MenPai == "丐帮").OrderBy(m => m.Age); 79 var res11 = master.Where(m => m.Level > 8 && m.MenPai == "丐帮").OrderBy(m => m.Age).ThenBy(m=>m.Level); 80 81 82 83 //把人物按照所学功夫进行分类,看下哪个功夫修炼人数最多(分组查询) 84 var res13 = from k in kongfu 85 join m in master on k.Name equals m.Kongfu 86 into groups 87 orderby groups.Count() 88 select new { kongfu = k,count=groups.Count()}; 89 90 //按照字段分组 91 var res14 = from m in master 92 group m by m.MenPai 93 into g 94 select new { count=g.Count(),key=g.Key}; //g.Key按照哪个属性分组 95 96 //量词操作符any all判断集合中是否满足某个条件 97 bool res15=master.Any(m => m.MenPai == "丐帮"); 98 Console.WriteLine(res15);//只要有一个满足返回true 99 100 bool res16=master.All(m => m.MenPai == "丐帮");//所有都满足才返回true 101 102 103 104 Console.ReadKey(); 105 } 106 107 //过滤方法 108 static bool Test1(Master m) 109 { 110 if (m.Level > 8) return true; 111 return false; 112 } 113 }
Unity 单例
1. 继承于MonoBehaviour(不随着场景切换而销毁)
基类代码:
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 5 /// <summary> 6 /// 单例模式基类,继承于 MonoBehaviour,不随场景切换而销毁 7 /// 在场景中新建空物体 DDOL,挂载 T 类继承 DDOLSingleton<T> 即可 8 /// </summary> 9 /// <typeparam name="T"></typeparam> 10 public abstract class DDOLSingleton<T> : MonoBehaviour where T : DDOLSingleton<T> 11 { 12 protected static T _instance = null; 13 14 public static T Instance 15 { 16 get 17 { 18 if (null == _instance) 19 { 20 GameObject go = GameObject.Find("DDOL"); 21 if (null == go) 22 { 23 go = new GameObject("DDOL"); 24 DontDestroyOnLoad(go); 25 } 26 _instance = go.GetComponent<T>(); 27 if (null == _instance) 28 { 29 _instance = go.AddComponent<T>(); 30 } 31 } 32 return _instance; 33 } 34 } 35 }
测试:
新建空实体 DDOL,挂载脚本 GameManage。
新建一个按钮,实现加载下一场景功能,测试是否随场景销毁而销毁。
Hierarchy :
GameManage 代码:
1 using System.Collections.Generic; 2 using UnityEngine; 3 4 /// <summary> 5 /// 单例 6 /// </summary> 7 public class GameManage : DDOLSingleton<GameManage> { 8 public void TestMethod() 9 { 10 Debug.Log("GameManage"); 11 } 12 }
测试代码:
1 /// <summary> 2 /// 测试单例 3 /// </summary> 4 public class Test : MonoBehaviour { 5 6 // Use this for initialization 7 void Start () { 8 GameManage.Instance.TestMethod(); 9 } 10 11 // Update is called once per frame 12 void Update () { 13 14 } 15 16 public void OnBtnClick() 17 { 18 SceneManager.LoadScene(1); 19 } 20 }
效果图:
2. 不继承于MonoBehaviour(随着场景切换而销毁)
基类代码:
1 /// <summary> 2 /// 单例模式基类,不继承于 MonoBehaviour,随场景切换而销毁 3 /// 挂载 T 类继承 DDOLSingleton<T>,重载 Init 函数即可 4 /// </summary> 5 /// <typeparam name="T"></typeparam> 6 public abstract class Singleton<T> where T : class, new() 7 { 8 protected static T _instance = null; 9 10 public static T Instance 11 { 12 get 13 { 14 if (null == _instance) 15 { 16 _instance = new T(); 17 } 18 return _instance; 19 } 20 } 21 22 protected Singleton() 23 { 24 if (null != _instance) 25 { 26 Debug.LogError("This " + typeof(T).ToString() + " Singleton Instance is not null !!!"); 27 } 28 Init(); 29 } 30 31 public virtual void Init() 32 { 33 34 } 35 }
此时 GameManage 代码修改为:
1 /// <summary> 2 /// 测试 Singleton 3 /// </summary> 4 public class GameManage : Singleton<GameManage> { 5 public override void Init() 6 { 7 base.Init(); 8 } 9 10 public void TestMethod() 11 { 12 Debug.Log("GameManage"); 13 } 14 }