1
0
Fork 0
LaboratoryProtection/Assets/UnityTest/ZXL/Scripts/Test.cs

104 lines
2.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Threading;
using Cysharp.Threading.Tasks;
using DragonSoul.Shared;
using Sirenix.OdinInspector;
using UnityEditor;
using UnityEngine;
namespace UnityTest.ZXL
{
public class Test : SerializedMonoBehaviour
{
#region UnitaskTest
private async UniTask WaitTime()
{
await UniTask.Delay(TimeSpan.FromSeconds(3));
Debug.Log("等待了三秒");
}
private CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
private async UniTask<bool> WaitTime_CanCencel(float time, CancellationToken token)
{
try
{
UniTaskCompletionSource source = new UniTaskCompletionSource();
await source.Task;
source.TrySetResult();
Debug.Log("开始等待五秒");
await UniTask.Delay(TimeSpan.FromSeconds(time), DelayType.DeltaTime, PlayerLoopTiming.Update, token);
Debug.Log("等待了五秒");
return true;
}
catch (OperationCanceledException e)
{
Debug.Log("取消了等待");
return false;
}
}
private async ETTask ETTaskTest()
{
Debug.Log("开始等待五秒");
await UniTask.Delay(TimeSpan.FromSeconds(5));
Debug.Log("等待了五秒");
await ETTask.CompletedTask;
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.R))
{
WaitTime_CanCencel(5, _cancellationTokenSource.Token);
}
if (Input.GetKeyDown(KeyCode.E))
{
ETTaskTest().Coroutine();
}
if (Input.GetKeyDown(KeyCode.T))
{
Debug.Log("取消了");
_cancellationTokenSource.Cancel();
_cancellationTokenSource.Dispose();
}
}
#endregion
#if UNITY_EDITOR
public List<DialogueData> dialogueData;
[Button("AddData")]
void Add(DialogueData data)
{
this.dialogueData.Add(data);
}
[ReadOnly] [FolderPath] public string folderPath;
public string assetName;
[Button("Create Asset")]
void AA()
{
string path = $"{folderPath}/{assetName}.asset";
DialogueAsset dialogueAsset = ScriptableObject.CreateInstance<DialogueAsset>();
dialogueAsset.datas = dialogueData;
AssetDatabase.CreateAsset(dialogueAsset, path);
}
#endif
}
}