zoukankan      html  css  js  c++  java
  • 对象池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)
  • 相关阅读:
    MySql使用游标Cursor循环(While)更新数据
    初试TinyIoCContainer笔记
    用Razor做静态页面生成器
    在CentOS6.5上安装MariaDB
    mono的远程调试
    mono3.2.3+Jexus5.5+openSuSE13.1的asp.net
    mono3.2和monodevelop4.0在ubuntu12.04上两天的苦战
    第一节知识点:.net与c#的概念
    支付宝支付功能(使用支付宝sdk)
    vs2017/vs2019 去掉 单击aspx文件预览页面
  • 原文地址:https://www.cnblogs.com/XiaoLang0/p/9639296.html
Copyright © 2011-2022 走看看