基于Unity3D的对象池的实现

对象池在游戏优化中起着很重要的作用,今天我们来实现一个简单的对象池功能。

1.打开Unity,创建3个C#脚本,分别为GameController.cs(游戏控制器),ObjectPool.cs(对象池),CubeMgr.cs(延迟销毁物体),代码分别如下:

GameController.cs用于游戏的控制器,点击鼠标左键,生成Cube。

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameController : MonoBehaviour {
	public static GameController _instance;
	//cube预设体
	public GameObject cubePrefab;
	//对象池
	public ObjectPool op;
	void Awake(){
		_instance = this;
	}	
	// Use this for initialization
	void Start () {
		op = new ObjectPool (cubePrefab, 3);
	}
	// Update is called once per frame
	void Update () {
		if (Input.GetMouseButtonDown(0)) {
			GameObject go = op.GetInstance();
			StartCoroutine( go.GetComponent<CubeMgr> ().DelayTime ());
		}
	}
}
 

 ObjectPool.cs用于管理生成的所有的游戏对象。

using UnityEngine;  
using System.Collections.Generic;  
  
public class ObjectPool{  
    //要生成的对象池预设  
    private GameObject prefab;  
    //对象池列表  
    private List<GameObject> pool;  
    //构造函数  
    public ObjectPool(GameObject prefab, int initialSize) {  
        this.prefab = prefab;  
        this.pool = new List<GameObject>();  
        for (int i = 0; i < initialSize; i++) {  
            AlllocateInstance();  
        }  
    }  
    /// <summary>  
    /// 获取实例  
    /// </summary>  
    public GameObject GetInstance() {  
        if (pool.Count == 0) {  
            AlllocateInstance();  
        }  
        GameObject instance = pool[0];  
        pool.RemoveAt(0);  
        instance.SetActive(true);  
        return InitInstance (instance);  
    }  
    /// <summary>  
    /// 初始化实例  
    /// </summary>  
    protected virtual GameObject InitInstance(GameObject instance){  
        instance.transform.position = new Vector3 (Random.Range (-5f, 5f), Random.Range (-5f, 5f), Random.Range (-5f, 5f));  
        Debug.Log ("初始化实例");  
        return instance;  
    }  
    //protected virtual void   
    /// <summary>  
    /// 释放实例  
    /// </summary>  
    public void ReleaseInstance(GameObject instance) {  
        instance.SetActive(false);  
        pool.Add(RevokeInstance (instance));  
    }  
    /// <summary>  
    /// 撤销实例  
    /// </summary>  
    protected virtual GameObject RevokeInstance(GameObject instance){  
        Debug.Log ("撤销实例");  
        return instance;  
    }  
    /// <summary>  
    /// 生成本地实例  
    /// </summary>  
    private GameObject AlllocateInstance() {  
        GameObject instance = (GameObject) GameObject.Instantiate(prefab);  
        instance.SetActive(false);  
        pool.Add(instance);  
        return GenerateInstance (instance);  
    }  
    /// <summary>  
    /// 创建实例  
    /// </summary>  
    protected GameObject GenerateInstance(GameObject instance){  
        Debug.Log ("创建实例");  
        return instance;  
    }  
}

CubeMgr.cs用于物体的延迟销毁。

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CubeMgr : MonoBehaviour {
    public IEnumerator DelayTime()
    {
        yield return new WaitForSeconds(3f);
        GameController._instance.op.ReleaseInstance(this.gameObject);
    }
}

2.如图创建游戏对象:

3.运行游戏,点击鼠标左键,即可看到有物体生成。大笑大笑大笑如图:

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值