-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRuntimeGameObjectPoolExcerpt.cs
More file actions
110 lines (95 loc) · 3.96 KB
/
Copy pathRuntimeGameObjectPoolExcerpt.cs
File metadata and controls
110 lines (95 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Curated FINAL PRESSURE portfolio excerpt.
// Pool storage, diagnostics, helpers, and project types are omitted.
// This file demonstrates production spawn/release invariants; it is not standalone compilable code.
using System.Collections.Generic;
using UnityEngine;
namespace FinalPressure.Gameplay.Features.FX
{
public static partial class RuntimeGameObjectPool
{
public static GameObject Spawn(
GameObject prefab,
Vector3 position,
Quaternion rotation,
Transform parent = null,
float scaleMultiplier = 1f,
float lifetime = 0f,
bool stripColliders = true)
{
if (prefab == null)
return null;
if (IsFinite(position) == false || IsFinite(rotation) == false || IsFinite(scaleMultiplier) == false)
{
RecordInvalidPosition(prefab.name);
return null;
}
int key = prefab.GetEntityId().GetHashCode();
GameObject instance = TakeOrCreate(prefab, key, stripColliders);
PooledGameObject handle = GetHandle(instance);
handle.ResetForSpawn();
Transform instanceTransform = instance.transform;
instanceTransform.SetParent(parent, false);
instanceTransform.SetPositionAndRotation(position, rotation);
instanceTransform.localScale = handle.BaseScale * Mathf.Max(0.05f, scaleMultiplier);
handle.MarkSpawned();
ActiveInstances.Add(instance);
instance.SetActive(true);
handle.RestorePresentationState();
RestartPresentation(instance);
ArmLifetime(instance, lifetime);
RecordSpawn(handle.FamilyId, IsAtWorldOrigin(instance));
return instance;
}
public static void ReleaseOrDestroy(GameObject instance)
{
if (instance == null)
return;
PooledGameObject handle = instance.GetComponent<PooledGameObject>();
if (handle == null)
{
ActiveInstances.Remove(instance);
KillTweens(instance);
StopPresentation(instance);
instance.GetComponent<VfxLifecycleTag>()?.NotifyPoolRelease(visibleAfterRelease: false);
DestroyRuntimeObject(instance);
return;
}
if (handle.TryMarkReleased() == false)
return;
ActiveInstances.Remove(instance);
KillTweens(instance);
StopPresentation(instance);
handle.DisablePresentationState();
Transform instanceTransform = instance.transform;
instanceTransform.SetParent(GetRoot(), false);
instanceTransform.localPosition = Vector3.zero;
instanceTransform.localRotation = Quaternion.identity;
instanceTransform.localScale = handle.BaseScale;
bool visibleAfterRelease = handle.HasEnabledRenderer();
instance.GetComponent<VfxLifecycleTag>()?.NotifyPoolRelease(visibleAfterRelease);
instance.SetActive(false);
RecordRelease(handle.FamilyId, visibleAfterRelease);
if (Pools.TryGetValue(handle.PoolKey, out Stack<GameObject> pool) == false)
{
pool = new Stack<GameObject>();
Pools.Add(handle.PoolKey, pool);
}
if (pool.Count >= MaxPooledPerPrefab)
{
DestroyRuntimeObject(instance);
return;
}
pool.Push(instance);
}
public static void ReleaseAllActive()
{
if (ActiveInstances.Count == 0)
return;
GameObject[] snapshot = new GameObject[ActiveInstances.Count];
ActiveInstances.CopyTo(snapshot);
for (int i = 0; i < snapshot.Length; i++)
ReleaseOrDestroy(snapshot[i]);
ActiveInstances.Clear();
}
}
}