using System;
using System.Collections.Generic;
using System.Threading;
using Cysharp.Threading.Tasks;
using HighlightPlus;
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.UI.Extensions;
using Object = UnityEngine.Object;
namespace UnityTest.ZXL
{
public static class CommonHelper
{
///
/// 获取子物体所有具有某个组件的物体
///
///
///
///
public static List GetComponentAllChild(this GameObject self) where T : Object
{
List list = new List();
Get(self, ref list);
return list;
}
private static void Get(GameObject go, ref List list) where T : Object
{
if (go.TryGetComponent(out T t))
{
list.Add(t);
}
var goTransform = go.transform;
for (var i = 0; i < goTransform.childCount; i++)
{
Get(goTransform.GetChild(i).gameObject, ref list);
}
}
///
/// 高亮开启
///
///
public static void HighlightOpen(this GameObject self)
{
var highlightEffect = self.GetOrAddComponent();
var load = Resources.Load("Highlight Plus Profile");
highlightEffect.ProfileLoad(load);
highlightEffect.highlighted = true;
self.GetComponent().isCanClick = true;
}
///
/// 高亮关闭
///
///
public static void HighlightOff(this GameObject self)
{
if (!self.TryGetComponent(out HighlightEffect highlightEffect))
Debug.LogError($"{self.name} dont have HighlightEffect Component !!!!");
highlightEffect.highlighted = false;
}
///
/// 等待动画播放结束后执行回调方法
///
///
///
///
///
public static System.Collections.IEnumerator WaitAnimatorPlayOver(this Animator animator, string clipName, Action action)
{
bool isOver = false;
AnimatorStateInfo animatorInfo;
animator.Play(clipName);
Debug.Log($"Start Play {clipName} Clip");
while (!isOver)
{
yield return null;
animatorInfo = animator.GetCurrentAnimatorStateInfo(0); //必须放在update里
if ((animatorInfo.normalizedTime > 1.0f) && (animatorInfo.IsName(clipName))) //normalizedTime: 范围0 -- 1, 0是动作开始,1是动作结束
{
animator.SetInteger(clipName, 0); //播放完成后回到待机动画
isOver = true;
}
}
Debug.Log($"End Play {clipName} Clip");
action?.Invoke();
}
public static async UniTask WaitAnimatorPlayOver(this Animator animator, string clipName)
{
bool isOver = false;
AnimatorStateInfo animatorInfo;
animator.Play(clipName);
Debug.Log($"Start Play {clipName} Clip");
while (!isOver)
{
await UniTask.NextFrame();
animatorInfo = animator.GetCurrentAnimatorStateInfo(0); //必须放在update里
if ((animatorInfo.normalizedTime > 1.0f) && (animatorInfo.IsName(clipName))) //normalizedTime: 范围0 -- 1, 0是动作开始,1是动作结束
{
animator.SetInteger(clipName, 0); //播放完成后回到待机动画
isOver = true;
}
}
Debug.Log($"End Play {clipName} Clip");
}
///
/// 等待时间
///
///
public static async UniTask WaitTime(float time)
{
await UniTask.Delay(TimeSpan.FromSeconds(time));
}
public static void ShowOrHideObject(this GameObject self, bool isShow)
{
self.SetActive(isShow);
}
public static void ShowOrHideObject(this Transform self, bool isShow)
{
self.gameObject.SetActive(isShow);
}
public static void SetPlayDirector(this ProcessBase processBase, PlayableDirector playableDirector, PlayTimelineMode playMode)
{
processBase.CurrentPlayableDirector = playableDirector;
switch (playMode)
{
case PlayTimelineMode.Play:
playableDirector.gameObject.SetActive(true);
playableDirector.Play();
break;
case PlayTimelineMode.Pause:
playableDirector.Pause();
break;
case PlayTimelineMode.Resume:
playableDirector.Resume();
break;
case PlayTimelineMode.Stop:
playableDirector.Stop();
playableDirector.gameObject.SetActive(false);
break;
default:
throw new ArgumentOutOfRangeException(nameof(playMode), playMode, null);
}
}
}
///
/// 播放模式
///
public enum PlayTimelineMode
{
Play,
Pause,
Resume,
Stop
}
}