1
This commit is contained in:
99
Unity/Scripts/Mono/MonoAction.cs
Normal file
99
Unity/Scripts/Mono/MonoAction.cs
Normal file
@@ -0,0 +1,99 @@
|
||||
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;
|
||||
public void Excute()
|
||||
{
|
||||
(monoBehaviour as IAction).Excute();
|
||||
}
|
||||
}
|
||||
[System.Serializable]
|
||||
public struct InvokeUnityEvent : IAction
|
||||
{
|
||||
public UnityEvent unityEvent;
|
||||
public void Excute()
|
||||
{
|
||||
unityEvent.Invoke();
|
||||
}
|
||||
}
|
||||
[System.Serializable]
|
||||
public struct ExitApplation : IAction
|
||||
{
|
||||
public void Excute()
|
||||
{
|
||||
BITAppForUnity.Exit();
|
||||
}
|
||||
}
|
||||
[System.Serializable]
|
||||
public struct DebugText : IAction
|
||||
{
|
||||
[SerializeReference, SubclassSelector] public References text;
|
||||
|
||||
public void Excute()
|
||||
{
|
||||
Debug.Log(text);
|
||||
}
|
||||
}
|
||||
public class MonoAction : MonoBehaviour, IAction
|
||||
{
|
||||
[SerializeReference, SubclassSelector] public List<IAction> actions;
|
||||
CancellationToken cancellationToken;
|
||||
void Start()
|
||||
{
|
||||
cancellationToken = gameObject.GetCancellationTokenOnDestroy();
|
||||
}
|
||||
public async void Excute()
|
||||
{
|
||||
try
|
||||
{
|
||||
await UniTask.SwitchToMainThread(cancellationToken);
|
||||
foreach (var x in actions)
|
||||
{
|
||||
x.Excute();
|
||||
}
|
||||
}
|
||||
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";
|
||||
|
||||
button.clicked += agent.Excute;
|
||||
return root;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
Reference in New Issue
Block a user