加入很多新内容,这波一块交了
This commit is contained in:
@@ -3,6 +3,11 @@ using System.Collections;
|
||||
|
||||
public class Animation_GenerateJudgementSituationPrefab : MonoBehaviour
|
||||
{
|
||||
private sealed class PooledJudgeTag : MonoBehaviour
|
||||
{
|
||||
public GameObject prefabSource;
|
||||
}
|
||||
|
||||
public static Animation_GenerateJudgementSituationPrefab Instance;
|
||||
|
||||
[Header("Inspector")]
|
||||
@@ -42,6 +47,9 @@ public class Animation_GenerateJudgementSituationPrefab : MonoBehaviour
|
||||
|
||||
// Track last scene to detect scene changes
|
||||
private int lastSceneIndex = -1;
|
||||
private Coroutine prewarmJudgeCoroutine;
|
||||
private readonly System.Collections.Generic.Dictionary<GameObject, System.Collections.Generic.Stack<GameObject>> judgePools
|
||||
= new System.Collections.Generic.Dictionary<GameObject, System.Collections.Generic.Stack<GameObject>>();
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
@@ -147,7 +155,10 @@ public class Animation_GenerateJudgementSituationPrefab : MonoBehaviour
|
||||
|
||||
if (JudgeManager.IsDebugEnabled) Debug.Log($"[Animation_Generate] Spawning judge prefab: color={color}, result={judgeResult}, prefab={prefabToUse.name}");
|
||||
|
||||
GameObject instance = Instantiate(prefabToUse, spawnPoint);
|
||||
GameObject instance = GetPooledJudge(prefabToUse, spawnPoint);
|
||||
if (instance == null)
|
||||
return;
|
||||
|
||||
instance.transform.localPosition = Vector3.zero;
|
||||
|
||||
// Documentation text normalized.
|
||||
@@ -159,6 +170,52 @@ public class Animation_GenerateJudgementSituationPrefab : MonoBehaviour
|
||||
StartCoroutine(AnimateSprite(instance));
|
||||
}
|
||||
|
||||
public void PrewarmJudgePrefabs(int perPrefab = 1)
|
||||
{
|
||||
if (!Application.isPlaying)
|
||||
return;
|
||||
|
||||
if (prewarmJudgeCoroutine != null)
|
||||
{
|
||||
StopCoroutine(prewarmJudgeCoroutine);
|
||||
}
|
||||
|
||||
prewarmJudgeCoroutine = StartCoroutine(PrewarmJudgePrefabsRoutine(Mathf.Max(1, perPrefab)));
|
||||
}
|
||||
|
||||
public IEnumerator PrewarmJudgePrefabsRoutine(int perPrefab = 1)
|
||||
{
|
||||
GameObject[] prefabs =
|
||||
{
|
||||
perfect_judge_prefab,
|
||||
great_judge_prefab,
|
||||
good_judge_prefab,
|
||||
miss_judge_prefab
|
||||
};
|
||||
|
||||
int count = Mathf.Max(1, perPrefab);
|
||||
foreach (GameObject prefab in prefabs)
|
||||
{
|
||||
if (prefab == null)
|
||||
continue;
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
GameObject instance = null;
|
||||
try
|
||||
{
|
||||
instance = CreatePooledJudge(prefab);
|
||||
ReturnJudgeToPool(instance);
|
||||
}
|
||||
catch { }
|
||||
|
||||
yield return null;
|
||||
}
|
||||
}
|
||||
|
||||
prewarmJudgeCoroutine = null;
|
||||
}
|
||||
|
||||
private IEnumerator AnimateSprite(GameObject obj)
|
||||
{
|
||||
if (obj == null) yield break;
|
||||
@@ -232,7 +289,76 @@ public class Animation_GenerateJudgementSituationPrefab : MonoBehaviour
|
||||
|
||||
// Documentation text normalized.
|
||||
|
||||
if (obj != null) Destroy(obj);
|
||||
if (obj != null) ReturnJudgeToPool(obj);
|
||||
}
|
||||
|
||||
private GameObject GetPooledJudge(GameObject prefab, Transform parent)
|
||||
{
|
||||
if (prefab == null || parent == null)
|
||||
return null;
|
||||
|
||||
if (!judgePools.TryGetValue(prefab, out var pool))
|
||||
{
|
||||
pool = new System.Collections.Generic.Stack<GameObject>();
|
||||
judgePools[prefab] = pool;
|
||||
}
|
||||
|
||||
GameObject instance = null;
|
||||
while (pool.Count > 0 && instance == null)
|
||||
{
|
||||
instance = pool.Pop();
|
||||
}
|
||||
|
||||
if (instance == null)
|
||||
{
|
||||
instance = CreatePooledJudge(prefab);
|
||||
}
|
||||
|
||||
if (instance == null)
|
||||
return null;
|
||||
|
||||
instance.transform.SetParent(parent, false);
|
||||
instance.SetActive(true);
|
||||
return instance;
|
||||
}
|
||||
|
||||
private GameObject CreatePooledJudge(GameObject prefab)
|
||||
{
|
||||
if (prefab == null)
|
||||
return null;
|
||||
|
||||
GameObject instance = Instantiate(prefab, transform);
|
||||
var tag = instance.GetComponent<PooledJudgeTag>();
|
||||
if (tag == null)
|
||||
tag = instance.AddComponent<PooledJudgeTag>();
|
||||
tag.prefabSource = prefab;
|
||||
instance.SetActive(false);
|
||||
return instance;
|
||||
}
|
||||
|
||||
private void ReturnJudgeToPool(GameObject instance)
|
||||
{
|
||||
if (instance == null)
|
||||
return;
|
||||
|
||||
var tag = instance.GetComponent<PooledJudgeTag>();
|
||||
if (tag == null || tag.prefabSource == null)
|
||||
{
|
||||
Destroy(instance);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!judgePools.TryGetValue(tag.prefabSource, out var pool))
|
||||
{
|
||||
pool = new System.Collections.Generic.Stack<GameObject>();
|
||||
judgePools[tag.prefabSource] = pool;
|
||||
}
|
||||
|
||||
instance.transform.SetParent(transform, false);
|
||||
instance.transform.localPosition = new Vector3(99999f, 99999f, 0f);
|
||||
instance.transform.localRotation = Quaternion.identity;
|
||||
instance.SetActive(false);
|
||||
pool.Push(instance);
|
||||
}
|
||||
|
||||
private Transform GetSpawnPoint(string color)
|
||||
|
||||
Reference in New Issue
Block a user