using UnityEngine; using System.Collections; using System.Collections.Generic; public class EffectUtil { public static GameObject CreateLanding() { GameObject gameObj = GameObject.Instantiate(Resources.Load("Prefabs/Effects/Landing")); return gameObj; } public static GameObject CreateDisappear() { GameObject gameObj = GameObject.Instantiate(Resources.Load("Prefabs/Effects/Disappear")); return gameObj; } private static Dictionary> effectCacheDict = new Dictionary>(); public static GameObject CreateEffect(string path) { GameObject gameObj = null; if(effectCacheDict.ContainsKey(path) && effectCacheDict[path].Count > 0) { gameObj = effectCacheDict[path][0]; effectCacheDict[path].RemoveAt(0); gameObj.SetActive(true); ParticleSystem ps = gameObj.GetComponent(); if(ps != null) ps.Play(); } else { GameObject prefabObj = Resources.Load("Prefabs/Effects/"+path); if(prefabObj == null) { Debuger.LogError("Could not load effect from : "+path); return null; } gameObj = GameObject.Instantiate(prefabObj); } EffectDelayDestroy delayDes = gameObj.GetComponent(); if(delayDes != null) delayDes.path = path; return gameObj; } public static void Recycle(string path, GameObject obj) { if(StringUtil.Empty(path)) { GameObject.Destroy(obj); Debuger.LogError("Recycal Effect path is empty "+obj); return; } // Debuger.Log("Recycal Effect["+path+"] "+obj); if(obj.transform.parent != null) obj.transform.SetParent(null); ParticleSystem ps = obj.GetComponent(); if(ps != null) ps.Stop(); obj.SetActive(false); if(effectCacheDict.ContainsKey(path)) { effectCacheDict[path].Add(obj); } else { List list = new List(); list.Add(obj); effectCacheDict.Add(path, list); } } public static void ClearCache() { effectCacheDict.Clear(); } }