对象池

作者: 貪狼大人 | 来源:发表于2017-11-01 21:18 被阅读0次
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class PoolController : SingletonMono<PoolController> {
    
        Dictionary<string, SubPool> Mypool = new Dictionary<string, SubPool>();
        //根据名字返回对象
        public GameObject Spawn(string name)
        {
            SubPool pool;
            if (!Mypool.ContainsKey(name))
            {
                //注册新池子
                biuldpool(name);
    
            }
            pool = Mypool[name];
           return  pool.Spawn();
        }
        public void UnSpawn(GameObject obj)
        {
            foreach (SubPool pool in Mypool.Values)
            {
                if (pool.ContainObj(obj))
                {
                    print("999");
                    pool.UnSpawn(obj);
                    break;
                }
            }
        }
        public void UnSpawnAll()
        {
            foreach (SubPool pool in Mypool.Values)
            {
                pool.UnSpawnAll();
            }
        }
        //注册新池子
        private void biuldpool(string name)
        {
            GameObject obj = Resources.Load<GameObject>(name);
            SubPool pool = new SubPool(obj); Mypool.Add(name, pool);
        }
    }
    
    

    相关文章

      网友评论

          本文标题:对象池

          本文链接:https://www.haomeiwen.com/subject/kaqnpxtx.html