using System.Collections; using System.Collections.Generic; using UnityEngine; using System; public class SparksManager : MonoBehaviour { public static SparksManager Instance; public Spark[] SparkTemplates; public Transform Root_Pool; List m_Pool = new List(); private void Awake() { Instance = this; } Spark GetSparkTemplate(string name) { foreach (var effect in SparkTemplates) { if (effect != null && effect.Name == name) { return effect; } } return null; } Spark RecycleSpark(string name) { foreach (var effect in m_Pool) { if (effect != null && effect.Name == name) { return effect; } } return null; } public void PlaySpark(string name, Vector3 position) { Spark effect = RecycleSpark(name); if (effect == null) { Spark temp = GetSparkTemplate(name); effect = UnityEngine.Object.Instantiate(temp); } else { m_Pool.Remove(effect); } effect.Host = null; effect.transform.position = position; effect.transform.SetParent(this.transform); effect.gameObject.SetActive(true); } public void PlaySpark(string name, Transform host) { Spark effect = RecycleSpark(name); if (effect == null) { Spark temp = GetSparkTemplate(name); effect = UnityEngine.Object.Instantiate(temp); } else { m_Pool.Remove(effect); } effect.Host = host; effect.transform.position = host.position; effect.transform.SetParent(this.transform); effect.gameObject.SetActive(true); } // 回收特效 public void CycleSpark(Spark effect) { effect.gameObject.SetActive(false); effect.transform.SetParent(Root_Pool); m_Pool.Add(effect); } }