标签:Once else 静态 move ace led 单列 ali str
用字典写的池子,还用了伪单利
伪单利要找一个物体把脚本挂起来,get方法要挂在你要点击的butten上,set脚本要挂在需要调用出来的物体上
池子代码(用字典写的):
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 5 public class zidianPool:MonoBehaviour//伪单列 6 { 7 public static zidianPool Instace;//公有静态接口 8 private void Start() 9 { 10 Instace = this;//生命周期内是咧 11 } 12 13 //private static zidianPool instance; 14 //private zidianPool() { } 15 //public static zidianPool Instace 16 //{ 17 // get { 18 // if (instance==null) 19 // { 20 // instance = new zidianPool(); 21 // } 22 // return instance; 23 // } 24 //} 25 26 Dictionary<string,List<GameObject>> dir=new Dictionary<string,List<GameObject>>();//存放不同形状的物体 27 public GameObject ISget(string name) 28 { 29 GameObject go = null;//要拿出来的东西 30 if (dir.ContainsKey(name))//是否包含named的key 31 { 32 List<GameObject> list=dir[name]; 33 if (list.Count>0) 34 { 35 go = list[0]; 36 go.SetActive(true); 37 list.RemoveAt(0); 38 } 39 } 40 if (go==null)//没有名字 或者list为空的时候 41 { 42 go=GameObject.Instantiate(Resources.Load(name)) as GameObject; 43 int index = go.name.IndexOf("(Clone)"); 44 go.name = go.name.Substring(0, index); 45 } 46 return go; 47 } 48 49 public void Isset(GameObject obj) 50 { 51 obj.SetActive(false); 52 if (dir.ContainsKey(obj.name))//字典里是否已经有包含的物体 53 { 54 dir[obj.name].Add(obj); 55 56 } 57 else 58 { 59 List<GameObject> list = new List<GameObject>(); 60 list.Add(obj); 61 dir[obj.name] = list; 62 } 63 64 // obj.transform.parent = GameObject.Find("panel").transform; 65 } 66 }
get方法的脚本,也就是把东西拿出:
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 5 public class getzidian : MonoBehaviour { 6 string[] s = { "Capsule", "Sphere" }; 7 public void get() 8 { 9 GameObject gp= zidianPool.Instace.ISget(s[Random.Range(0,2)]); 10 gp.transform.position = new Vector3(0, 5, 0); 11 } 12 }
set的脚本,也就是存入:
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 5 public class setzidian : MonoBehaviour { 6 7 // Use this for initialization 8 void Start () { 9 10 } 11 12 // Update is called once per frame 13 void Update () { 14 if (transform.position.y<-5) 15 { 16 zidianPool.Instace.Isset(gameObject); 17 } 18 } 19 }
用list写的单列,单利不需要挂脚本
池子的脚本代码:
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 5 public class pool { 6 7 private static pool instance; 8 private pool() { } 9 public static pool Instance 10 { 11 get { 12 if (instance==null) 13 { 14 instance = new pool(); 15 } 16 return instance; 17 } 18 } 19 List<GameObject> listpool = new List<GameObject>(); 20 public GameObject GetCube(string name) 21 { 22 Debug.Log("我在池子里"); 23 GameObject go = null; 24 if (listpool.Count>0) 25 { 26 go = listpool[0]; 27 go.SetActive(true); 28 listpool.RemoveAt(0); 29 } 30 if (go==null) 31 { 32 go = GameObject.Instantiate(Resources.Load(name)) as GameObject; 33 int index = go.name.IndexOf("(Clone)"); 34 go.name = go.name.Substring(0, index); 35 } 36 return go; 37 } 38 public void setpool(GameObject obj) 39 { 40 obj.SetActive(false); 41 listpool.Add(obj); 42 } 43 }
getj脚本,拿出要存入的东西:
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 5 public class get : MonoBehaviour { 6 GameObject obj; 7 // Use this for initialization 8 void Start () { 9 10 } 11 public void GetPool() 12 { 13 obj= pool.Instance.GetCube("Cube"); 14 obj.transform.position = new Vector3(0,5,0); 15 } 16 // Update is called once per frame 17 void Update () { 18 19 } 20 }
set脚本,就是把东西存入里面:
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 5 public class set : MonoBehaviour { 6 7 // Use this for initialization 8 void Start () { 9 10 } 11 12 // Update is called once per frame 13 void Update () { 14 if (gameObject.activeSelf) 15 { 16 if (transform.position.y < -5) 17 { 18 pool.Instance.setpool(gameObject); 19 } 20 } 21 } 22 }
单列(写了池子pool)用list实现的方法, 与伪单例(写了池子zidianpool),用字典实现的方法,可以存入不同,i名字的物体
标签:Once else 静态 move ace led 单列 ali str
原文地址:https://www.cnblogs.com/satanj/p/9789093.html