对象池3(方法功能)PoolManager(控制)PoolTimeObject(时间管理)text01(调用)Destorys(销毁)

1.对象池PoolManager

namespace kernal

{

    public class PoolManager : MonoBehaviour

    {

        //“缓冲池”集合

        public static Dictionary<string, Pools> PoolsArray = new Dictionary<string, Pools>();

        // 加入“池”

        public static void Add(Pools pool)

        {

            if (PoolsArray.ContainsKey(pool.name)) return;

            PoolsArray.Add(pool.name, pool);

        }

        // 删除不用的

        public static void DestroyAllInactive()

        {

            foreach (KeyValuePair<string, Pools> keyValue in PoolsArray)

            {

                keyValue.Value.DestoryUnused();

            }

        }

        // 清空“池”

        void OnDestroy()

        {

            PoolsArray.Clear();

        }

    }

}

2.对象池 内部类: 池时间

    //[System.Serializable]

    public class PoolTimeObject

    {

        public GameObject instance;

        public float time;

    }//PoolTimeObject.cs_end

}

3.对象池调用text01

public class text01 : MonoBehaviour

{

    public Transform CubeTransform;

    public GameObject Spheres;

    private GameObject SpheressGameObject;

    void Update ()

    {

        if (Input.GetKeyDown(KeyCode.A))

        {

            StartCoroutine(LoadParticalEffectInPool(0.1f, Spheres,CubeTransform.transform.position, CubeTransform.transform.rotation, null));

        }

    }

    protected IEnumerator LoadParticalEffectInPool(float internalTime, GameObject goParEffPrefab, Vector3 VecParticalEffect, Quaternion QuaParticalEffect, Transform tranParent, AudioClip acAudioEffect = null)

    {

        //间隔时间

        yield return new WaitForSeconds(internalTime);

        //在缓冲池中,得到一个指定的预设“激活体”。

        GameObject goParticalPrefab = PoolManager.PoolsArrayDictionary["Ssss"].GetGameObjectByPool(goParEffPrefab, VecParticalEffect, QuaParticalEffect);

        if (tranParent != null)

        {

            goParticalPrefab.transform.parent = tranParent; //确定父节点

        }

    }

}

4.对象池对象删除Destorys

public class Destorys : MonoBehaviour {

        public float FloRecoverTime = 1F;        //回收时间

        public float SpeedSphere = 5;              //运动速度

        private Rigidbody _rigidbody;              //对象池对象刚体

        void Start()

        {

            _rigidbody = gameObject.GetComponent<Rigidbody>();

        }

        void Update()

        {

            _rigidbody.AddForce(Vector3.forward*SpeedSphere);

        }

        void OnEnable()

        {

            StartCoroutine("RecoverdGameObjectByTime");

        }

        void OnDisable()

        {

            StopCoroutine("RecoverdGameObjectByTime");

        }

        // 回收对象,根据指定的时间点

        IEnumerator RecoverdGameObjectByTime()

        {

          yield return new WaitForSeconds(FloRecoverTime); 

   PoolManager.PoolsArrayDictionary["Ssss"].RecoverGameObjectToPools(this.gameObject);

        }

   }

注:在对象池中的模型物体,使用时调用方法最好放在OnEnable中调用,在OnDisable中结束,回收时恢复最初状态,方便下次调用时出现的形态与自身属性。与之前对象池2(方法功能)Pools对象池1(方法功能)PoolOption联合使用。

支持个人观看使用,如商用或转载,请告知! -----萧朗(QQ:453929789 Email:xiaolang_xl@sina.com)
原文地址:https://www.cnblogs.com/XiaoLang0/p/9639296.html