对象池二
来源:互联网 发布:2017淘宝手机首页装修 编辑:程序博客网 时间:2024/05/16 08:58
using UnityEngine;using System.Collections;using System.Collections.Generic;public class PoolMsg : MonoBehaviour { public static PoolMsg instance; Dictionary<string, ArrayList> pool = new Dictionary<string, ArrayList>(); void Awake() { instance = this; } public GameObject GetInPool(string key,Vector3 pos,Quaternion q) { string prefabName = key + "(Clone)"; GameObject go; if (pool.ContainsKey(prefabName) && pool[prefabName].Count > 0) { go = pool[prefabName][0] as GameObject; pool[prefabName].RemoveAt(0); go.SetActive(true); } else { go = Instantiate(Resources.Load(key)) as GameObject; go.transform.SetParent(transform); } go.transform.position = pos; go.transform.rotation = q; return go; } public GameObject ReturnPool(GameObject go) { string prefabName = go.name; if (pool.ContainsKey(prefabName)) { pool[prefabName].Add(go); } else { pool[prefabName] = new ArrayList() { go }; } go.SetActive(false); return go; }}//挂在子弹上using UnityEngine;using System.Collections;public class Hide : MonoBehaviour { [SerializeField] float delayTime = 2f; void Update() { StartCoroutine(HideBullet()); }IEnumerator HideBullet() { yield return new WaitForSeconds(delayTime); PoolMsg.instance.ReturnPool(this.gameObject); }}//挂在控制发射的物体上using UnityEngine;using System.Collections;public class TankMove : MonoBehaviour { [SerializeField] GameObject buttlePrefab; [SerializeField] Transform firePos; [SerializeField] float bulletSpeed = 100f; void Update() { Fire(); } void Fire() { if (Input.GetKeyUp(KeyCode.Space)) { GameObject go=PoolMsg.instance.GetInPool(buttlePrefab.name, firePos.position, firePos.rotation); go.GetComponent<Rigidbody>().velocity = transform.forward * bulletSpeed; } }}
1 0
- 对象池二
- simple_pool对象池——优化<二>
- simple_pool对象池——优化<二>
- DHTML对象模型 (二)
- ASP对象总结(二)
- C++对象创建(二)
- C++对象模型(二)
- javascript对象讨论二
- Javascript对象(二)
- [Remoting] 二:远程对象
- 面向对象(二)
- [Remoting] 二:远程对象
- DOM对象(二)
- 面向对象程序设计(二)
- 面向对象高级(二)
- JDBC (二) -- Statement对象
- 面向对象(二)
- 面向对象(二)
- Azure 平台上 SharePoint 2013 服务器灾难还原后,重新加场的方法。
- AngularJS 截取字符串
- python+Django环境搭建
- Android业务组件化之URL Scheme使用
- 秦始皇百万雄狮三年内消失之谜
- 对象池二
- JNDI简介
- 云处理
- dts中memreserve和reserved-memory的区别
- 封箱和拆箱
- android studio 2.2.3 导入第三方.so,并调用方法(无cpp、c源码)
- java web Servlet--实现文件下载功能
- “聊天的秘密”之HMM读心术
- KEIL MDK LIB库文件的制作