删除文件 Assets/scripts/gamePlay_gameplay/NotePool.cs
This commit is contained in:
@@ -1,190 +0,0 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
using UnityEngine;
|
|
||||||
|
|
||||||
public class NotePool : MonoBehaviour
|
|
||||||
{
|
|
||||||
public static NotePool Instance { get; private set; }
|
|
||||||
|
|
||||||
public GameObject[] notePrefabs; // 短音符预制体(按颜色存储)
|
|
||||||
public GameObject[] holdNotePrefabs; // 长音符片段预制体
|
|
||||||
public GameObject[] startNotePrefabs; // 长音符start片段预制体
|
|
||||||
private int poolSize = 12; // 每种类型的音符池大小
|
|
||||||
private int maxPoolSize = 60; // 池的最大容量
|
|
||||||
|
|
||||||
private Dictionary<int, Stack<GameObject>> notePools; // 短音符对象池
|
|
||||||
private Dictionary<int, Stack<GameObject>> holdNotePools; // 长音符片段对象池
|
|
||||||
private Dictionary<int, Stack<GameObject>> startNotePools; // 长音符start片段对象池
|
|
||||||
|
|
||||||
private void Awake()
|
|
||||||
{
|
|
||||||
if (Instance == null)
|
|
||||||
{
|
|
||||||
Instance = this;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Destroy(gameObject);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
notePools = new Dictionary<int, Stack<GameObject>>();
|
|
||||||
holdNotePools = new Dictionary<int, Stack<GameObject>>();
|
|
||||||
startNotePools = new Dictionary<int, Stack<GameObject>>();
|
|
||||||
|
|
||||||
for (int i = 0; i < notePrefabs.Length; i++)
|
|
||||||
{
|
|
||||||
notePools[i] = new Stack<GameObject>();
|
|
||||||
holdNotePools[i] = new Stack<GameObject>();
|
|
||||||
startNotePools[i] = new Stack<GameObject>();
|
|
||||||
|
|
||||||
for (int j = 0; j < poolSize; j++)
|
|
||||||
{
|
|
||||||
AddToPool(notePools[i], notePrefabs[i]);
|
|
||||||
AddToPool(startNotePools[i], startNotePrefabs[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int j = 0; j < poolSize * 5; j++)
|
|
||||||
{
|
|
||||||
AddToPool(holdNotePools[i], holdNotePrefabs[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private GameObject GetObjectFromPool(Stack<GameObject> pool, GameObject prefab)
|
|
||||||
{
|
|
||||||
if (pool.Count > 0)
|
|
||||||
{
|
|
||||||
GameObject obj = pool.Pop();
|
|
||||||
obj.SetActive(true);
|
|
||||||
return obj;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (pool.Count < maxPoolSize)
|
|
||||||
{
|
|
||||||
GameObject obj = Instantiate(prefab);
|
|
||||||
obj.SetActive(true);
|
|
||||||
return obj;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Debug.LogWarning("对象池已满,生成新的音符!");
|
|
||||||
GameObject obj = Instantiate(prefab);
|
|
||||||
obj.SetActive(true);
|
|
||||||
return obj;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ReturnObjectToPool(Stack<GameObject> pool, GameObject obj)
|
|
||||||
{
|
|
||||||
if (obj == null || pool.Contains(obj)) return; // 防止重复回收
|
|
||||||
|
|
||||||
if (obj != null)
|
|
||||||
{
|
|
||||||
obj.SetActive(false); // 设置为非活动状态
|
|
||||||
if (pool.Count < maxPoolSize)
|
|
||||||
{
|
|
||||||
pool.Push(obj);
|
|
||||||
Debug.Log($"已归还对象:{obj.name},当前池容量:{pool.Count}");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Debug.LogWarning("对象池已满,无法继续归还对象!");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void AddToPool(Stack<GameObject> pool, GameObject prefab)
|
|
||||||
{
|
|
||||||
if (pool.Count < maxPoolSize)
|
|
||||||
{
|
|
||||||
GameObject obj = Instantiate(prefab);
|
|
||||||
obj.SetActive(false);
|
|
||||||
pool.Push(obj);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public GameObject GetNote(string color)
|
|
||||||
{
|
|
||||||
|
|
||||||
int colorIndex = GetColorIndexFromName(color);
|
|
||||||
return GetObjectFromPool(notePools[colorIndex], notePrefabs[colorIndex]);
|
|
||||||
}
|
|
||||||
|
|
||||||
public GameObject GetStartNote(string color)
|
|
||||||
{
|
|
||||||
|
|
||||||
int colorIndex = GetColorIndexFromName(color);
|
|
||||||
return GetObjectFromPool(startNotePools[colorIndex], startNotePrefabs[colorIndex]);
|
|
||||||
}
|
|
||||||
|
|
||||||
public GameObject GetHoldNoteSegment(string color)
|
|
||||||
{
|
|
||||||
|
|
||||||
int colorIndex = GetColorIndexFromName(color);
|
|
||||||
return GetObjectFromPool(holdNotePools[colorIndex], holdNotePrefabs[colorIndex]);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ReturnNote(GameObject note, string color)
|
|
||||||
{
|
|
||||||
|
|
||||||
NoteController noteScript = note.GetComponent<NoteController>();
|
|
||||||
if (noteScript != null)
|
|
||||||
{
|
|
||||||
noteScript.ResetState();
|
|
||||||
}
|
|
||||||
ReturnObjectToPool(notePools[GetColorIndexFromName(color)], note);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ReturnStartNote(GameObject startNote, string color)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrEmpty(color))
|
|
||||||
{
|
|
||||||
Debug.LogWarning($" 归还 StartNote 时 color 为空!音符名: {startNote.name}");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
HoldNote holdNote = startNote.GetComponent<HoldNote>();
|
|
||||||
if (holdNote != null)
|
|
||||||
{
|
|
||||||
holdNote.ResetState(); // 确保重置音符状态
|
|
||||||
}
|
|
||||||
|
|
||||||
ReturnObjectToPool(startNotePools[GetColorIndexFromName(color)], startNote);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ReturnHoldNoteSegment(GameObject holdNote, string color)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrEmpty(color))
|
|
||||||
{
|
|
||||||
Debug.LogWarning($" 归还 HoldNoteSegment 时 color 为空!音符名: {holdNote.name}");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
HoldNote holdNoteScript = holdNote.GetComponent<HoldNote>();
|
|
||||||
if (holdNoteScript != null)
|
|
||||||
{
|
|
||||||
holdNoteScript.ResetState(); // 确保重置音符状态
|
|
||||||
}
|
|
||||||
|
|
||||||
ReturnObjectToPool(holdNotePools[GetColorIndexFromName(color)], holdNote);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private int GetColorIndexFromName(string colorName)
|
|
||||||
{
|
|
||||||
|
|
||||||
switch (colorName)
|
|
||||||
{
|
|
||||||
case "red": return 0;
|
|
||||||
case "green": return 1;
|
|
||||||
case "yellow": return 2;
|
|
||||||
case "purple": return 3;
|
|
||||||
case "blue": return 4;
|
|
||||||
default:
|
|
||||||
Debug.LogError($"未识别的颜色: {colorName},默认使用红色");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user