惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Palo Alto Networks Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
C
CERT Recently Published Vulnerability Notes
C
Cybersecurity and Infrastructure Security Agency CISA
S
Schneier on Security
S
Securelist
酷 壳 – CoolShell
酷 壳 – CoolShell
C
CXSECURITY Database RSS Feed - CXSecurity.com
Cyberwarzone
Cyberwarzone
Apple Machine Learning Research
Apple Machine Learning Research
S
SegmentFault 最新的问题
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
GbyAI
GbyAI
Security Latest
Security Latest
Last Week in AI
Last Week in AI
Microsoft Security Blog
Microsoft Security Blog
云风的 BLOG
云风的 BLOG
Recorded Future
Recorded Future
Webroot Blog
Webroot Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
TaoSecurity Blog
TaoSecurity Blog
C
Cisco Blogs
博客园 - 【当耐特】
Blog — PlanetScale
Blog — PlanetScale
Hugging Face - Blog
Hugging Face - Blog
B
Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Attack and Defense Labs
Attack and Defense Labs
The Last Watchdog
The Last Watchdog
U
Unit 42
阮一峰的网络日志
阮一峰的网络日志
Project Zero
Project Zero
WordPress大学
WordPress大学
L
LINUX DO - 最新话题
F
Fortinet All Blogs
L
LINUX DO - 热门话题
PCI Perspectives
PCI Perspectives
Simon Willison's Weblog
Simon Willison's Weblog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
MongoDB | Blog
MongoDB | Blog
Latest news
Latest news
P
Proofpoint News Feed
T
Threat Research - Cisco Blogs
The Hacker News
The Hacker News
爱范儿
爱范儿
O
OpenAI News
J
Java Code Geeks
T
The Exploit Database - CXSecurity.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More

博客园 - 阿土仔

Unity UGUI动态生成控件 Unity场景间数据传递方法 UGUI防止穿透和判断点击的是否是UI Unity中的Character Controller Unity插件研究-EasyTouch V5 Unity塔防游戏的创建 Unity性能优化-DrawCall Unity性能优化-对象池 Unity性能优化-音频设置 Unity性能优化-遮挡剔除 Unity异步加载场景 Unity 协程 HBuilder git使用-分工合作 HBuilder git合作-代码同步 微信小程序开发-窗体设置 HBuilder git合作-从Git Hub Clone项目 HBuilder git合作-上传项目到Git Hub 微信小程序开发-tabbar组件 HBuilder git使用-建立仓库,邀请用户
Unity 2019中对象池的用法
阿土仔 · 2023-05-05 · via 博客园 - 阿土仔

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ExamplePool : MonoBehaviour
{
private ObjectPool<GameObject> objectPool;

public GameObject objectType;

public int poolSize = 10;

void Start()
{
objectPool = new ObjectPool<GameObject>(() =>
{
GameObject obj = Instantiate(objectType);
obj.SetActive(false);
return obj;
}, poolSize);
}

void Update()
{
GameObject obj = objectPool.Get();
obj.SetActive(true);
obj.transform.position = new Vector3(Random.Range(-5f, 5f), 1f, Random.Range(-5f, 5f));

StartCoroutine(ReleaseObjectAfterDuration(obj, 1f));
}

private IEnumerator ReleaseObjectAfterDuration(GameObject obj, float delay)
{
yield return new WaitForSeconds(delay);
objectPool.Release(obj);
}
}

在此示例中,我们创建了一个可以管理GameObject对象池的ObjectPool<GameObject>实例。我们使用了Unity 2019提供的新泛型对象池API:new ObjectPool<T>(Func<T> factoryMethod, int initialCapacity)。在函数中,我们传递了一个工厂方法,用于在添加至池中前实例化一个新对象。

Start()方法中,我们实例化了一个GameObject类型对象池,初始化完成后,我们可以随时使用Get()Release()方法。当需要一个实例时,我们可以使用Get()方法返回一个可用的对象实例并将其设置为已激活状态。当我们使用完对象时,需要将其释放到对象池中,这时我们使用Release()方法来将其放回对象池。

值得一提的是,在Unity中自带的泛型对象池API中,我们并不需要实现对象的创建或销毁等处理。对象池自动处理创建和销毁的逻辑,并通过工厂方法减少复杂的编码需求。