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

92 lines
2.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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
{
/// <summary>
/// 获取子物体所有具有某个组件的物体
/// </summary>
/// <param name="self"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static List<T> GetComponentAllChild<T>(this GameObject self) where T : Object
{
List<T> list = new List<T>();
Get<T>(self, ref list);
return list;
}
private static void Get<T>(GameObject go, ref List<T> 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<T>(goTransform.GetChild(i).gameObject, ref list);
}
}
/// <summary>
/// 高亮开启
/// </summary>
/// <param name="self"></param>
public static void HighlightOpen(this GameObject self)
{
var highlightEffect = self.GetOrAddComponent<HighlightEffect>();
highlightEffect.highlighted = true;
}
/// <summary>
/// 高亮关闭
/// </summary>
/// <param name="self"></param>
public static void HighlightOff(this GameObject self)
{
if (!self.TryGetComponent(out HighlightEffect highlightEffect))
Debug.LogError($"{self.name} dont have HighlightEffect Component !!!!");
highlightEffect.highlighted = false;
}
/// <summary>
/// 等待动画播放结束后执行回调方法
/// </summary>
/// <param name="animator"></param>
/// <param name="clipName"></param>
/// <param name="action"></param>
/// <returns></returns>
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");
}
}
}