网站模板建站教程视频,织梦手机网站建设,茶叶网站开发目的和意义,苏州做网站推广问题描述#xff1a;Unity在创建和销毁对象的时候是很消耗性能的#xff0c;所以我们在销毁一个对象的时候#xff0c;可以不用Destroy#xff0c;而是将这个物体隐藏后放到回收池里面#xff0c;当再次需要的时候如果回收池里面有之前回收的对象#xff0c;就直接拿来用…问题描述Unity在创建和销毁对象的时候是很消耗性能的所以我们在销毁一个对象的时候可以不用Destroy而是将这个物体隐藏后放到回收池里面当再次需要的时候如果回收池里面有之前回收的对象就直接拿来用这样就可以大量的创建和销毁物体。
首先创建一个子弹预制体并添加刚体组件然后创建一个Skill空对象和一个Skill脚本 using System.Collections;
using UnityEngine;public class Skill : MonoBehaviour
{public int _SkillLv;//技能等级public GameObject bluut;//子弹预制体[Header(可调节参数)]//速度合成public float _Speedy;public float _Speedx;public float _cd;void Start(){StartCoroutine(FireCoroutine());}//定义一个携程用于在一定时间后发射子弹//间隔为_cd秒IEnumerator FireCoroutine(){while (true){yield return new WaitForSeconds(_cd);fashe(_SkillLv);}}//发射子弹的方法void fashe(int lv){for (int i 0; i lv; i){GameObject bullet Instantiate(bluut, transform.position, Quaternion.identity);int randomY Random.Range(200, (int)_Speedy);int randomX Random.Range(-(int)_Speedx, (int)_Speedx);bullet.GetComponentRigidbody2D().AddForce(new Vector2(randomX, randomY));Destroy(bullet, 5f);}}
}将Skill脚本加到Skill空对象上然后将子弹预制体拖入然后运行游戏。
调整参数并观察游戏的帧率可以看到在等级为10cd为1的时候帧率还是挺高的 上上强度当等级为500,cd为1的时候可以看到此时的帧率下降明显 现在看看加入对象池的效果 首先创建一个BulletPoolManager空对象和对应的脚本并将脚本拖到空对象上 using System.Collections.Generic;
using UnityEngine;public class BulletPoolManager : MonoBehaviour
{// 子弹的预制体public GameObject bulletPrefab;//初始化对象池private QueueGameObject pool new QueueGameObject();//单例模式public static BulletPoolManager Instance;void Awake(){if (Instance null){Instance this;DontDestroyOnLoad(gameObject);}else{Destroy(gameObject);}}//创建子弹的方法public GameObject GetBullet(){if (pool.Count 0){GameObject obj pool.Dequeue();obj.SetActive(true);return obj;}else{// 如果对象池里面没有可用的子弹则实例化一个新的子弹对象//注意这里是为了演示所有子弹是直接从预制体中实例化的实际使用时需要动态创建子弹对象GameObject newbullet Instantiate(bulletPrefab); return newbullet;}}// 回收子弹的方法public void ReturnBullet(GameObject bullet){bullet.SetActive(false);bullet.transform.SetParent(transform);pool.Enqueue(bullet);}
}
将子弹的预制体拖到回收池对应位置这里为了方便直接用拖的实际使用的时候根据需要修改这里 修改技能代码
主要修改的地方 using System.Collections;
using UnityEngine;public class Skill : MonoBehaviour
{public int _SkillLv;//技能等级public GameObject bluut;//子弹预制体[Header(可调节参数)]//速度合成public float _Speedy;public float _Speedx;public float _cd;void Start(){StartCoroutine(FireCoroutine());}//定义一个携程用于在一定时间后发射子弹//间隔为_cd秒IEnumerator FireCoroutine(){while (true){yield return new WaitForSeconds(_cd);fashe(_SkillLv);}}//发射子弹的方法void fashe(int lv){for (int i 0; i lv; i){GameObject bullet BulletPoolManager.Instance.GetBullet();bullet.transform.position transform.position;bullet.transform.parent gameObject.transform;int randomY Random.Range(200, (int)_Speedy);int randomX Random.Range(-(int)_Speedx, (int)_Speedx);bullet.GetComponentRigidbody2D().AddForce(new Vector2(randomX, randomY));StartCoroutine(ReturnBullet(bullet));}}IEnumerator ReturnBullet(GameObject bullet){yield return new WaitForSeconds( 5f);BulletPoolManager.Instance.ReturnBullet(bullet);}
}运行游戏并修改参数我们会发现同样等级10cd1的时候的帧率要高于不用回收池的时候。 试试等级500cd为1 可以看到使用对象池还是有一点提升的。
你可以在这里看到回收的对象 这是用的对象池是用队列来存储回收的对象的在实际开发中并不一定要使用队列只要能存储多个对象的数据结构不管是泛型还是数组都可以当做对象池。