Unity对象池的创建与使用
本文提供全流程,中文翻译。
Chinar 坚持将简单的生活方式,带给世人!
(拥有更好的阅读体验 —— 高分辨率用户请根据需求调整网页缩放比例) |
Chinar —— 心分享、心创新!
助力快速完成 Unity 对象池的创建与使用
为新手节省宝贵的时间,避免采坑! |
Chinar 教程效果:
全文高清图片,点击即可放大观看 (很多人竟然不知道)
1
MonoSingleton —— 单例基类
任何继承自 MonoSingleton 泛型基类的脚本/类 都是单例类
![举个栗子黑白88]()
using UnityEngine;
public abstract class MonoSingleton<T> : MonoBehaviour where T : MonoSingleton<T>
{
private static T instance;
public static T Instance
{
get
{
if (instance == null)
{
instance = FindObjectOfType(typeof(T)) as T;
if (instance == null) instance = new GameObject("Chinar Single of " + typeof(T).ToString(), typeof(T)).GetComponent<T>();
}
return instance;
}
}
private void Awake()
{
if (instance == null) instance = this as T;
}
private void OnApplicationQuit()
{
instance = null;
}
}
2
ObjectPool —— 对象池
新建一个脚本 ObjectPool 继承自泛型基类 MonoSingleton《ObjectPool》
是以 ObjectPool 就会是一个单例类
![举个栗子黑白88]()
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectPool : MonoSingleton<ObjectPool>
{
private Dictionary<string, List<GameObject>> cache = new Dictionary<string, List<GameObject>>();
int i = 0;
public GameObject CreateObject(string key, GameObject go, Vector3 position, Quaternion quaternion)
{
GameObject tempgo = cache.ContainsKey(key) ? cache[key].Find(p => !p.activeSelf) : null;
if (tempgo != null)
{
tempgo.transform.position = position;
tempgo.transform.rotation = quaternion;
}
else
{
tempgo = Instantiate(go, position, quaternion);
print("实例化物体数量:" + i++);
if (!cache.ContainsKey(key))
{
cache.Add(key, new List<GameObject>());
}
cache[key].Add(tempgo);
}
tempgo.SetActive(true);
return tempgo;
}
public void CollectObject(GameObject go)
{
go.SetActive(false);
}
public void CollectObject(GameObject go, float delay)
{
StartCoroutine(Collect(go, delay));
}
private IEnumerator Collect(GameObject go, float delay)
{
yield return new WaitForSeconds(delay);
CollectObject(go);
}
public void Clear(string key)
{
if (cache.ContainsKey(key))
{
for (int i = 0; i < cache[key].Count; i++)
{
Destroy(cache[key][i]);
}
cache.Remove(key);
}
}
public void ClearAll()
{
var list = new List<string>(cache.Keys);
for (int i = 0; i < list.Count; i++)
{
Clear(list[i]);
}
}
}
3
PoolTest —— 测试对象池的使用
新建一个脚本 PoolTest 用来测试对象池的使用
![举个栗子黑白88]()
using UnityEngine;
public class PoolTest : MonoBehaviour
{
GameObject go;
GameObject item;
void Start()
{
go = Resources.Load<GameObject>("Cube");
}
void Update()
{
if (Input.GetKey(KeyCode.Space))
{
item = ObjectPool.Instance.CreateObject("Object1", go, Vector3.zero, new Quaternion(0, 0, 0, 0));
item.transform.position += new Vector3(0, 0, 1);
}
if (Input.GetMouseButtonDown(0))
{
if (item != null)
{
ObjectPool.Instance.CollectObject(item, 0.2f);
}
}
if (Input.GetMouseButtonDown(1))
{
ObjectPool.Instance.Clear("Object1");
}
}
}
支持
May Be —— 搞开发,总有一天要做的事!
拥有自己的服务器,无需再找攻略!
Chinar 提供一站式教程,闭眼式创建!
为新手节省宝贵时间,避免采坑! |
先点击领取 —— 阿里全产品优惠券 (享受最低优惠)
1 —— 云服务器超全购买流程 (新手必备!)
2 —— 阿里ECS云服务器自定义配置 - 购买教程(新手必备!)
3—— Windows 服务器配置、运行、建站一条龙 !
4 —— Linux 服务器配置、运行、建站一条龙 !
Chinar
END
本博客为非营利性个人原创,除部分有明确署名的作品外,所刊登的所有作品的著作权均为本人所拥有,本人保留所有法定权利。违者必究
对于需要复制、转载、链接和传播博客文章或内容的,请及时和本博主进行联系,留言,Email: ichinar@icloud.com
对于经本博主明确授权和许可使用文章及内容的,使用时请注明文章或内容出处并注明网址