1. 程式人生 > >Unity3d物件池(全)

Unity3d物件池(全)

PoolManager.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

public class PoolManager : MonoSinglton<PoolManager>
{
    public Dictionary<string,SmallPool> smallPools = new Dictionary<string, SmallPool>();


    public GameObject TakeOut(string name)
    {
       foreach(var s in smallPools.Keys)
        {
            if (name == s)
            {
               return  smallPools[s].TakeOut();
            }
        }
        SmallPool sp = new SmallPool();
        sp.ObjectName = name;
        smallPools.Add(name, sp);
        return sp.TakeOut();
    }

    public void PutOn(string name, GameObject go)
    {
        foreach(var s in smallPools.Keys)
        {
            if (name == s)
            {
                smallPools[s].PutOn(go);
            }
        }
    }
}

SmallPool.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

public class SmallPool
{
    private string objectName;
    private Dictionary<int,GameObject> list=new Dictionary<int, GameObject>();
    
    public string ObjectName
    {
        get
        {
            return objectName;
        }
        set
        {
            objectName = value;
        }
    }

    public GameObject TakeOut()
    {      
        foreach(var go in list.Values)
        {
            if (go!=null&&!go.activeSelf)
            {
                go.SetActive(true);
                return go;
            }
        }
        GameObject goo = Resources.Load<GameObject>(objectName);
        goo=GameObject.Instantiate(goo);
        list.Add(list.Count, goo);
        return goo;
    }
    public void PutOn(GameObject go)
    {
        go.SetActive(false);
    }
    public void UnSpawnAll()
    {
        foreach (var go in list.Values)
        {
            go.SetActive(false);
        }
    }
}

Game.cs:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

[RequireComponent(typeof(PoolManager))]
public class Game : MonoBehaviour
{
    private PoolManager pm;
    private void Start()
    {
        pm = PoolManager.Instance;
    }
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.V))
        {
            string name = "Cube";
           GameObject go= pm.TakeOut(name);
           Debug.Log(name);
           StartCoroutine(DelayUnSpawn(name,go));
        }
        if (Input.GetKeyDown(KeyCode.B))
        {
            string name = "Sphere";
            GameObject go = pm.TakeOut(name);
            Debug.Log(name);
            StartCoroutine(DelayUnSpawn(name, go));
        }
    }
    IEnumerator DelayUnSpawn(string name, GameObject go)
    {
        yield return new WaitForSeconds(3);
        pm.PutOn(name,go);
    }
}

MonoSinglton.cs:

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

public class MonoSinglton<T> : MonoBehaviour
where T:MonoBehaviour{

    private static T instance;

    public static T Instance
    {
        get
        {
            return instance;
        }
    }

    public virtual void Awake()
    {
        instance = this as T;
    }
}