LaboratoryProtection/Assets/UnityTest/ZXL/Scripts/Process/ProcessManager.cs

124 lines
3.7 KiB
C#

using System;
using System.Collections.Generic;
using DragonSoul.Shared;
using Mono.Event;
using PMaker.MessagePipe;
using Sirenix.OdinInspector;
using Sirenix.Serialization;
using UnityEditor;
using UnityEngine;
namespace UnityTest.ZXL
{
public class ProcessManager : SerializedMonoBehaviour
{
[ShowInInspector] [ReadOnly] private ProcessBase currentProcess;
private ETCancellationToken _token;
[OdinSerialize] private Dictionary<ProcessType, ProcessBase> processGos = new Dictionary<ProcessType, ProcessBase>();
private int processIndex;
[ReadOnly] public string hhh = "**************************************";
[LabelText("初始启动流程")] public ProcessType startProcessType;
private void Awake()
{
EventManager.Instance.Subscribe(ProcessOverEventArgs.EventId, PlayableStoppedEvent);
// SetProcess(ProcessType.一_爆炸发生场景);
// processIndex = 0;
SetProcess(startProcessType);
}
private void OnDestroy()
{
EventManager.Instance.Unsubscribe(ProcessOverEventArgs.EventId, PlayableStoppedEvent);
}
private void PlayableStoppedEvent(object sender, GameEventArgs e)
{
var args = e as ProcessOverEventArgs;
ChangeProcess();
}
void ChangeProcess()
{
processIndex++;
ProcessType processType;
if (processIndex == 0)
{
processType = ProcessType._;
}
else if (processIndex == 1)
{
processType = ProcessType._;
}
else if (processIndex == 2)
{
processType = ProcessType._;
}
else if (processIndex == 3)
{
processType = ProcessType._;
}
else
{
// 完成四个模块的任务了
Debug.Log("完成四个模块的任务了");
MessageKit.GetPublisher<string>().Publish("返回类型");
return;
}
Debug.Log($"{processType}");
_token = new ETCancellationToken();
processGos.TryGetValue(processType, out ProcessBase processBase);
foreach (var processGosValue in processGos.Values)
{
processGosValue.gameObject.SetActive(false);
}
currentProcess = processBase;
if (currentProcess != null) currentProcess.gameObject.SetActive(true);
}
void SetProcess(ProcessType processType)
{
Debug.Log($"{processType}");
processIndex = (int)startProcessType;
_token = new ETCancellationToken();
processGos.TryGetValue(processType, out ProcessBase processBase);
foreach (var processGosValue in processGos.Values)
{
processGosValue.gameObject.SetActive(false);
}
currentProcess = processBase;
if (currentProcess != null) currentProcess.gameObject.SetActive(true);
}
#if UNITY_EDITOR
[Button]
void Add()
{
processGos = new Dictionary<ProcessType, ProcessBase>();
for (var i = 0; i < transform.childCount; i++)
{
var tran = transform.GetChild(i);
var processBase = tran.GetComponent<ProcessBase>();
processGos.Add(processBase.processType, processBase);
}
UnityEditor.EditorUtility.SetDirty(this);
AssetDatabase.SaveAssets();
}
#endif
}
}