using System; using PhxhSDK; using UnityEngine; using Framework.UI; using System.Collections; using Cysharp.Threading.Tasks; using System.Collections.Generic; using Constants = Framework.Constants.Constants; public class GfxManager : SingletonMono, IInitable { private Dictionary gfxDic; public void Init() { gfxDic = new Dictionary(); } public void ShowGfxOnce(ParticleSystem particle, GameObject gfx, Action callBack = null) { if (particle == null || gfx == null) return; gfx.SetActive(true); particle.Play(); CoroutineHandler.Instance.StartCoroutine(CheckParticleSystem(particle, gfx, callBack)); } private IEnumerator CheckParticleSystem(ParticleSystem particle, GameObject gfx, Action callBack) { while (particle && particle.isPlaying) { yield return null; } if (particle && gfx) { gfx.SetActive(false); particle.Stop(); } callBack?.Invoke(); } public async UniTask LoadGfx(string gfxName) { try { if (gfxDic.TryGetValue(gfxName, out var gfx) && gfx != null) { gfx.SetActive(true); return gfx; } await PreLoadGfx(gfxName); return gfxDic[gfxName]; } catch (Exception e) { DebugUtil.LogError("GfxManager.LoadGfx gfx:[{0}], Error: {1}", gfxName, e); return null; } } public async UniTask PreLoadGfx(string gfxName) { try { if (gfxDic == null) gfxDic = new Dictionary(); if (!gfxDic.ContainsKey(gfxName)) { var gfx = await AssetManager.Instance.LoadAssetAsync(string.Format(Constants.GfxPath, gfxName)); var gfxObj = Instantiate(gfx, UIRoot.Instance.rootGfx.transform); gfxObj.SetActive(false); gfxObj.name = gfx.name; gfxDic.Add(gfxName, gfxObj); return gfxObj; } return null; } catch (Exception e) { DebugUtil.LogError("GfxManager.PreLoadGfx gfx:[{0}], Error: {1}", gfxName, e); return null; } } public void ReturnGfxObj(GameObject obj, bool onDestroy = false) { try { if (gfxDic == null) return; string objName = obj.name; if (gfxDic.ContainsKey(objName)) { obj.SetActive(false); if (onDestroy) { UnloadGfx(objName); Destroy(obj); gfxDic.Remove(objName); } } else { Destroy(obj); DebugUtil.LogError("找不到对应的特效, 摧毁该物体: {0}", objName); } } catch (Exception e) { DebugUtil.LogError("GfxManager.ReturnGfxObj gfx:[{0}], Error: {1}", obj.name, e); } } public void ReturnGfxObj(string objName, bool onDestroy = false) { try { if (gfxDic == null) return; if (gfxDic.TryGetValue(objName, out var obj)) { obj.SetActive(false); if (onDestroy) { UnloadGfx(objName); Destroy(obj); gfxDic.Remove(objName); } } else { DebugUtil.LogError("找不到对应的特效: {0}", objName); } } catch (Exception e) { DebugUtil.LogError("GfxManager.ReturnGfxObj gfx:[{0}], Error: {1}", objName, e); } } private void UnloadGfx(string gfxName) { if (gfxDic.TryGetValue(gfxName, out var gfx)) { AssetManager.Instance.Unload(string.Format(Constants.GfxPath, gfxName)); } } public void Release() { foreach (var gfx in gfxDic) { if (gfx.Value != null) { DestroyImmediate(gfx.Value); } } gfxDic = null; } }