Skip to content

Commit cb294f5

Browse files
committed
object pooler
1 parent 453c1c8 commit cb294f5

14 files changed

+367
-29
lines changed

Bullet/BulletDestroyScript.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
public class BulletDestroyScript : MonoBehaviour {
6+
private void OnEnable()
7+
{
8+
Invoke("Destroy", 2f);
9+
}
10+
private void Destroy()
11+
{
12+
gameObject.SetActive(false);
13+
14+
}
15+
private void OnDisable()
16+
{
17+
CancelInvoke();
18+
}
19+
}

Bullet/BulletFireScript.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
public class BulletFireScript : MonoBehaviour {
6+
7+
public float fireTime = 0.05f;
8+
9+
// Use this for initialization
10+
void Start () {
11+
12+
InvokeRepeating("Fire", fireTime, fireTime);
13+
}
14+
15+
void Fire()
16+
{
17+
GameObject obj = ObjectPoolerScript.current.GetPooledObject();
18+
if (obj == null)
19+
{
20+
return;
21+
}
22+
obj.transform.position = transform.position;
23+
obj.transform.rotation = transform.rotation;
24+
obj.SetActive(true);
25+
}
26+
27+
}

Bullet/BulletScript.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
public class BulletScript : MonoBehaviour {
6+
public float speed = 10;
7+
8+
// Update is called once per frame
9+
void Update () {
10+
transform.Translate(speed * Time.deltaTime * transform.up);
11+
}
12+
}

Bullet/ObjectPoolerScript.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
public class ObjectPoolerScript : MonoBehaviour {
6+
7+
public static ObjectPoolerScript current;
8+
public GameObject pooledObject;
9+
public int pooledAmount = 20;
10+
//用来判定该池是否能拓展
11+
public bool willGrow = true;
12+
13+
public List<GameObject> pooledObjects;
14+
15+
private void Awake()
16+
{
17+
current = this;
18+
}
19+
// Use this for initialization
20+
void Start () {
21+
pooledObjects = new List<GameObject>();
22+
for (int i = 0; i < pooledAmount; i++)
23+
{
24+
GameObject obj = Instantiate(pooledObject);
25+
obj.SetActive(false);
26+
pooledObjects.Add(obj);
27+
}
28+
}
29+
30+
public GameObject GetPooledObject()
31+
{
32+
for (int i = 0; i < pooledObjects.Count; i++)
33+
{
34+
//不要对list进行remove操作,而用遍历的方法,因为遍历更快
35+
if (!pooledObjects[i].activeInHierarchy)
36+
{
37+
return pooledObjects[i];
38+
}
39+
}
40+
if (willGrow)
41+
{
42+
GameObject obj = Instantiate(pooledObject);
43+
pooledObjects.Add(obj);
44+
return obj;
45+
}
46+
return null;
47+
}
48+
}

Character.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ public virtual void Start() {
1818

1919
public virtual void Update() { }
2020

21-
public virtual void Fire()
22-
{
23-
GameObject missile_clone = Instantiate(bullet, transform.position, Quaternion.Euler(transform.eulerAngles));
24-
missile_clone.SetActive(true);
25-
}
21+
//public virtual void Fire()
22+
//{
23+
// GameObject missile_clone = Instantiate(bullet, transform.position, Quaternion.Euler(transform.eulerAngles));
24+
// missile_clone.SetActive(true);
25+
//}
2626

2727
private void OnTriggerEnter2D(Collider2D collision)
2828
{

EnemyBehavior.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public override void Update () {
5353
if (limited_time_fire < 0)
5454
{
5555
limited_time_fire = firetime_maxvalue;
56-
Fire();
56+
//Fire();
5757
}
5858
}
5959
}

ExploseScript.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
public class ExploseScript : MonoBehaviour {
6+
7+
//public float
8+
public GameObject explosion;
9+
//对对象池的大小进行限定
10+
public int pooledAmount = 5;
11+
List<GameObject> explosions;
12+
// Use this for initialization
13+
void Start () {
14+
explosions = new List<GameObject>();
15+
for (int i = 0; i < pooledAmount; i++)
16+
{
17+
GameObject obj = (GameObject)Instantiate(explosion);
18+
obj.SetActive(false);
19+
explosions.Add(obj);
20+
}
21+
}
22+
23+
// Update is called once per frame
24+
void Update () {
25+
26+
}
27+
}

Explosion.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,22 @@ public class Explosion : MonoBehaviour {
88
void Start () {
99
//这种写法相当于建立了一个可以自销毁的游戏对象,但是这并不是一个好方法
1010
//Destroy(gameObject, live_time);
11-
//Debug.Log("111");
12-
13-
1411
}
12+
private void OnEnable()
13+
{
14+
Invoke("Destroy", 1.5f);
15+
}
1516

16-
//利用时间对延时进行重构,unity中Action对应c#的Event
17-
//public GameObject go;
18-
//public Action OnTimeupEvent;
19-
17+
private void Destroy()
18+
{
19+
gameObject.SetActive(false);
20+
}
2021

22+
private void OnDisable()
23+
{
24+
//使用这个函数的原因在于可以上面的Invoke函数是有延时的
25+
//这种做法可以保证直接结束调用
26+
//而不会使其在被停止使用时被停止使用两次
27+
CancelInvoke();
28+
}
2129
}

PlayerBehavior.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public override void Update () {
5252
}
5353
if (Input.GetKeyDown(KeyCode.Space))
5454
{
55-
Fire();
55+
//Fire();
5656
}
5757
transform.Translate(moveSpeed * new Vector3(h, v, 0) * Time.deltaTime, Space.World);
5858
}

ResourceLoadedManager.cs

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
/// <summary>
5+
/// 资源加载管理器
6+
/// </summary>
7+
public class ResourceLoadedManager : SingletonBehaviour<ResourceLoadedManager>
8+
{
9+
//预制件和精灵有好多种
10+
public Dictionary<string, GameObject> dictLoadedPrefab;
11+
public Dictionary<string, Sprite> dictLoadedImageSprite;
12+
//子弹和爆炸效果有好多个
13+
public List<GameObject> listBullet;
14+
public List<GameObject> listBoomEffect;
15+
16+
//资源加载管理器的私有构造函数
17+
private ResourceLoadedManager()
18+
{
19+
dictLoadedPrefab = new Dictionary<string, GameObject>();
20+
dictLoadedImageSprite = new Dictionary<string, Sprite>();
21+
listBullet = new List<GameObject>();
22+
listBoomEffect = new List<GameObject>();
23+
}
24+
/// <summary>
25+
/// 自定义的资源管理器添加元素函数
26+
/// </summary>
27+
/// <typeparam name="T"></typeparam>
28+
/// <typeparam name="K"></typeparam>
29+
/// <param name="dictionary"></param>
30+
/// <param name="key"></param>
31+
/// <param name="value"></param>
32+
private void Add<T, K>(Dictionary<T, K> dictionary, T key, K value) where K : Object
33+
{
34+
if (dictionary == null)
35+
{
36+
dictionary = new Dictionary<T, K>();
37+
}
38+
if (!dictionary.ContainsKey(key))
39+
{
40+
dictionary.Add(key, value);
41+
}
42+
else
43+
{
44+
dictionary[key] = value;
45+
}
46+
}
47+
//对于各种类型数据缓存的各自添加数据方法
48+
//private void AddToLoadedPrefab(string name, GameObject gameObject)
49+
//{
50+
// Add<string, GameObject>(dictLoadedPrefab, name, gameObject);
51+
//}
52+
//private void AddToLoadedImageSprite(string name, Sprite sprite)
53+
//{
54+
// Add<string, Sprite>(dictLoadedImageSprite, name, sprite);
55+
//}
56+
/// <summary>
57+
/// 获取否则设置resources为加载对象
58+
/// </summary>
59+
/// <typeparam name="T"></typeparam>
60+
/// <param name="pathname"></param>
61+
/// <returns></returns>
62+
public T GetOrSetLoadedObject<T>(string pathname = default(string)) where T : Object
63+
{
64+
if (typeof(T) == typeof(GameObject))
65+
{
66+
if (dictLoadedPrefab == null)
67+
{
68+
dictLoadedPrefab = new Dictionary<string, GameObject>();
69+
}
70+
if (!dictLoadedPrefab.ContainsKey(pathname))
71+
{
72+
GameObject go = Resources.Load<GameObject>(pathname);
73+
Add<string, GameObject>(dictLoadedPrefab, pathname, go);
74+
}
75+
return dictLoadedPrefab[pathname] as T;
76+
}
77+
else if (typeof(T) == typeof(Sprite))
78+
{
79+
if (dictLoadedImageSprite == null)
80+
{
81+
dictLoadedImageSprite = new Dictionary<string, Sprite>();
82+
}
83+
if (!dictLoadedImageSprite.ContainsKey(pathname))
84+
{
85+
Sprite sprite = Resources.Load<Sprite>(pathname);
86+
Add<string, Sprite>(dictLoadedImageSprite, pathname, sprite);
87+
}
88+
return dictLoadedImageSprite[name] as T;
89+
}
90+
return null;
91+
}
92+
93+
public T GetOrCreateInstance<T>(List<T> list, string pathname) where T : Object
94+
{
95+
if (list != null && list.Count > 0)
96+
{
97+
T t = list[0];
98+
list.RemoveAt(0);
99+
return t;
100+
}
101+
else
102+
{
103+
T t = GetOrSetLoadedObject<T>(pathname);
104+
105+
if (typeof(T) == typeof(GameObject))//如果是预制件,直接实例化一个出来加入列表中
106+
{
107+
T t_clone = Instantiate(t);
108+
list.Add(t_clone);//本来没有,是不是老师忘了写
109+
return t_clone;
110+
}
111+
else
112+
{
113+
list.Add(t);//如果是精灵,则加入列表当中
114+
return t;
115+
}
116+
}
117+
}
118+
119+
public void RecoverBullet(GameObject go)
120+
{
121+
listBullet.Add(go);
122+
}
123+
public void RecoverBoomEffect(GameObject go)
124+
{
125+
listBoomEffect.Add(go);
126+
}
127+
128+
//public BaseMissile GetOrCreateBullet(string pathnameBullet)
129+
//{
130+
// return GetOrCreateInstance<GameObject>(listBullet, pathnameBullet).get
131+
//}
132+
}

ResourceManager.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
/// <summary>
5+
/// 用来管理游戏资源的类
6+
/// </summary>
7+
public class ResourceManager : SingletonBehaviour<ResourceManager>
8+
{
9+
//保存默认资源路径
10+
public const string RESOURCE_PREFAB_PATH = "Prefabs/";
11+
12+
//private
13+
14+
private ResourceManager()
15+
{
16+
17+
}
18+
}

Singleton.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.Reflection;
3+
using System.Collections;
4+
using System.Collections.Generic;
5+
using UnityEngine;
6+
7+
/// <summary>
8+
/// 非继承自MonoBehaviour的单例对象继承此类,比如资源管理器等等
9+
/// </summary>
10+
public class Singleton<T> where T : class
11+
{
12+
private static T _instance;
13+
private static readonly object syslock = new object();
14+
//tips:这个是为了作为参数传入函数中,当并不想利用其储存什么数据时,使其长度为0就行。
15+
public static readonly Type[] emptyTypes = new Type[0];
16+
17+
public static T Instance
18+
{
19+
get
20+
{
21+
if (_instance == null)
22+
{
23+
//保证创建的过程是单位操作
24+
lock (syslock)
25+
{
26+
if (_instance == null)
27+
{
28+
//ci的作用是为了确定构造函数是否为private
29+
ConstructorInfo ci = typeof(T).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, emptyTypes, null);
30+
if (ci == null)
31+
{
32+
throw new InvalidOperationException("class must contain a private constructor");
33+
}
34+
//ci为构造函数信息,用ci可以调用构造函数信息
35+
_instance = (T)ci.Invoke(null);
36+
}
37+
}
38+
}
39+
return _instance;
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)