56 lines
1.7 KiB
C#
56 lines
1.7 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 = true;
|
|
|
|
private Transform _rootTrans;
|
|
|
|
public EffectOrderCacher()
|
|
{
|
|
_rootTrans = new GameObject("[EffectOrderCacher]").transform;
|
|
_rootTrans.gameObject.SetActive(false);
|
|
}
|
|
|
|
public async UniTask<GameObject> _LoadEffect(string prefabPath)
|
|
{
|
|
if (effectOrderCache.TryGetValue(prefabPath, out var effect))
|
|
{
|
|
return effect;
|
|
}
|
|
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);
|
|
return prefab;
|
|
}
|
|
|
|
public void Clear()
|
|
{
|
|
effectOrderCache.Clear();
|
|
_currentOrder = -32768;
|
|
}
|
|
}
|
|
}
|