2023-06-05 19:57:17 +08:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
using Cysharp.Threading.Tasks;
|
|
|
|
using System;
|
|
|
|
using System.Threading;
|
|
|
|
using UnityEngine.Events;
|
|
|
|
using UnityEngine.UIElements;
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
using UnityEditor;
|
|
|
|
using UnityEditor.UIElements;
|
|
|
|
#endif
|
|
|
|
namespace BITKit
|
|
|
|
{
|
|
|
|
[System.Serializable]
|
|
|
|
public struct InvokeMonoAction : IAction
|
|
|
|
{
|
|
|
|
public MonoBehaviour monoBehaviour;
|
2023-06-29 14:57:11 +08:00
|
|
|
public void Execute()
|
2023-06-05 19:57:17 +08:00
|
|
|
{
|
2023-06-29 14:57:11 +08:00
|
|
|
(monoBehaviour as IAction).Execute();
|
2023-06-05 19:57:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
[System.Serializable]
|
|
|
|
public struct InvokeUnityEvent : IAction
|
|
|
|
{
|
|
|
|
public UnityEvent unityEvent;
|
2023-06-29 14:57:11 +08:00
|
|
|
public void Execute()
|
2023-06-05 19:57:17 +08:00
|
|
|
{
|
|
|
|
unityEvent.Invoke();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
[System.Serializable]
|
|
|
|
public struct ExitApplation : IAction
|
|
|
|
{
|
2023-06-29 14:57:11 +08:00
|
|
|
public void Execute()
|
2023-06-05 19:57:17 +08:00
|
|
|
{
|
|
|
|
BITAppForUnity.Exit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
[System.Serializable]
|
|
|
|
public struct DebugText : IAction
|
|
|
|
{
|
|
|
|
[SerializeReference, SubclassSelector] public References text;
|
|
|
|
|
2023-06-29 14:57:11 +08:00
|
|
|
public void Execute()
|
2023-06-05 19:57:17 +08:00
|
|
|
{
|
|
|
|
Debug.Log(text);
|
|
|
|
}
|
|
|
|
}
|
2023-06-29 14:57:11 +08:00
|
|
|
|
|
|
|
[System.Serializable]
|
|
|
|
public class MonoActionProxy : IAction
|
|
|
|
{
|
|
|
|
[SerializeField]private MonoBehaviour monoBehaviour;
|
|
|
|
public void Execute()
|
|
|
|
{
|
|
|
|
((IAction)monoBehaviour).Execute();
|
|
|
|
}
|
|
|
|
}
|
2023-06-05 19:57:17 +08:00
|
|
|
public class MonoAction : MonoBehaviour, IAction
|
|
|
|
{
|
|
|
|
[SerializeReference, SubclassSelector] public List<IAction> actions;
|
|
|
|
CancellationToken cancellationToken;
|
|
|
|
void Start()
|
|
|
|
{
|
|
|
|
cancellationToken = gameObject.GetCancellationTokenOnDestroy();
|
|
|
|
}
|
2023-06-29 14:57:11 +08:00
|
|
|
public async void Execute()
|
2023-06-05 19:57:17 +08:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
await UniTask.SwitchToMainThread(cancellationToken);
|
|
|
|
foreach (var x in actions)
|
|
|
|
{
|
2023-06-29 14:57:11 +08:00
|
|
|
x.Execute();
|
2023-06-05 19:57:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
catch (System.Exception)
|
|
|
|
{
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
[CustomEditor(typeof(MonoAction))]
|
|
|
|
public class MonoActinoInspector : BITInspector<MonoAction>
|
|
|
|
{
|
|
|
|
public override VisualElement CreateInspectorGUI()
|
|
|
|
{
|
|
|
|
CreateSubTitle("Settings");
|
|
|
|
var actions = root.Create<PropertyField>();
|
|
|
|
var button = root.Create<Button>();
|
|
|
|
actions.bindingPath = nameof(MonoAction.actions);
|
|
|
|
|
|
|
|
button.text = "Excute";
|
|
|
|
|
2023-06-29 14:57:11 +08:00
|
|
|
button.clicked += agent.Execute;
|
2023-06-05 19:57:17 +08:00
|
|
|
return root;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|