using System;
using System.Collections.Generic;
using HighlightPlus;
using UnityEngine;
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();
highlightEffect.highlighted = 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");
}
}
}