Forest_Client/Forest/Assets/Scripts/Gameplay/Manager/GfxManager.cs

169 lines
4.5 KiB
C#
Raw Normal View History

2024-06-12 15:01:54 +08:00
using System;
using PhxhSDK;
using UnityEngine;
using Framework.UI;
using System.Collections;
using Framework.Constants;
2024-06-12 15:01:54 +08:00
using Cysharp.Threading.Tasks;
using System.Collections.Generic;
public class GfxManager : SingletonMono<GfxManager>, IInitable
{
private Dictionary<string, GameObject> gfxDic;
public void Init()
{
gfxDic = new Dictionary<string, GameObject>();
}
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<GameObject> LoadGfx(string gfxName, bool show = false)
2024-06-12 15:01:54 +08:00
{
try
{
if (gfxDic.TryGetValue(gfxName, out var gfx) && gfx != null)
{
gfx.SetActive(true);
return gfx;
}
await PreLoadGfx(gfxName, show);
2024-06-12 15:01:54 +08:00
return gfxDic[gfxName];
}
catch (Exception e)
{
DebugUtil.LogError("GfxManager.LoadGfx gfx:[{0}], Error: {1}", gfxName, e);
return null;
}
}
2024-07-25 22:51:34 +08:00
public async UniTask<GameObject> PreLoadGfx(string gfxName, bool show = false, bool inUI = false)
2024-06-12 15:01:54 +08:00
{
try
{
if (gfxDic == null)
gfxDic = new Dictionary<string, GameObject>();
if (!gfxDic.ContainsKey(gfxName))
{
var gfx = await AssetManager.Instance.LoadAssetAsync<GameObject>(string.Format(PathConstants.GfxPath,
2024-06-12 15:01:54 +08:00
gfxName));
2024-07-25 22:51:34 +08:00
var root = inUI ? UIRoot.Instance.root.transform : UIRoot.Instance.rootGfx.transform;
var gfxObj = Instantiate(gfx, root);
gfxObj.SetActive(show);
2024-06-12 15:01:54 +08:00
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(PathConstants.GfxPath, gfxName));
2024-06-12 15:01:54 +08:00
}
}
public void Release()
{
foreach (var gfx in gfxDic)
{
if (gfx.Value != null)
{
DestroyImmediate(gfx.Value);
}
}
gfxDic = null;
}
}