OneShotOneKill/Assets/Script/InGame/Mgr/EffectMgr.cs

129 lines
4.2 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EffectMgr : MonoBehaviourSingletonTemplate<EffectMgr>
{
Dictionary<string, List<EffectBase>> dic_effect = new Dictionary<string, List<EffectBase>>();
public bool LoadComplete;
protected override void Awake()
{
base.Awake();
DontDestroy();
}
public IEnumerator Co_Load(Action<float> addslidevalue)
{
LoadComplete = false;
var lst = table_effect.Ins.Get_DataList();
int totalCount = lst.Count;
// 슬라이더 35% 사용
float add1value = 0.35f / totalCount;
const int batchSize = 10; // 🔹 한 번에 로드할 개수
for (int batchStart = 0; batchStart < totalCount; batchStart += batchSize)
{
int batchEnd = Mathf.Min(batchStart + batchSize, totalCount);
// 🔹 이번 배치의 로드 코루틴 리스트
List<Coroutine> coroutines = new List<Coroutine>();
for (int i = batchStart; i < batchEnd; i++)
{
var data = lst[i];
if (!dic_effect.ContainsKey(data.s_Effect))
dic_effect.Add(data.s_Effect, new List<EffectBase>());
// 🔹 병렬 실행용 코루틴 등록
coroutines.Add(StartCoroutine(LoadEffect(data, add1value, addslidevalue)));
}
// 🔹 배치에 포함된 모든 로드가 끝날 때까지 대기
foreach (var c in coroutines)
yield return c;
// 🔹 한 배치 끝나면 잠깐 쉬어주기 (UI 멈춤 방지)
yield return null;
}
LoadComplete = true;
}
private IEnumerator LoadEffect(EffectTableData data, float add1value, Action<float> addslidevalue)
{
bool done = false;
yield return AddrResourceMgr.Ins.LoadObjectSequential<GameObject>(
$"Ingame/Effect/{data.s_Effect}.prefab",
prefab =>
{
for (int k = 0; k < data.n_LoadAmount; k++)
{
var eff = DSUtil.Get_Clone<EffectBase>(prefab, transform);
#if UNITY_EDITOR
if (eff == null)
MyEditorDialog.Show_Dialog($"{data.s_Effect} 이펙트에 EffectBase 스크립트가 없습니다.", null);
#endif
dic_effect[data.s_Effect].Add(eff);
eff.Off();
}
addslidevalue?.Invoke(add1value);
done = true;
});
yield return new WaitUntil(() => done);
}
EffectBase Get_Effect(string effect)
{
if (dic_effect.ContainsKey(effect))
{
for (int i = 0; i < dic_effect[effect].Count; i++)
{
if (!dic_effect[effect][i].isActiveAndEnabled)
return dic_effect[effect][i];
}
var neweffect = DSUtil.Get_Clone<EffectBase>(dic_effect[effect][0].gameObject, transform);
neweffect.gameObject.SetActive(false);
neweffect.transform.localScale = dic_effect[effect][0].transform.localScale;
dic_effect[effect].Add(neweffect);
return neweffect;
}
else
Popup.Ins.Set($"{effect} 이펙트가 없습니다. 테이블을 확인해 주세요.");
return null;
}
public EffectBase Get_EffectBase(string effect) { return Get_Effect(effect); }
public void Show_Effect(string effect, Vector3 pos, float offtime = 2f)
{
var effectobj = Get_Effect(effect);
if (effectobj != null) effectobj.Set(effect, pos, offtime);
}
public EffectBase Show_FollowEffect(string effect, eEffectPivot pivot, Actor actor, Vector3 pos, float offtime = 2f)
{
var effectobj = Get_Effect(effect);
if (effectobj != null) effectobj.Set(effect, pivot, actor, pos, offtime);
return effectobj;
}
public void AllOff()
{
#if UNITY_EDITOR
if (MyValue.m_MyStageData.m_EnterType == eStageEnterType.Tool || MyValue.m_MyStageData.m_EnterType == eStageEnterType.Tool_Mob)
return;
#endif
foreach (var item in dic_effect)
for (int i = 0; i < item.Value.Count; i++)
item.Value[i].Off();
}
}