Unity对象池封装

一共分为两个类,SubPool与ObjectPool,SubPool类为总对象池包含ObjectPool的子池子,ObjectPool包含需要入池的对象。

调用方法:

  • 入池:ObjectPool.Instance.Spawn()
  • 出池:ObjectPool.Instance.UnSpawn()
  • 全部出池:ObjectPool.Instance.UnSpawnAll()
  • 销毁池子:ObjectPool.Instance.DisPool();

SubPool类(继承至单例父类)

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

public class SubPool
{
    List Pool=new List();

    /// 
    /// 入池
    /// 
    public GameObject Spawn(string obj)
    {
            foreach (GameObject item in Pool)
            {
                if (!item.activeSelf)
                {
                    item.SetActive(true);
                    item.SendMessage("Spawn",SendMessageOptions.DontRequireReceiver);
                    return item;
                }
            }
        GameObject go = CreateObj(obj);
        Pool.Add(go);
       go.SendMessage("Spawn",SendMessageOptions.DontRequireReceiver);
        return go;
    }

/// 
/// 出池
/// 
/// 
 public void UnSpawn(GameObject go=null,string goname="")
    {
        if (go!=null)
        {
            go.SetActive(false);
        }
        else
        {
            for (int i = 0; i < Pool.Count; i++)
            {
                if (Pool[i].activeSelf)
                {
                    Pool[i].SetActive(false);
                    return;
                }
            }
        }
    }

/// 
/// 全部出池
/// 
public void UnSpawnAll()
    {
        foreach (GameObject item in Pool)
        {
            item.SetActive(false);
        }
    }

    /// 
    /// 创建对象
    /// 
    GameObject CreateObj(string obj)
    {
        GameObject go = GameObject.Instantiate(Resources.Load("Prefabs/Others/"+obj) as GameObject);
        go.transform.SetParent(GameObject.Find("Pool").transform, false);
        go.name = go.name.Replace("(Clone)", "");
        return go;
    }
}

ObjectPool类

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

public class ObjectPool:Manager
{
    ObjectPool()
    {
    }

    Dictionary pools= new Dictionary();
    
    /// 
    /// 入池
    /// 
    /// 创建的对象名
    public GameObject Spawn(string obj)
    {
            if (pools.ContainsKey(obj))
            {
                return pools[obj].Spawn(obj);
            }
            else
            {
                SubPool sp = new SubPool();
               var go = sp.Spawn(obj);
                pools.Add(obj, sp);
                return go;
            }
        return null;
    }

    /// 
    /// 出池
    /// 
    /// 需要出池对象
    public void UnSpawn(GameObject go=null,string goName="")
    {
        if (!go)
        {
            if (pools.ContainsKey(goName))
            {
                pools[goName].UnSpawn(null,goName);
            }
        }
        else
        {
            if (pools.ContainsKey(go.name))
            {
                pools[go.name].UnSpawn(go);
            }
        }

    }

    /// 
    /// 全部出池
    /// 
    /// 出池的名称
    public void UnSpawnAll(string pool)
    {
        if (pools.ContainsKey(pool))
        {
            pools[pool].UnSpawnAll();
        }
    }

    /// 
    /// 销毁池子
    /// 
    /// 销毁池的名称
    public void DisPool(string pool)
    {
        if (pools.ContainsKey(pool))
        {
            pools.Remove(pool);
        }
    }


}

你可能感兴趣的:(Unity对象池封装)