简单对象池

来源:互联网 发布:wix链接国内域名 编辑:程序博客网 时间:2024/06/01 09:23
    ///// <summary>    ///// 放在对象池里面的对象必须实现的接口    ///// </summary>    //public interface IPoolObject    //{    //    /// <summary>    //    /// Object初始化时调用    //    /// </summary>    //    void Init();    //    /// <summary>    //    /// Object回收时调用    //    /// </summary>    //    void Release();    //}    //using UnityEngine;    //using System.Collections;    //public class BasePoolObject : MonoBehaviour, IPoolObject    //{    //    public void Init()    //    {    //        Debug.Log("初始化对象,比如位置");    //    }    //    public void Release()    //    {    //        Debug.Log("释放对象");    //    }    //}    //using UnityEngine;    //using System.Collections;    //using System.Collections.Generic;    //public class ObjectPool : MonoBehaviour    //{    //    /// <summary>    //    /// 预生成的个数    //    /// </summary>    //    public int preNum;    //    /// <summary>    //    /// 对象列表    //    /// </summary>    //    public Queue<GameObject> pool = new Queue<GameObject>();    //    /// <summary>    //    ///要放入的对象的预置    //    /// </summary>    //    public GameObject poolObjectPefab;    //    void InitPool()    //    {    //        for (int i = 0; i < preNum; i++)    //        {    //            GameObject obj = GameObject.Instantiate(poolObjectPefab);    //            IPoolObject comp = obj.GetComponent<IPoolObject>();    //            if (comp == null)    //            {    //                Debug.LogError("预置上必须绑有实现了IPoolObject接口的类");    //            }    //            comp.Init();    //            pool.Enqueue(obj);    //            obj.SetActive(false);    //        }    //    }    //    /// <summary>    //    /// 创建一个对象    //    /// </summary>    //    /// <returns>从池里取出对象.</returns>    //    public GameObject RequestObject()    //    {    //        GameObject obj;    //        if (pool.Count != 0)    //        {    //            obj = pool.Dequeue();    //        }    //        else    //        {    //            obj = GameObject.Instantiate(poolObjectPefab);    //        }    //        IPoolObject comp = obj.GetComponent<IPoolObject>();    //        if (comp == null)    //        {    //            Debug.LogError("预置上必须绑有实现了IPoolObject接口的类");    //        }    //        obj.SetActive(true);    //        comp.Init();    //        return obj;    //    }    //    /// <summary>    //    /// 回收对象    //    /// </summary>    //    /// <param name="obj">Object.</param>    //    public void RecoverObject(GameObject obj)    //    {    //        IPoolObject comp = obj.GetComponent<IPoolObject>();    //        if (comp == null)    //        {    //            Debug.LogError("预置上必须绑有实现了IPoolObject接口的类");    //        }    //        pool.Enqueue(obj);    //        comp.Release();    //        obj.SetActive(false);    //    }    //    void Awake()    //    {    //        InitPool();    //    }    //}
0 0