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

175 lines
5.7 KiB
C#
Raw Normal View History

2023-09-12 23:18:01 +08:00
using System;
using System.Collections.Generic;
2023-09-14 15:36:17 +08:00
using System.Threading;
using Cysharp.Threading.Tasks;
2023-09-12 23:18:01 +08:00
using HighlightPlus;
using UnityEngine;
using UnityEngine.Playables;
2023-09-12 23:18:01 +08:00
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>();
2023-09-18 01:37:18 +08:00
var load = Resources.Load<HighlightProfile>("Highlight Plus Profile");
highlightEffect.ProfileLoad(load);
2023-09-12 23:18:01 +08:00
highlightEffect.highlighted = true;
2023-09-13 20:15:22 +08:00
self.GetComponent<ObjectComponent>().isCanClick = true;
2023-09-12 23:18:01 +08:00
}
/// <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");
2023-09-13 09:28:54 +08:00
action?.Invoke();
}
2023-09-14 15:36:17 +08:00
public static async UniTask WaitAnimatorPlayOver(this Animator animator, string clipName)
2023-09-13 09:28:54 +08:00
{
bool isOver = false;
2023-09-14 15:36:17 +08:00
AnimatorStateInfo animatorInfo;
animator.Play(clipName);
Debug.Log($"Start Play {clipName} Clip");
2023-09-13 09:28:54 +08:00
while (!isOver)
{
2023-09-14 15:36:17 +08:00
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); //播放完成后回到待机动画
2023-09-13 09:28:54 +08:00
isOver = true;
2023-09-14 15:36:17 +08:00
}
2023-09-13 09:28:54 +08:00
}
2023-09-14 15:36:17 +08:00
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));
2023-09-13 09:28:54 +08:00
}
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);
2023-09-12 23:18:01 +08:00
}
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
2023-09-12 23:18:01 +08:00
}
}