单列,单列不断生成球,点击butten的时候

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _01单利
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             Bag bag = new Bag();
14             bag.Isbeg();
15         }
16     }
17    public  class Player
18     {  //单利:是设计模式中的一种,作用:保证我们一个类每次只生成一个实例(创建对象只能进行一次实例化),给我们提供了方便,方便调用该类的方法
19        //1.在该类内部创建一个实例(默认构造私有)
20        //2.提供一个静态接口(属性或者方法)根据这个借口调用里面的方法,字段,属性
21        //3.该类作为静态实例实现
22        //4.调用方便,节约内存
23 
24         private static  Player st;//静态是列子
25         public   int ph = 0;
26        public  int maxPh = 100;
27         private Player() { }//私有的构造函数
28         public static Player Instance//静态接口
29         {
30             get
31             {
32                 st = new Player();//创建一个
33                 return st;
34             }
35         }
36     }
37     public class Bag
38     {
39         public void Isbeg()
40         {
41             //模拟背包里的血量 ,加血的时候不能超过最大值
42             Player p=  Player.Instance; ;//创建了一个对象,如果多次调用instance就会出现多个对象的初始调用
43             p.ph += 101;
44             int num = p.ph;
45             num = num > Player.Instance.maxPh ? Player.Instance.maxPh : num;
46             Console.WriteLine( num);
47         }
48     }
49 }

单列不断生成球,点击butten的时候

1池子的编写:

 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 
 5 public class SpherePool
 6 {
 7     private static SpherePool sp;//私有类为静态
 8     private SpherePool() { }//把构造设为私有
 9     public static SpherePool Instance//静态方法
10     {
11         get
12         {
13             if (sp == null)
14             {
15                 sp = new SpherePool();
16 
17             }
18             return sp;
19         }
20     }
21     List<GameObject> listSphere = new List<GameObject>();//把球存进list里面
22     public GameObject GetSphere(string name)//一个GameObject的属性的方法,返回名字
23     {
24         GameObject obj = null;//开始的物体是空的
25         if (listSphere.Count > 0)//判断list里面是否有值
26         {
27             obj = listSphere[0];//从第一个开始拿
28             obj.SetActive(true);//如果有就让它显现
29             listSphere.RemoveAt(0);//把拿出的删除掉
30         }
31         if (obj == null)//没有这个物体,接下来就创造
32         {
33             //Instantiate(Resources.Load(name))需要有名字一模一样的文件夹,预制体放在里面
34             obj = GameObject.Instantiate(Resources.Load(name)) as GameObject; //用物体接受创造 的,再强转
35             //因为找的是名字,所以把生成的后缀去掉
36             int index = obj.name.IndexOf("(Clone)");
37             obj.name = obj.name.Substring(0, index);
38         }
39         return obj;
40     }
41 
42     public void SaveSphere(GameObject sphere)
43     {
44         sphere.SetActive(false);
45         listSphere.Add(sphere);
46     }
47 
48 }

2.点击butten的时候:

 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 
 5 public class getPool : MonoBehaviour {
 6    
 7     // Use this for initialization
 8     void Start () {
 9         
10     }
11     
12     public void IsGetPool()
13     {
14         GameObject go= SpherePool.Instance.GetSphere("Sphere");
15         go.transform.position = new Vector3(0, 4, 0);
16     }
17     // Update is called once per frame
18     void Update () {
19         
20     }
21 }

3.在自己的物体上挂脚本,让它到一定点失活,然后传参数:

 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 
 5 public class SetPool : 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<-7)
17             {
18                 SpherePool.Instance.SaveSphere(gameObject);//给getPool类里的SaveSphere方法,传入物体
19             }
20         }
21     }
22 }

 

posted @ 2018-09-12 20:37  白纸菇凉  阅读(175)  评论(0编辑  收藏  举报