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

210 lines
6.1 KiB
C#

using System;
using System.Collections.Generic;
using DragonSoul.Shared;
using Mono.Event;
using Sirenix.OdinInspector;
using Sirenix.Serialization;
using UnityEngine;
using UnityEngine.Playables;
namespace UnityTest.ZXL
{
public abstract class ProcessBase : SerializedMonoBehaviour
{
public abstract ProcessType processType { get; }
// public GameObject go;
[OdinSerialize] protected List<PlayableDirector> playableDirectors;
private PlayableDirector currentPlayableDirector;
#region Task
protected ETTask<bool> currentTask;
protected ETCancellationToken currentToken;
protected void TokenCancel()
{
currentTask?.SetResult(false);
currentToken.Remove(TokenCancel);
}
#endregion
public PlayableDirector CurrentPlayableDirector
{
set
{
if (currentPlayableDirector != null && currentPlayableDirector == value)
return;
currentPlayableDirector = value;
}
get
{
if (currentPlayableDirector == null)
{
currentPlayableDirector = playableDirectors[0];
}
return currentPlayableDirector;
}
}
protected int index;
#if UNITY_EDITOR
[Button]
void Add(GameObject go)
{
var list = go.GetComponentAllChild<PlayableDirector>();
playableDirectors = new List<PlayableDirector>(list);
}
#endif
protected virtual void OnInit()
{
EventManager.Instance.Subscribe(ConstEventArgs.EventId, ConstEventLogic);
EventManager.Instance.Subscribe(ClickObjectEventArgs.EventId, ClickObjectEvent);
EventManager.Instance.Subscribe(PlayableStoppedEventArgs.EventId, PlayableStoppedEvent);
foreach (var playableDirector in playableDirectors)
{
playableDirector.gameObject.SetActive(false);
}
this.index = 0;
}
protected virtual void OnUpdate()
{
}
protected virtual void OnLevel()
{
EventManager.Instance.Unsubscribe(ConstEventArgs.EventId, ConstEventLogic);
EventManager.Instance.Unsubscribe(ClickObjectEventArgs.EventId, ClickObjectEvent);
EventManager.Instance.Unsubscribe(PlayableStoppedEventArgs.EventId, PlayableStoppedEvent);
}
protected virtual void Next()
{
foreach (var playableDirector in playableDirectors)
{
playableDirector.gameObject.SetActive(false);
}
index++;
AnimatorManager.Instance().HideAll();
}
// protected abstract void ProcessLogic();
public virtual void ConstEventLogic(object sender, GameEventArgs e)
{
var args = e as ConstEventArgs;
Debug.Log($"接收到抛出的事件{args.constDataType}");
}
public virtual void PlayableStoppedEvent(object sender, GameEventArgs e)
{
var args = e as PlayableStoppedEventArgs;
Next();
}
public virtual void ClickObjectEvent(object sender, GameEventArgs e)
{
}
private void OnEnable()
{
ETTask.ExceptionHandler -= ExceptionHandler;
ETTask.ExceptionHandler += ExceptionHandler;
OnInit();
}
private void ExceptionHandler(Exception obj)
{
UnityEngine.Debug.LogError(obj.ToString());
}
private void Update()
{
OnUpdate();
#if UNITY_EDITOR
if (Input.GetKeyDown(KeyCode.P))
{
Debug.Log("结束当前流程");
EventManager.Instance.FireNow(this, new ProcessOverEventArgs(processType));
}
#endif
}
private void OnDisable()
{
OnLevel();
}
/// <summary>
/// 打开高亮物体并等待点击后恢复timeline播放
/// </summary>
/// <param name="highlightObjectType"></param>
/// <param name="isAutoResume"></param>
protected async ETTask WaitOperateFinish_ClickHighlight(HighlightObjectType highlightObjectType, bool isAutoResume = true)
{
currentTask = ETTask<bool>.Create();
currentToken = new ETCancellationToken();
currentToken.Add(TokenCancel);
ObjectDataComponent.Instance().GetObject(highlightObjectType).HighlightOpen();
await currentTask;
if (isAutoResume)
this.SetPlayDirector(CurrentPlayableDirector, PlayTimelineMode.Resume);
}
/// <summary>
/// 等待点击后恢复timeline播放
/// </summary>
/// <param name="isAutoResume"></param>
protected async ETTask<bool> WaitOperateFinish_Click(bool isAutoResume = true)
{
currentTask = ETTask<bool>.Create();
currentToken = new ETCancellationToken();
currentToken.Add(TokenCancel);
bool result = await currentTask;
if (isAutoResume)
this.SetPlayDirector(CurrentPlayableDirector, PlayTimelineMode.Resume);
return result;
}
/// <summary>
/// 打开UI并等待点击后恢复timeline播放
/// </summary>
/// <param name="uiType"></param>
/// <param name="isAutoResume"></param>
protected async ETTask WaitOperateFinish_ClickUI(UIType uiType, bool isAutoResume = true)
{
currentTask = ETTask<bool>.Create();
currentToken = new ETCancellationToken();
currentToken.Add(TokenCancel);
UIManager.Instance().ShowUI(uiType);
await currentTask;
UIManager.Instance().HideUI(uiType);
if (isAutoResume)
this.SetPlayDirector(CurrentPlayableDirector, PlayTimelineMode.Resume);
}
}
public enum ProcessType
{
_ = 0,
_,
_,
_,
}
}