From dd7843aa7175763fe0b424fb3372c84742449211 Mon Sep 17 00:00:00 2001 From: Jiangqvweihuan <15987148+jiangqvweihuan@user.noreply.gitee.com> Date: Sun, 20 Jul 2025 03:05:34 +0000 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20Assets/s?= =?UTF-8?q?cripts/gamePlay=5Fgameplay/NotePool.cs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/scripts/gamePlay_gameplay/NotePool.cs | 190 ------------------- 1 file changed, 190 deletions(-) delete mode 100644 Assets/scripts/gamePlay_gameplay/NotePool.cs diff --git a/Assets/scripts/gamePlay_gameplay/NotePool.cs b/Assets/scripts/gamePlay_gameplay/NotePool.cs deleted file mode 100644 index 0a530bf6..00000000 --- a/Assets/scripts/gamePlay_gameplay/NotePool.cs +++ /dev/null @@ -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> notePools; // 短音符对象池 - private Dictionary> holdNotePools; // 长音符片段对象池 - private Dictionary> startNotePools; // 长音符start片段对象池 - - private void Awake() - { - if (Instance == null) - { - Instance = this; - } - else - { - Destroy(gameObject); - return; - } - - notePools = new Dictionary>(); - holdNotePools = new Dictionary>(); - startNotePools = new Dictionary>(); - - for (int i = 0; i < notePrefabs.Length; i++) - { - notePools[i] = new Stack(); - holdNotePools[i] = new Stack(); - startNotePools[i] = new Stack(); - - 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 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 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 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(); - 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(); - 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(); - 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; - } - } -}