forked from zxl/LaboratoryProtection
175 lines
5.7 KiB
C#
175 lines
5.7 KiB
C#
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
|
||
{
|
||
/// <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>();
|
||
var load = Resources.Load<HighlightProfile>("Highlight Plus Profile");
|
||
highlightEffect.ProfileLoad(load);
|
||
highlightEffect.highlighted = true;
|
||
self.GetComponent<ObjectComponent>().isCanClick = 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");
|
||
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");
|
||
}
|
||
|
||
/// <summary>
|
||
/// 等待时间
|
||
/// </summary>
|
||
/// <param name="time"></param>
|
||
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);
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 播放模式
|
||
/// </summary>
|
||
public enum PlayTimelineMode
|
||
{
|
||
Play,
|
||
Pause,
|
||
Resume,
|
||
Stop
|
||
}
|
||
} |