组件池(Component Pool)

组件池介绍

组件池是此框架内置的适用于Unity的Component的引用池,当组件Component进入该引用池时,此组件会被隐藏(默认是Component所挂在的GameObject将被SetActive(false)),而当Component从该引用池拿出时,此组件会被显示或者说取消隐藏(默认是Component所挂在的GameObject将被自动SetActive(true))。

内置的组件池策略实现

实现的是 IPoolPolicy,主要是DefaultComponentPoolPolicy,用于传给 DefaultPoolDefaultConcurrentPool作为实例化参数。

https://github.com/VMware233/VMFramework/blob/main/Assets/VMFramework/Main/Core/Collections/Pools/ComponentPool/ComponentPoolPolicy.cs
https://github.com/VMware233/VMFramework/blob/main/Assets/VMFramework/Main/Core/Collections/Pools/ComponentPool/DefaultComponentPoolPolicy.cs
https://github.com/VMware233/VMFramework/blob/main/Assets/VMFramework/Main/Core/Collections/Pools/ComponentPool/CustomComponentPoolPolicy.cs

示例

以下是一个组件池的使用示例,实现一个简单的音效管理器:

https://github.com/VMware233/VMFramework/blob/main/Assets/Examples/Core/Pools/AudioSpawner.cs

如果顺利编译,并把此脚本挂到一个GameObject上,则应如下所示:

最后更新于

这有帮助吗?