Unity中对象池的使用
来源:互联网 发布:螃蟹 知乎 编辑:程序博客网 时间:2024/05/21 09:30
Unity中对象池的使用
定义对象池
public class NewObjectPooler : MonoBehaviour{ public GameObject _gameObject; //需要创建的预制对象 public int int_PooledAmount = 15; //初始化数量 public Transform _tranForm; //放置预制对象的容器 public bool bool_IsGrow = true; //动态创建 List<GameObject> _gameObjects; private void Awake () { _gameObjects = new List<GameObject> (); for (int i = 0; i < int_PooledAmount; i++) { GameObject obj = (GameObject)Instantiate (_gameObject); obj.transform.SetParent (_tranForm); obj.transform.localScale = Vector3.one; obj.transform.position = _tranForm.position; obj.transform.rotation = _tranForm.rotation; obj.SetActive (false); _gameObjects.Add (obj); } } // 初始化 public void Reset () { for (int i = 0; i < _gameObjects.Count; i++) { _tranForm.GetChild (i).gameObject.SetActive (false); } } // 获取对象 public GameObject GetPooledGameObject () { for (int i = 0; i < _gameObjects.Count; i++) { if (!_gameObjects [i].activeInHierarchy) { return _gameObjects [i]; } } // 动态添加 if (bool_IsGrow) { GameObject obj = (GameObject)Instantiate (_gameObject); obj.transform.SetParent (_tranForm); obj.transform.localScale = Vector3.one; obj.transform.position = _tranForm.position; obj.transform.rotation = _tranForm.rotation; _gameObjects.Add (obj); return obj; } return null; }}
对象池的使用
public void SetData (List<DataClass.TaskData> datas) { newObjectPooler.Reset (); for (int i = 0; i < datas.Count; i++) { GameObject obj = newObjectPooler.GetPooledGameObject (); obj.SetActive (true); UITaskItem item = obj.GetComponent<UITaskItem> (); item.SetData (datas [i]); } content.sizeDelta = new Vector2 (content.sizeDelta.x, (gridLayoutGroup.cellSize.y + gridLayoutGroup.spacing.y) * datas.Count); }
阅读全文
1 0
- unity中对象池的使用
- Unity中对象池的使用
- unity对象池的使用
- unity中对象池
- Unity对象池使用
- Unity中对象池的应用
- Unity中对象池的应用
- Unity关于对象池的使用
- Unity关于对象池的使用
- PoolManager 对象池插件(Unity)的使用
- unity中如何使用litjson做对象的序列化
- unity的对象池
- unity中对象池运用
- unity中对象池运用
- Unity 对象池简单使用
- Unity 对象池的简单介绍(Dictionary使用)
- Unity中XMLParser的使用
- Unity中MovieTexture的使用
- String.trim函数算法实现
- Genymotion运行出错,包括虚拟机VBox的兼容问题
- js 中{},[]中括号,大括号使用详解
- 传入类名,通过反射,将泛型实例化
- gzip压缩
- Unity中对象池的使用
- Machine Learning学习笔记-概述
- javaweb前端后端通信乱码问题剖析
- FCC算法:六、找出多个数组中的最大数--Return Largest Numbers in Arrays
- python入门基础教程(一)
- git 配置
- 一知半解学CubeMX——TIM:定时中断实现LED闪烁
- eclipse 创建或导入的maven web项目常见问题汇总
- 机器学习--入门