浏览代码
Generic (Scriptable) Object Pool (#117)
Generic (Scriptable) Object Pool (#117)
* Generic Object Pool Added Generic Object Pool Added Generic Object Factory Added Example Scene * Added missing documentation * Update Pool.cs Removed stray virtual keyword for time being * Removed old testing assets * Implemented Requested Changes - Removed _unavailable collection from Pool - Added batch Request/Return methods - Added IPool interface to enforce common methods - Modified example scene to showcase batch methods - Pared down ComponentFactory to its base function * Added Experimental ScriptableObjects Added FactorySOs Added PoolSOs * Implemented Pool/Factory as ScriptableObject * Initialize on Return * Added Runtime Creation Example & Various fixes and cleanup Added runtime creation example renamed Pool.Add to Pool.Create made Pool.Create protected instead of public Streamlined Request method Initialized List with known size in batch Request method Only DestroyImmediate pool ro.../main
GitHub
4 年前
当前提交
66ea2175
共有 40 个文件被更改,包括 2032 次插入 和 0 次删除
-
33UOP1_Project/Assets/LocalPoolTester.cs
-
11UOP1_Project/Assets/LocalPoolTester.cs.meta
-
8UOP1_Project/Assets/Scripts/Factory.meta
-
8UOP1_Project/Assets/Scripts/Pool.meta
-
22UOP1_Project/Assets/Scripts/Factory/ComponentFactorySO.cs
-
11UOP1_Project/Assets/Scripts/Factory/ComponentFactorySO.cs.meta
-
16UOP1_Project/Assets/Scripts/Factory/FactorySO.cs
-
11UOP1_Project/Assets/Scripts/Factory/FactorySO.cs.meta
-
11UOP1_Project/Assets/Scripts/Factory/IFactory.cs
-
11UOP1_Project/Assets/Scripts/Factory/IFactory.cs.meta
-
59UOP1_Project/Assets/Scripts/Pool/ComponentPool.cs
-
11UOP1_Project/Assets/Scripts/Pool/ComponentPool.cs.meta
-
8UOP1_Project/Assets/Scripts/Pool/Example.meta
-
8UOP1_Project/Assets/Scripts/Pool/Example/Assets.meta
-
464UOP1_Project/Assets/Scripts/Pool/Example/Assets/Example.unity
-
7UOP1_Project/Assets/Scripts/Pool/Example/Assets/Example.unity.meta
-
16UOP1_Project/Assets/Scripts/Pool/Example/Assets/ParticleFactory.asset
-
8UOP1_Project/Assets/Scripts/Pool/Example/Assets/ParticleFactory.asset.meta
-
1001UOP1_Project/Assets/Scripts/Pool/Example/Assets/Particles.prefab
-
7UOP1_Project/Assets/Scripts/Pool/Example/Assets/Particles.prefab.meta
-
16UOP1_Project/Assets/Scripts/Pool/Example/Assets/Shared GlobalParticlePool.asset
-
8UOP1_Project/Assets/Scripts/Pool/Example/Assets/Shared GlobalParticlePool.asset.meta
-
21UOP1_Project/Assets/Scripts/Pool/Example/ParticleFactorySO.cs
-
11UOP1_Project/Assets/Scripts/Pool/Example/ParticleFactorySO.cs.meta
-
37UOP1_Project/Assets/Scripts/Pool/Example/ParticlePoolSO.cs
-
11UOP1_Project/Assets/Scripts/Pool/Example/ParticlePoolSO.cs.meta
-
21UOP1_Project/Assets/Scripts/Pool/Example/PoolTester.cs
-
11UOP1_Project/Assets/Scripts/Pool/Example/PoolTester.cs.meta
-
37UOP1_Project/Assets/Scripts/Pool/Example/PoolableParticle.cs
-
11UOP1_Project/Assets/Scripts/Pool/Example/PoolableParticle.cs.meta
-
12UOP1_Project/Assets/Scripts/Pool/IPool.cs
-
11UOP1_Project/Assets/Scripts/Pool/IPool.cs.meta
-
13UOP1_Project/Assets/Scripts/Pool/IPoolable.cs
-
11UOP1_Project/Assets/Scripts/Pool/IPoolable.cs.meta
-
59UOP1_Project/Assets/Scripts/Pool/PoolSO.cs
-
11UOP1_Project/Assets/Scripts/Pool/PoolSO.cs.meta
|
|||
using System.Collections; |
|||
using System.Collections.Generic; |
|||
using UnityEngine; |
|||
|
|||
public class LocalPoolTester : MonoBehaviour |
|||
{ |
|||
[SerializeField] |
|||
private PoolableParticle _prefab = default; |
|||
[SerializeField] |
|||
private int _initialPoolSize = 5; |
|||
|
|||
private ParticlePoolSO _pool; |
|||
private ParticleFactorySO _factory; |
|||
|
|||
private IEnumerator Start() |
|||
{ |
|||
_factory = ScriptableObject.CreateInstance<ParticleFactorySO>(); |
|||
_factory.Prefab = _prefab; |
|||
_pool = ScriptableObject.CreateInstance<ParticlePoolSO>(); |
|||
_pool.name = gameObject.name; |
|||
_pool.Factory = _factory; |
|||
_pool.InitialPoolSize = _initialPoolSize; |
|||
List<PoolableParticle> particles = _pool.Request(10) as List<PoolableParticle>; |
|||
foreach (PoolableParticle particle in particles) |
|||
{ |
|||
particle.transform.position = Random.insideUnitSphere * 5f; |
|||
particle.Play(); |
|||
} |
|||
yield return new WaitForSecondsRealtime(5f); |
|||
_pool.Return(particles); |
|||
} |
|||
|
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: ff0fb790d778a05429eca3a001ab6be1 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
fileFormatVersion: 2 |
|||
guid: 89f8a7242ed47df46b9536d718743192 |
|||
folderAsset: yes |
|||
DefaultImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
fileFormatVersion: 2 |
|||
guid: e62282de61949e443adb64fc8c9892e6 |
|||
folderAsset: yes |
|||
DefaultImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using UnityEngine; |
|||
|
|||
namespace UOP1.Factory |
|||
{ |
|||
/// <summary>
|
|||
/// Implements the IFactory interface for Component types.
|
|||
/// </summary>
|
|||
/// <typeparam name="T">Specifies the component to create.</typeparam>
|
|||
public abstract class ComponentFactorySO<T> : ScriptableObject, IFactory<T> where T : Component |
|||
{ |
|||
public abstract T Prefab |
|||
{ |
|||
get; |
|||
set; |
|||
} |
|||
|
|||
public virtual T Create() |
|||
{ |
|||
return Instantiate(Prefab); |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 1d5f471b49fdf1c43a84a8deeedc5183 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using UnityEngine; |
|||
|
|||
namespace UOP1.Factory |
|||
{ |
|||
/// <summary>
|
|||
/// Implements the IFactory interface for non-abstract types.
|
|||
/// </summary>
|
|||
/// <typeparam name="T">Specifies the non-abstract type to create.</typeparam>
|
|||
public abstract class FactorySO<T> : ScriptableObject, IFactory<T> where T : new() |
|||
{ |
|||
public virtual T Create() |
|||
{ |
|||
return new T(); |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: dd765064f0956cc4d9fb91bb4c0e43ea |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
namespace UOP1.Factory |
|||
{ |
|||
/// <summary>
|
|||
/// Represents a factory.
|
|||
/// </summary>
|
|||
/// <typeparam name="T">Specifies the type to create.</typeparam>
|
|||
public interface IFactory<T> |
|||
{ |
|||
T Create(); |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 8f48ef472d47ea948bd7440ec57cd265 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using UnityEngine; |
|||
|
|||
namespace UOP1.Pool |
|||
{ |
|||
/// <summary>
|
|||
/// Implements a Pool for Component types.
|
|||
/// </summary>
|
|||
/// <typeparam name="T">Specifies the component to pool.</typeparam>
|
|||
public abstract class ComponentPool<T> : PoolSO<T> where T : Component, IPoolable |
|||
{ |
|||
public abstract int InitialPoolSize { get; set; } |
|||
private GameObject _poolRootObject; |
|||
|
|||
private void InitializePool() |
|||
{ |
|||
_poolRootObject = new GameObject(name); |
|||
DontDestroyOnLoad(_poolRootObject); |
|||
for (int i = 0; i < InitialPoolSize; i++) |
|||
{ |
|||
_available.Push(Create()); |
|||
} |
|||
} |
|||
|
|||
public override T Request() |
|||
{ |
|||
if (_poolRootObject == null) |
|||
{ |
|||
InitializePool(); |
|||
} |
|||
return base.Request(); |
|||
} |
|||
|
|||
public override void Return(T member) |
|||
{ |
|||
if (_poolRootObject == null) |
|||
{ |
|||
InitializePool(); |
|||
} |
|||
base.Return(member); |
|||
} |
|||
|
|||
protected override T Create() |
|||
{ |
|||
T newMember = base.Create(); |
|||
newMember.transform.SetParent(_poolRootObject.transform); |
|||
return newMember; |
|||
} |
|||
|
|||
public override void OnDisable() |
|||
{ |
|||
base.OnDisable(); |
|||
#if UNITY_EDITOR
|
|||
DestroyImmediate(_poolRootObject); |
|||
#else
|
|||
Destroy(_poolRootObject); |
|||
#endif
|
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: cf67ce39a704c6842aa23316769815b9 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
fileFormatVersion: 2 |
|||
guid: 8725c4e580619da4d8dcac9c7fc2369f |
|||
folderAsset: yes |
|||
DefaultImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
fileFormatVersion: 2 |
|||
guid: 0b6e60a80f970744aab92189658ae2db |
|||
folderAsset: yes |
|||
DefaultImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
%YAML 1.1 |
|||
%TAG !u! tag:unity3d.com,2011: |
|||
--- !u!29 &1 |
|||
OcclusionCullingSettings: |
|||
m_ObjectHideFlags: 0 |
|||
serializedVersion: 2 |
|||
m_OcclusionBakeSettings: |
|||
smallestOccluder: 5 |
|||
smallestHole: 0.25 |
|||
backfaceThreshold: 100 |
|||
m_SceneGUID: 00000000000000000000000000000000 |
|||
m_OcclusionCullingData: {fileID: 0} |
|||
--- !u!104 &2 |
|||
RenderSettings: |
|||
m_ObjectHideFlags: 0 |
|||
serializedVersion: 9 |
|||
m_Fog: 0 |
|||
m_FogColor: {r: 0.5, g: 0.5, b: 0.5, a: 1} |
|||
m_FogMode: 3 |
|||
m_FogDensity: 0.01 |
|||
m_LinearFogStart: 0 |
|||
m_LinearFogEnd: 300 |
|||
m_AmbientSkyColor: {r: 0.212, g: 0.227, b: 0.259, a: 1} |
|||
m_AmbientEquatorColor: {r: 0.114, g: 0.125, b: 0.133, a: 1} |
|||
m_AmbientGroundColor: {r: 0.047, g: 0.043, b: 0.035, a: 1} |
|||
m_AmbientIntensity: 1 |
|||
m_AmbientMode: 0 |
|||
m_SubtractiveShadowColor: {r: 0.42, g: 0.478, b: 0.627, a: 1} |
|||
m_SkyboxMaterial: {fileID: 10304, guid: 0000000000000000f000000000000000, type: 0} |
|||
m_HaloStrength: 0.5 |
|||
m_FlareStrength: 1 |
|||
m_FlareFadeSpeed: 3 |
|||
m_HaloTexture: {fileID: 0} |
|||
m_SpotCookie: {fileID: 10001, guid: 0000000000000000e000000000000000, type: 0} |
|||
m_DefaultReflectionMode: 0 |
|||
m_DefaultReflectionResolution: 128 |
|||
m_ReflectionBounces: 1 |
|||
m_ReflectionIntensity: 1 |
|||
m_CustomReflection: {fileID: 0} |
|||
m_Sun: {fileID: 0} |
|||
m_IndirectSpecularColor: {r: 0, g: 0, b: 0, a: 1} |
|||
m_UseRadianceAmbientProbe: 0 |
|||
--- !u!157 &3 |
|||
LightmapSettings: |
|||
m_ObjectHideFlags: 0 |
|||
serializedVersion: 11 |
|||
m_GIWorkflowMode: 1 |
|||
m_GISettings: |
|||
serializedVersion: 2 |
|||
m_BounceScale: 1 |
|||
m_IndirectOutputScale: 1 |
|||
m_AlbedoBoost: 1 |
|||
m_EnvironmentLightingMode: 0 |
|||
m_EnableBakedLightmaps: 1 |
|||
m_EnableRealtimeLightmaps: 0 |
|||
m_LightmapEditorSettings: |
|||
serializedVersion: 12 |
|||
m_Resolution: 2 |
|||
m_BakeResolution: 40 |
|||
m_AtlasSize: 1024 |
|||
m_AO: 0 |
|||
m_AOMaxDistance: 1 |
|||
m_CompAOExponent: 1 |
|||
m_CompAOExponentDirect: 0 |
|||
m_ExtractAmbientOcclusion: 0 |
|||
m_Padding: 2 |
|||
m_LightmapParameters: {fileID: 0} |
|||
m_LightmapsBakeMode: 1 |
|||
m_TextureCompression: 1 |
|||
m_FinalGather: 0 |
|||
m_FinalGatherFiltering: 1 |
|||
m_FinalGatherRayCount: 256 |
|||
m_ReflectionCompression: 2 |
|||
m_MixedBakeMode: 2 |
|||
m_BakeBackend: 1 |
|||
m_PVRSampling: 1 |
|||
m_PVRDirectSampleCount: 32 |
|||
m_PVRSampleCount: 512 |
|||
m_PVRBounces: 2 |
|||
m_PVREnvironmentSampleCount: 256 |
|||
m_PVREnvironmentReferencePointCount: 2048 |
|||
m_PVRFilteringMode: 1 |
|||
m_PVRDenoiserTypeDirect: 1 |
|||
m_PVRDenoiserTypeIndirect: 1 |
|||
m_PVRDenoiserTypeAO: 1 |
|||
m_PVRFilterTypeDirect: 0 |
|||
m_PVRFilterTypeIndirect: 0 |
|||
m_PVRFilterTypeAO: 0 |
|||
m_PVREnvironmentMIS: 1 |
|||
m_PVRCulling: 1 |
|||
m_PVRFilteringGaussRadiusDirect: 1 |
|||
m_PVRFilteringGaussRadiusIndirect: 5 |
|||
m_PVRFilteringGaussRadiusAO: 2 |
|||
m_PVRFilteringAtrousPositionSigmaDirect: 0.5 |
|||
m_PVRFilteringAtrousPositionSigmaIndirect: 2 |
|||
m_PVRFilteringAtrousPositionSigmaAO: 1 |
|||
m_ExportTrainingData: 0 |
|||
m_TrainingDataDestination: TrainingData |
|||
m_LightProbeSampleCountMultiplier: 4 |
|||
m_LightingDataAsset: {fileID: 0} |
|||
m_UseShadowmask: 1 |
|||
--- !u!196 &4 |
|||
NavMeshSettings: |
|||
serializedVersion: 2 |
|||
m_ObjectHideFlags: 0 |
|||
m_BuildSettings: |
|||
serializedVersion: 2 |
|||
agentTypeID: 0 |
|||
agentRadius: 0.5 |
|||
agentHeight: 2 |
|||
agentSlope: 45 |
|||
agentClimb: 0.4 |
|||
ledgeDropHeight: 0 |
|||
maxJumpAcrossDistance: 0 |
|||
minRegionArea: 2 |
|||
manualCellSize: 0 |
|||
cellSize: 0.16666667 |
|||
manualTileSize: 0 |
|||
tileSize: 256 |
|||
accuratePlacement: 0 |
|||
debug: |
|||
m_Flags: 0 |
|||
m_NavMeshData: {fileID: 0} |
|||
--- !u!1 &257078474 |
|||
GameObject: |
|||
m_ObjectHideFlags: 0 |
|||
m_CorrespondingSourceObject: {fileID: 0} |
|||
m_PrefabInstance: {fileID: 0} |
|||
m_PrefabAsset: {fileID: 0} |
|||
serializedVersion: 6 |
|||
m_Component: |
|||
- component: {fileID: 257078476} |
|||
- component: {fileID: 257078475} |
|||
m_Layer: 0 |
|||
m_Name: PoolTester |
|||
m_TagString: Untagged |
|||
m_Icon: {fileID: 0} |
|||
m_NavMeshLayer: 0 |
|||
m_StaticEditorFlags: 0 |
|||
m_IsActive: 1 |
|||
--- !u!114 &257078475 |
|||
MonoBehaviour: |
|||
m_ObjectHideFlags: 0 |
|||
m_CorrespondingSourceObject: {fileID: 0} |
|||
m_PrefabInstance: {fileID: 0} |
|||
m_PrefabAsset: {fileID: 0} |
|||
m_GameObject: {fileID: 257078474} |
|||
m_Enabled: 1 |
|||
m_EditorHideFlags: 0 |
|||
m_Script: {fileID: 11500000, guid: 47b003fc4c0c7f94fb3630ba713073aa, type: 3} |
|||
m_Name: |
|||
m_EditorClassIdentifier: |
|||
_pool: {fileID: 11400000, guid: 73e856d683fe4d44aa05fc3a5fe882c0, type: 2} |
|||
--- !u!4 &257078476 |
|||
Transform: |
|||
m_ObjectHideFlags: 0 |
|||
m_CorrespondingSourceObject: {fileID: 0} |
|||
m_PrefabInstance: {fileID: 0} |
|||
m_PrefabAsset: {fileID: 0} |
|||
m_GameObject: {fileID: 257078474} |
|||
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} |
|||
m_LocalPosition: {x: 0.014519863, y: -2.6415513, z: -2.9726562} |
|||
m_LocalScale: {x: 1, y: 1, z: 1} |
|||
m_Children: [] |
|||
m_Father: {fileID: 0} |
|||
m_RootOrder: 2 |
|||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} |
|||
--- !u!1 &457623035 |
|||
GameObject: |
|||
m_ObjectHideFlags: 0 |
|||
m_CorrespondingSourceObject: {fileID: 0} |
|||
m_PrefabInstance: {fileID: 0} |
|||
m_PrefabAsset: {fileID: 0} |
|||
serializedVersion: 6 |
|||
m_Component: |
|||
- component: {fileID: 457623037} |
|||
- component: {fileID: 457623036} |
|||
m_Layer: 0 |
|||
m_Name: Directional Light |
|||
m_TagString: Untagged |
|||
m_Icon: {fileID: 0} |
|||
m_NavMeshLayer: 0 |
|||
m_StaticEditorFlags: 0 |
|||
m_IsActive: 1 |
|||
--- !u!108 &457623036 |
|||
Light: |
|||
m_ObjectHideFlags: 0 |
|||
m_CorrespondingSourceObject: {fileID: 0} |
|||
m_PrefabInstance: {fileID: 0} |
|||
m_PrefabAsset: {fileID: 0} |
|||
m_GameObject: {fileID: 457623035} |
|||
m_Enabled: 1 |
|||
serializedVersion: 10 |
|||
m_Type: 1 |
|||
m_Shape: 0 |
|||
m_Color: {r: 1, g: 0.95686275, b: 0.8392157, a: 1} |
|||
m_Intensity: 1 |
|||
m_Range: 10 |
|||
m_SpotAngle: 30 |
|||
m_InnerSpotAngle: 21.80208 |
|||
m_CookieSize: 10 |
|||
m_Shadows: |
|||
m_Type: 2 |
|||
m_Resolution: -1 |
|||
m_CustomResolution: -1 |
|||
m_Strength: 1 |
|||
m_Bias: 0.05 |
|||
m_NormalBias: 0.4 |
|||
m_NearPlane: 0.2 |
|||
m_CullingMatrixOverride: |
|||
e00: 1 |
|||
e01: 0 |
|||
e02: 0 |
|||
e03: 0 |
|||
e10: 0 |
|||
e11: 1 |
|||
e12: 0 |
|||
e13: 0 |
|||
e20: 0 |
|||
e21: 0 |
|||
e22: 1 |
|||
e23: 0 |
|||
e30: 0 |
|||
e31: 0 |
|||
e32: 0 |
|||
e33: 1 |
|||
m_UseCullingMatrixOverride: 0 |
|||
m_Cookie: {fileID: 0} |
|||
m_DrawHalo: 0 |
|||
m_Flare: {fileID: 0} |
|||
m_RenderMode: 0 |
|||
m_CullingMask: |
|||
serializedVersion: 2 |
|||
m_Bits: 4294967295 |
|||
m_RenderingLayerMask: 1 |
|||
m_Lightmapping: 4 |
|||
m_LightShadowCasterMode: 0 |
|||
m_AreaSize: {x: 1, y: 1} |
|||
m_BounceIntensity: 1 |
|||
m_ColorTemperature: 6570 |
|||
m_UseColorTemperature: 0 |
|||
m_BoundingSphereOverride: {x: 0, y: 0, z: 0, w: 0} |
|||
m_UseBoundingSphereOverride: 0 |
|||
m_ShadowRadius: 0 |
|||
m_ShadowAngle: 0 |
|||
--- !u!4 &457623037 |
|||
Transform: |
|||
m_ObjectHideFlags: 0 |
|||
m_CorrespondingSourceObject: {fileID: 0} |
|||
m_PrefabInstance: {fileID: 0} |
|||
m_PrefabAsset: {fileID: 0} |
|||
m_GameObject: {fileID: 457623035} |
|||
m_LocalRotation: {x: 0.40821788, y: -0.23456968, z: 0.10938163, w: 0.8754261} |
|||
m_LocalPosition: {x: 0, y: 3, z: 0} |
|||
m_LocalScale: {x: 1, y: 1, z: 1} |
|||
m_Children: [] |
|||
m_Father: {fileID: 0} |
|||
m_RootOrder: 1 |
|||
m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0} |
|||
--- !u!1 &648587141 |
|||
GameObject: |
|||
m_ObjectHideFlags: 0 |
|||
m_CorrespondingSourceObject: {fileID: 0} |
|||
m_PrefabInstance: {fileID: 0} |
|||
m_PrefabAsset: {fileID: 0} |
|||
serializedVersion: 6 |
|||
m_Component: |
|||
- component: {fileID: 648587143} |
|||
- component: {fileID: 648587142} |
|||
m_Layer: 0 |
|||
m_Name: LocalPoolTester |
|||
m_TagString: Untagged |
|||
m_Icon: {fileID: 0} |
|||
m_NavMeshLayer: 0 |
|||
m_StaticEditorFlags: 0 |
|||
m_IsActive: 1 |
|||
--- !u!114 &648587142 |
|||
MonoBehaviour: |
|||
m_ObjectHideFlags: 0 |
|||
m_CorrespondingSourceObject: {fileID: 0} |
|||
m_PrefabInstance: {fileID: 0} |
|||
m_PrefabAsset: {fileID: 0} |
|||
m_GameObject: {fileID: 648587141} |
|||
m_Enabled: 1 |
|||
m_EditorHideFlags: 0 |
|||
m_Script: {fileID: 11500000, guid: ff0fb790d778a05429eca3a001ab6be1, type: 3} |
|||
m_Name: |
|||
m_EditorClassIdentifier: |
|||
_prefab: {fileID: -6757246126632128155, guid: 13e8a2294914b7744a0b9fe47b807ead, |
|||
type: 3} |
|||
_initialPoolSize: 5 |
|||
--- !u!4 &648587143 |
|||
Transform: |
|||
m_ObjectHideFlags: 0 |
|||
m_CorrespondingSourceObject: {fileID: 0} |
|||
m_PrefabInstance: {fileID: 0} |
|||
m_PrefabAsset: {fileID: 0} |
|||
m_GameObject: {fileID: 648587141} |
|||
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} |
|||
m_LocalPosition: {x: 0.014519863, y: -2.6415513, z: -2.9726562} |
|||
m_LocalScale: {x: 1, y: 1, z: 1} |
|||
m_Children: [] |
|||
m_Father: {fileID: 0} |
|||
m_RootOrder: 4 |
|||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} |
|||
--- !u!1 &1164917333 |
|||
GameObject: |
|||
m_ObjectHideFlags: 0 |
|||
m_CorrespondingSourceObject: {fileID: 0} |
|||
m_PrefabInstance: {fileID: 0} |
|||
m_PrefabAsset: {fileID: 0} |
|||
serializedVersion: 6 |
|||
m_Component: |
|||
- component: {fileID: 1164917338} |
|||
- component: {fileID: 1164917337} |
|||
- component: {fileID: 1164917336} |
|||
- component: {fileID: 1164917335} |
|||
m_Layer: 0 |
|||
m_Name: Main Camera |
|||
m_TagString: MainCamera |
|||
m_Icon: {fileID: 0} |
|||
m_NavMeshLayer: 0 |
|||
m_StaticEditorFlags: 0 |
|||
m_IsActive: 1 |
|||
--- !u!114 &1164917335 |
|||
MonoBehaviour: |
|||
m_ObjectHideFlags: 0 |
|||
m_CorrespondingSourceObject: {fileID: 0} |
|||
m_PrefabInstance: {fileID: 0} |
|||
m_PrefabAsset: {fileID: 0} |
|||
m_GameObject: {fileID: 1164917333} |
|||
m_Enabled: 1 |
|||
m_EditorHideFlags: 0 |
|||
m_Script: {fileID: 11500000, guid: a79441f348de89743a2939f4d699eac1, type: 3} |
|||
m_Name: |
|||
m_EditorClassIdentifier: |
|||
m_RenderShadows: 1 |
|||
m_RequiresDepthTextureOption: 2 |
|||
m_RequiresOpaqueTextureOption: 2 |
|||
m_CameraType: 0 |
|||
m_Cameras: [] |
|||
m_RendererIndex: -1 |
|||
m_VolumeLayerMask: |
|||
serializedVersion: 2 |
|||
m_Bits: 1 |
|||
m_VolumeTrigger: {fileID: 0} |
|||
m_RenderPostProcessing: 0 |
|||
m_Antialiasing: 0 |
|||
m_AntialiasingQuality: 2 |
|||
m_StopNaN: 0 |
|||
m_Dithering: 0 |
|||
m_ClearDepth: 1 |
|||
m_RequiresDepthTexture: 0 |
|||
m_RequiresColorTexture: 0 |
|||
m_Version: 2 |
|||
--- !u!81 &1164917336 |
|||
AudioListener: |
|||
m_ObjectHideFlags: 0 |
|||
m_CorrespondingSourceObject: {fileID: 0} |
|||
m_PrefabInstance: {fileID: 0} |
|||
m_PrefabAsset: {fileID: 0} |
|||
m_GameObject: {fileID: 1164917333} |
|||
m_Enabled: 1 |
|||
--- !u!20 &1164917337 |
|||
Camera: |
|||
m_ObjectHideFlags: 0 |
|||
m_CorrespondingSourceObject: {fileID: 0} |
|||
m_PrefabInstance: {fileID: 0} |
|||
m_PrefabAsset: {fileID: 0} |
|||
m_GameObject: {fileID: 1164917333} |
|||
m_Enabled: 1 |
|||
serializedVersion: 2 |
|||
m_ClearFlags: 1 |
|||
m_BackGroundColor: {r: 0.19215687, g: 0.3019608, b: 0.4745098, a: 0} |
|||
m_projectionMatrixMode: 1 |
|||
m_GateFitMode: 2 |
|||
m_FOVAxisMode: 0 |
|||
m_SensorSize: {x: 36, y: 24} |
|||
m_LensShift: {x: 0, y: 0} |
|||
m_FocalLength: 50 |
|||
m_NormalizedViewPortRect: |
|||
serializedVersion: 2 |
|||
x: 0 |
|||
y: 0 |
|||
width: 1 |
|||
height: 1 |
|||
near clip plane: 0.3 |
|||
far clip plane: 1000 |
|||
field of view: 60 |
|||
orthographic: 0 |
|||
orthographic size: 5 |
|||
m_Depth: -1 |
|||
m_CullingMask: |
|||
serializedVersion: 2 |
|||
m_Bits: 4294967295 |
|||
m_RenderingPath: -1 |
|||
m_TargetTexture: {fileID: 0} |
|||
m_TargetDisplay: 0 |
|||
m_TargetEye: 3 |
|||
m_HDR: 1 |
|||
m_AllowMSAA: 1 |
|||
m_AllowDynamicResolution: 0 |
|||
m_ForceIntoRT: 0 |
|||
m_OcclusionCulling: 1 |
|||
m_StereoConvergence: 10 |
|||
m_StereoSeparation: 0.022 |
|||
--- !u!4 &1164917338 |
|||
Transform: |
|||
m_ObjectHideFlags: 0 |
|||
m_CorrespondingSourceObject: {fileID: 0} |
|||
m_PrefabInstance: {fileID: 0} |
|||
m_PrefabAsset: {fileID: 0} |
|||
m_GameObject: {fileID: 1164917333} |
|||
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} |
|||
m_LocalPosition: {x: 0, y: 1, z: -10} |
|||
m_LocalScale: {x: 1, y: 1, z: 1} |
|||
m_Children: [] |
|||
m_Father: {fileID: 0} |
|||
m_RootOrder: 0 |
|||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} |
|||
--- !u!1 &2125951696 |
|||
GameObject: |
|||
m_ObjectHideFlags: 0 |
|||
m_CorrespondingSourceObject: {fileID: 0} |
|||
m_PrefabInstance: {fileID: 0} |
|||
m_PrefabAsset: {fileID: 0} |
|||
serializedVersion: 6 |
|||
m_Component: |
|||
- component: {fileID: 2125951698} |
|||
- component: {fileID: 2125951697} |
|||
m_Layer: 0 |
|||
m_Name: PoolTester |
|||
m_TagString: Untagged |
|||
m_Icon: {fileID: 0} |
|||
m_NavMeshLayer: 0 |
|||
m_StaticEditorFlags: 0 |
|||
m_IsActive: 1 |
|||
--- !u!114 &2125951697 |
|||
MonoBehaviour: |
|||
m_ObjectHideFlags: 0 |
|||
m_CorrespondingSourceObject: {fileID: 0} |
|||
m_PrefabInstance: {fileID: 0} |
|||
m_PrefabAsset: {fileID: 0} |
|||
m_GameObject: {fileID: 2125951696} |
|||
m_Enabled: 1 |
|||
m_EditorHideFlags: 0 |
|||
m_Script: {fileID: 11500000, guid: 47b003fc4c0c7f94fb3630ba713073aa, type: 3} |
|||
m_Name: |
|||
m_EditorClassIdentifier: |
|||
_pool: {fileID: 11400000, guid: 73e856d683fe4d44aa05fc3a5fe882c0, type: 2} |
|||
--- !u!4 &2125951698 |
|||
Transform: |
|||
m_ObjectHideFlags: 0 |
|||
m_CorrespondingSourceObject: {fileID: 0} |
|||
m_PrefabInstance: {fileID: 0} |
|||
m_PrefabAsset: {fileID: 0} |
|||
m_GameObject: {fileID: 2125951696} |
|||
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} |
|||
m_LocalPosition: {x: 0.014519863, y: -2.6415513, z: -2.9726562} |
|||
m_LocalScale: {x: 1, y: 1, z: 1} |
|||
m_Children: [] |
|||
m_Father: {fileID: 0} |
|||
m_RootOrder: 3 |
|||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} |
|
|||
fileFormatVersion: 2 |
|||
guid: 7b66255e732c611449707befad58a236 |
|||
DefaultImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
%YAML 1.1 |
|||
%TAG !u! tag:unity3d.com,2011: |
|||
--- !u!114 &11400000 |
|||
MonoBehaviour: |
|||
m_ObjectHideFlags: 0 |
|||
m_CorrespondingSourceObject: {fileID: 0} |
|||
m_PrefabInstance: {fileID: 0} |
|||
m_PrefabAsset: {fileID: 0} |
|||
m_GameObject: {fileID: 0} |
|||
m_Enabled: 1 |
|||
m_EditorHideFlags: 0 |
|||
m_Script: {fileID: 11500000, guid: 2db2fc8ea575dfd4e927bf2c91d75b5c, type: 3} |
|||
m_Name: ParticleFactory |
|||
m_EditorClassIdentifier: |
|||
_prefab: {fileID: -6757246126632128155, guid: 13e8a2294914b7744a0b9fe47b807ead, |
|||
type: 3} |
|
|||
fileFormatVersion: 2 |
|||
guid: ca185e564147672458eeabb1f9080925 |
|||
NativeFormatImporter: |
|||
externalObjects: {} |
|||
mainObjectFileID: 0 |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
1001
UOP1_Project/Assets/Scripts/Pool/Example/Assets/Particles.prefab
文件差异内容过多而无法显示
查看文件
文件差异内容过多而无法显示
查看文件
|
|||
fileFormatVersion: 2 |
|||
guid: 13e8a2294914b7744a0b9fe47b807ead |
|||
PrefabImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
%YAML 1.1 |
|||
%TAG !u! tag:unity3d.com,2011: |
|||
--- !u!114 &11400000 |
|||
MonoBehaviour: |
|||
m_ObjectHideFlags: 0 |
|||
m_CorrespondingSourceObject: {fileID: 0} |
|||
m_PrefabInstance: {fileID: 0} |
|||
m_PrefabAsset: {fileID: 0} |
|||
m_GameObject: {fileID: 0} |
|||
m_Enabled: 1 |
|||
m_EditorHideFlags: 0 |
|||
m_Script: {fileID: 11500000, guid: 485377a59a3f4fc4b9618f557ab57b6f, type: 3} |
|||
m_Name: Shared GlobalParticlePool |
|||
m_EditorClassIdentifier: |
|||
_factory: {fileID: 11400000, guid: ca185e564147672458eeabb1f9080925, type: 2} |
|||
_initialPoolSize: 5 |
|
|||
fileFormatVersion: 2 |
|||
guid: 73e856d683fe4d44aa05fc3a5fe882c0 |
|||
NativeFormatImporter: |
|||
externalObjects: {} |
|||
mainObjectFileID: 0 |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using UnityEngine; |
|||
using UOP1.Factory; |
|||
|
|||
[CreateAssetMenu(fileName = "NewParticleFactory", menuName = "Factory/Particle Factory")] |
|||
public class ParticleFactorySO : ComponentFactorySO<PoolableParticle> |
|||
{ |
|||
[SerializeField] |
|||
private PoolableParticle _prefab = default; |
|||
|
|||
public override PoolableParticle Prefab { |
|||
get |
|||
{ |
|||
return _prefab; |
|||
} |
|||
set |
|||
{ |
|||
_prefab = value; |
|||
} |
|||
} |
|||
} |
|||
|
|
|||
fileFormatVersion: 2 |
|||
guid: 2db2fc8ea575dfd4e927bf2c91d75b5c |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using UnityEngine; |
|||
using UOP1.Pool; |
|||
using UOP1.Factory; |
|||
|
|||
[CreateAssetMenu(fileName = "NewParticlePool", menuName = "Pool/Particle Pool")] |
|||
public class ParticlePoolSO : ComponentPool<PoolableParticle> |
|||
{ |
|||
[SerializeField] |
|||
private ParticleFactorySO _factory; |
|||
[SerializeField] |
|||
private int _initialPoolSize; |
|||
|
|||
public override IFactory<PoolableParticle> Factory |
|||
{ |
|||
get |
|||
{ |
|||
return _factory; |
|||
} |
|||
set |
|||
{ |
|||
_factory = value as ParticleFactorySO; |
|||
} |
|||
} |
|||
|
|||
public override int InitialPoolSize |
|||
{ |
|||
get |
|||
{ |
|||
return _initialPoolSize; |
|||
} |
|||
set |
|||
{ |
|||
_initialPoolSize = value; |
|||
} |
|||
} |
|||
} |
|||
|
|
|||
fileFormatVersion: 2 |
|||
guid: 485377a59a3f4fc4b9618f557ab57b6f |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System.Collections; |
|||
using System.Collections.Generic; |
|||
using UnityEngine; |
|||
|
|||
public class PoolTester : MonoBehaviour |
|||
{ |
|||
[SerializeField] |
|||
private ParticlePoolSO _pool = default; |
|||
|
|||
private IEnumerator Start() |
|||
{ |
|||
List<PoolableParticle> particles = _pool.Request(10) as List<PoolableParticle>; |
|||
foreach(PoolableParticle particle in particles) |
|||
{ |
|||
particle.transform.position = Random.insideUnitSphere * 5f; |
|||
particle.Play(); |
|||
} |
|||
yield return new WaitForSeconds(5f); |
|||
_pool.Return(particles); |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 47b003fc4c0c7f94fb3630ba713073aa |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using UOP1.Pool; |
|||
using System; |
|||
using System.Collections; |
|||
using UnityEngine; |
|||
|
|||
public class PoolableParticle : MonoBehaviour, IPoolable |
|||
{ |
|||
[SerializeField] |
|||
private ParticleSystem _particleSystem = default; |
|||
|
|||
public void OnRequest() |
|||
{ |
|||
gameObject.SetActive(true); |
|||
} |
|||
|
|||
public void Play() |
|||
{ |
|||
_particleSystem.Play(); |
|||
} |
|||
|
|||
public void OnReturn(Action onReturned) |
|||
{ |
|||
StartCoroutine(DoReturn(onReturned)); |
|||
} |
|||
|
|||
IEnumerator DoReturn(Action onReturned) |
|||
{ |
|||
if (_particleSystem.isPlaying) |
|||
{ |
|||
yield return new WaitForSeconds(_particleSystem.main.duration - (_particleSystem.time % _particleSystem.main.duration)); |
|||
_particleSystem.Stop(); |
|||
} |
|||
yield return new WaitUntil(() => _particleSystem.particleCount == 0); |
|||
onReturned.Invoke(); |
|||
gameObject.SetActive(false); |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 91146385b82059840915ac17bbf904ae |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
namespace UOP1.Pool |
|||
{ |
|||
/// <summary>
|
|||
/// Represents a collection that pools objects of T.
|
|||
/// </summary>
|
|||
/// <typeparam name="T">Specifies the type of elements in the pool.</typeparam>
|
|||
public interface IPool<T> |
|||
{ |
|||
T Request(); |
|||
void Return(T member); |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 4f29f50d49951f94ca40d8027f7939d8 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System; |
|||
|
|||
namespace UOP1.Pool |
|||
{ |
|||
/// <summary>
|
|||
/// Represents an object that can be pooled.
|
|||
/// </summary>
|
|||
public interface IPoolable |
|||
{ |
|||
void OnRequest(); |
|||
void OnReturn(Action onReturned); |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 936084c4c3e4d6848bfa61084af46c1c |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using UOP1.Factory; |
|||
using System.Collections.Generic; |
|||
using UnityEngine; |
|||
|
|||
namespace UOP1.Pool |
|||
{ |
|||
/// <summary>
|
|||
/// A generic pool that generates members of type T on-demand via a factory.
|
|||
/// </summary>
|
|||
/// <typeparam name="T">Specifies the type of elements to pool.</typeparam>
|
|||
public abstract class PoolSO<T> : ScriptableObject, IPool<T> where T : IPoolable |
|||
{ |
|||
protected readonly Stack<T> _available = new Stack<T>(); |
|||
public abstract IFactory<T> Factory { get; set; } |
|||
|
|||
protected virtual T Create() |
|||
{ |
|||
return Factory.Create(); |
|||
} |
|||
|
|||
public virtual T Request() |
|||
{ |
|||
T member = _available.Count > 0 ? _available.Pop() : Create(); |
|||
member.OnRequest(); |
|||
return member; |
|||
} |
|||
|
|||
public virtual IEnumerable<T> Request(int num = 1) |
|||
{ |
|||
List<T> members = new List<T>(num); |
|||
for (int i = 0; i < num; i++) |
|||
{ |
|||
members.Add(Request()); |
|||
} |
|||
return members; |
|||
} |
|||
|
|||
public virtual void Return(T member) |
|||
{ |
|||
member.OnReturn(() => |
|||
{ |
|||
_available.Push(member); |
|||
}); |
|||
} |
|||
|
|||
public virtual void Return(IEnumerable<T> members) |
|||
{ |
|||
foreach (T member in members) |
|||
{ |
|||
Return(member); |
|||
} |
|||
} |
|||
|
|||
public virtual void OnDisable() |
|||
{ |
|||
_available.Clear(); |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: ddc7640d2e7701641b7bc7efe3c8a744 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
撰写
预览
正在加载...
取消
保存
Reference in new issue