Unity游戏开发——对象池

单个对象池

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 单个对象池
/// </summary>
[Serializable]
public class ObjectPool
{
    /// <summary>
    ///池子的名称
    /// </summary>
    public string Name;
    /// <summary>
    /// 池子的物体
    /// </summary>
    [SerializeField]
    private GameObject Prefab;
    /// <summary>
    /// 池子的最大数量
    /// </summary>
    [SerializeField]
    private int MaxCount;
    /// <summary>
    /// 对象池内所有对象的集合
    /// </summary>
    private  List<GameObject> PrefabList = new List<GameObject>();
    

    public bool Contains(GameObject go) 
    {
        return PrefabList.Contains(go);   
     }

     
    /// <summary>
    /// 从池子里获取对象
    /// </summary>
    public GameObject GetObject()
    {
        GameObject go = null;
        for (int i = 0; i < PrefabList.Count; i++)
        {
            if (!PrefabList[i].activeSelf)
            {
                go = PrefabList[i];
                go.SetActive(true);
                break;
            }
        }
        if (go == null)
        {
            if (PrefabList.Count > MaxCount)
            {

                GameObject.Destroy(PrefabList[0]);
                PrefabList.RemoveAt(0);

            }
            go = GameObject.Instantiate(Prefab);
            PrefabList.Add(go);
        }
        go.SendMessage("BeforeGetObject", SendMessageOptions.DontRequireReceiver);
        return go;

    }
    /// <summary>
    /// 隐藏指定的游戏对象
    /// </summary>
    /// <param name="go"></param>
    public void HideObject(GameObject go)
    {
        if (PrefabList.Contains(go))
        {
            go.SendMessage("BeforeHideObject",SendMessageOptions.DontRequireReceiver);
            go.SetActive(false);
         }

    }
    /// <summary>
    /// 隐藏该池子的全部游戏对象
    /// </summary>
    public void HideAllObject() 
    {
        for (int i = 0; i < PrefabList.Count; i++)
        {
            if (PrefabList[i].activeSelf) {
                HideObject(PrefabList[i]);
            }
        }    
    
    }
    public void InitPool() 
    {
        PrefabList = new List<GameObject>();
    }
}

管理类

using System.Collections.Generic;
using UnityEngine;

public class PoolManager
{
    private static PoolManager instance;

    public const string PoolConfigPath = @"Assets\Scripts\FrameWork\Pool\Resources\pool.asset";

    public PoolManager()
    {

        ObjectPoolList objectPoolList = Resources.Load<ObjectPoolList>("pool");
        foreach (var pool in objectPoolList.PoolList)
        {
            this.poolDict.Add(pool.Name, pool);
        }

    }
    public static PoolManager Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new PoolManager();
            }
            return instance;
        }
    }
    /// <summary>
    /// key是池子名称 value是单个池子
    /// </summary>
    private Dictionary<string, ObjectPool> poolDict = new Dictionary<string, ObjectPool>();
    /// <summary>
    /// 根据名字取物体
    /// </summary>  
    /// <param name="poolName"></param>
    /// <returns></returns>
    public GameObject GetObject(string poolName)
    {
        if (!poolDict.ContainsKey(poolName))
        {
            Debug.Log($"没有这个{poolName}池子");
            return null;
        }
       
        ObjectPool pool = poolDict[poolName];

        return pool.GetObject();
    }
    /// <summary>
    /// 隐藏指定物体
    /// </summary>
    /// <param name="go"></param>
    public void HideObject(GameObject go)
    {
        foreach (ObjectPool p in poolDict.Values)
        {
            if (p.Contains(go))
            {
                p.HideObject(go);
                return;
            }
        }
    }

    /// <summary>
    /// 指定一个池子隐藏池子内所有物体
    /// </summary>
    /// <param name="poolName"></param>
    public void HideAllObject(string poolName)  
    {
        if (!poolDict.ContainsKey(poolName))
        {
            Debug.Log($"没有这个{poolName}池子");
        }
        ObjectPool pool = poolDict[poolName];
        pool.HideAllObject();
    }
    /// <summary>
    /// 还原对象池
    /// </summary>
    public void InitAllPool()
    {

        foreach (var pool in poolDict.Values)
        {
            pool.InitPool();
        }
    }
}

using UnityEngine;
using UnityEditor;

public class PoolEditor : MonoBehaviour
{
    [MenuItem("Manager/Creat PoolConfig")]
    static void CreatePoolList() {
        ObjectPoolList poolList = ScriptableObject.CreateInstance<ObjectPoolList>();
        string path = PoolManager.PoolConfigPath;
        AssetDatabase.CreateAsset(poolList,path);
        AssetDatabase.SaveAssets();
        EditorUtility.DisplayDialog("提示","创建成功!","好的");
    
    }
}

using UnityEngine;

public abstract class ReuseableObject : MonoBehaviour
{

    /// <summary>
    /// 取出之前的重置操作
    /// </summary>
    public abstract void BeforeGetObject();

    /// <summary>
    /// 放入之前的还原操作
    /// </summary>
    public abstract void BeforeHideObject();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

暗夜__

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值