美文网首页
重写方法

重写方法

作者: 貪狼大人 | 来源:发表于2017-11-01 21:18 被阅读0次
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class SubPool
    {
    
        //子池子
        List<GameObject> mySubPool = new List<GameObject>();
        GameObject m_Prefab;
        public string Name
        {
            get
            {
                return m_Prefab.name;
            }
        }
        public SubPool(GameObject prefab)
        {
            m_Prefab = prefab;
        }
        public GameObject Spawn()
        {
            GameObject obj = null;
            foreach (GameObject item in mySubPool)
            {
                if (!item.activeSelf)
                {
                    
                    obj = item; break;
                }
            }
            //池中没有(不够)就新建
            if (obj == null)
            {
                obj = GameObject.Instantiate(m_Prefab);
                mySubPool.Add(obj);
            }
            //Debug.Log("111");
            obj.SetActive(true);
            IResuable ir = obj.GetComponent<IResuable>();
            if (ir != null)
                ir.Spawn();
            return obj;
        }
        public void UnSpawn(GameObject obj)
        {
            //看池中是否包含对象
            if (mySubPool.Contains(obj))
            {
                IResuable ir = obj.GetComponent<IResuable>();
                if (ir != null)
                {
                    ir.UnSpawn();
                }
                obj.SetActive(false);
                
            }
        }
        public void UnSpawnAll()
        {
            foreach (GameObject item in mySubPool)
            {
                if (item.activeSelf)
                {
                    UnSpawn(item);
                }
            }
        }
        public bool ContainObj(GameObject obj)
        {
            return mySubPool.Contains(obj);
        }
    }
    

    相关文章

      网友评论

          本文标题:重写方法

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