1
0
Fork 0
LaboratoryProtection/Assets/PMaker/Scripts/Await/AwaitBehaviour.cs

254 lines
7.0 KiB
C#

using Sirenix.OdinInspector;
using System.Threading;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using TMPro;
using Object = UnityEngine.Object;
namespace PMaker.Await
{
public abstract partial class AwaitBehaviour : BaseBehaviour
{
public bool isFinish = false;
public abstract UniTask WaitAsync(CancellationToken cancellationToken = default);
protected async UniTask WaitTrue(CancellationToken cancellationToken = default)
{
await UniTask.WaitUntil(() => this.isFinish == true, cancellationToken: cancellationToken);
}
protected async UniTask WaitFalse(CancellationToken cancellationToken = default)
{
await UniTask.WaitUntil(() => this.isFinish == false, cancellationToken: cancellationToken);
}
protected void SetTrue()
{
isFinish = true;
}
protected void SetFalse()
{
isFinish = false;
}
}
#if UNITY_EDITOR
public partial class AwaitBehaviour
{
[Button]
[HideInEditorMode]
private async UniTask WaitAsyncTest()
{
await this.WaitAsync(this.GetCancellationTokenOnDestroy());
}
}
#endif
public abstract partial class AwaitBehaviour
{
protected virtual void CheObjActive<T>(T obj, bool isOpen = false) where T : Object
{
if (obj != null)
{
var o = obj as Component;
o.gameObject.SetActive(isOpen);
return;
}
Debug.LogWarning($"ChangeObj is null");
}
/// <summary>
/// 改变Obj的状态 che为change
/// </summary>
/// <param name="item"></param>
/// <param name="isOpen"></param>
protected virtual void CheObjActive(GameObject item, bool isOpen = false)
{
if (item != null)
{
item.SetActive(isOpen);
return;
}
Debug.LogWarning($"ChangeGameObj is null");
}
/// <summary>
/// 改变Obj数组的状态 che为change
/// </summary>
/// <param name="item"></param>
/// <param name="isOpen"></param>
protected virtual void CheGameObjActive(GameObject[] item, bool isOpen = false)
{
for (int i = 0; i < item.Length; i++)
{
if (item[i] != null)
{
item[i].SetActive(isOpen);
}
}
}
/// <summary>
/// 改变Obj数组中的单个状态 che为change
/// </summary>
/// <param name="item"></param>
/// <param name="index"></param>
protected virtual void CheObjListOneActive(GameObject[] item, int index, bool isOpen = false)
{
for (int i = 0; i < item.Length; i++)
{
if (index.Equals(i))
{
item[i].SetActive(isOpen);
return;
}
}
}
/// <summary>
/// 除了传入的值激活其他都失活
/// </summary>
/// <param name="item"></param>
/// <param name="index"></param>
/// <param name="isOpen"></param>
protected void CheObjListAllActive<T>(List<T> item, int index = -1, bool isOpen = false) where T : Object
{
if (item.Count < 0)
{
return;
}
for (int i = 0; i < item.Count; i++)
{
var obj = item[i] as Component;
if (obj == null) continue;
if (index.Equals(i))
{
obj.gameObject.SetActive(isOpen);
}
else
{
obj.gameObject.SetActive(!isOpen);
}
}
}
/// <summary>
/// 除了传入的值激活其他都失活
/// </summary>
/// <param name="item"></param>
/// <param name="index"></param>
/// <param name="isOpen"></param>
protected void CheObjListAllActive<T>(List<T> item, int index = -1, int count = 0, bool isOpen = false)
where T : Object
{
if (item.Count < 0)
{
return;
}
for (int i = 0; i < item.Count; i++)
{
var obj = item[i] as Component;
if (obj == null) continue;
if (i < count)
{
if (index.Equals(i))
{
obj.gameObject.SetActive(isOpen);
}
}
else
{
obj.gameObject.SetActive(!isOpen);
}
}
}
/// <summary>
/// 除了传入的值激活其他都失活
/// </summary>
/// <param name="item"></param>
/// <param name="index"></param>
/// <param name="isOpen"></param>
protected void CheObjListAllActive(List<GameObject> item, int index = -1, bool isOpen = false)
{
if (item.Count < 0)
{
return;
}
for (int i = 0; i < item.Count; i++)
{
if (item[i] is null) continue;
if (index.Equals(i))
{
item[i].SetActive(isOpen);
}
else
{
item[i].SetActive(!isOpen);
}
}
}
public void DoTexts(Text text, string context = "", int time = 0, int speed = 0)
{
text.DOText(context, time).SetEase(Ease.Linear);
}
public void DoTMPTexts(TextMeshProUGUI text, string context = "", float time = 0, int speed = 0)
{
DOTween.To(() => string.Empty, value => text.text = value, context, time).SetEase(Ease.Linear);
}
/// <summary>
/// 除了传入的值激活其他都失活
/// </summary>
/// <param name="item"></param>
/// <param name="index"></param>
/// <param name="count"></param>
/// <param name="isOpen"></param>
protected void CheObjListAllActive(List<GameObject> item, int index = -1, int count = 0, bool isOpen = false)
{
if (item.Count < 0)
{
return;
}
for (int i = 0; i < item.Count; i++)
{
if (i < count)
{
if (item[i] is null) continue;
if (index.Equals(i))
{
item[i].SetActive(isOpen);
}
else
{
item[i].SetActive(!isOpen);
}
}
}
}
}
}