85 lines
2.8 KiB
C#
85 lines
2.8 KiB
C#
using System.Collections.Generic;
|
|
using Cysharp.Threading.Tasks;
|
|
using PhxhSDK;
|
|
using UnityEngine;
|
|
namespace Gameplay.Effect
|
|
{
|
|
public class EffectOrderCacher
|
|
{
|
|
public Dictionary<string, GameObject> effectOrderCache = new Dictionary<string, GameObject>();
|
|
|
|
private int _currentOrder = -32768;
|
|
|
|
public static bool enableCache = false;
|
|
|
|
private Transform _rootTrans;
|
|
|
|
private HashSet<string> _loadingEffects = new HashSet<string>();
|
|
|
|
public EffectOrderCacher()
|
|
{
|
|
_rootTrans = new GameObject("[EffectOrderCacher]").transform;
|
|
GameObject gameObject;
|
|
(gameObject = _rootTrans.gameObject).SetActive(false);
|
|
Object.DontDestroyOnLoad(gameObject);
|
|
}
|
|
|
|
public async UniTask<GameObject> _LoadEffect(string prefabPath)
|
|
{
|
|
if (effectOrderCache.TryGetValue(prefabPath, out var effect))
|
|
{
|
|
return effect;
|
|
}
|
|
if (_loadingEffects.Contains(prefabPath))
|
|
{
|
|
// 正在加载中,等待加载完成
|
|
var cachePath = prefabPath;
|
|
await UniTask.WaitUntil(() => !_loadingEffects.Contains(cachePath));
|
|
if (effectOrderCache.TryGetValue(cachePath, out effect))
|
|
{
|
|
return effect;
|
|
}
|
|
|
|
// 这里是加载失败的情况
|
|
DebugUtil.LogError("特效加载失败:{0}", cachePath);
|
|
return null;
|
|
}
|
|
_loadingEffects.Add(prefabPath);
|
|
var prefab = await AssetManager.Instance.LoadAssetAsync<GameObject>(prefabPath);
|
|
prefab = Object.Instantiate(prefab, _rootTrans);
|
|
|
|
if (enableCache)
|
|
{
|
|
var paticleRenderers = prefab.GetComponentsInChildren<ParticleSystemRenderer>();
|
|
foreach (var paticleRenderer in paticleRenderers)
|
|
{
|
|
paticleRenderer.sortingOrder = _currentOrder;
|
|
_currentOrder++;
|
|
if (_currentOrder > 32767)
|
|
{
|
|
_currentOrder = -32768;
|
|
}
|
|
}
|
|
}
|
|
effectOrderCache.Add(prefabPath, prefab);
|
|
_loadingEffects.Remove(prefabPath);
|
|
return prefab;
|
|
}
|
|
|
|
public void Clear()
|
|
{
|
|
foreach (var kv in effectOrderCache)
|
|
{
|
|
if (kv.Value != null)
|
|
{
|
|
Object.Destroy(kv.Value);
|
|
}
|
|
AssetManager.Instance.Unload(kv.Key);
|
|
}
|
|
effectOrderCache.Clear();
|
|
_loadingEffects.Clear();
|
|
_currentOrder = -32768;
|
|
}
|
|
}
|
|
}
|