com.alicizax.unity.framework/Runtime/ABase/Extension/UnityEngine.Common/UnityEngine.CommonExtensions.cs

149 lines
3.7 KiB
C#
Raw Normal View History

2026-03-23 19:10:57 +08:00
using AlicizaX;
using UnityEngine.UI;
namespace UnityEngine
{
[UnityEngine.Scripting.Preserve]
public static class ColorExtensions
{
public static Color Alpha(this Color color, float alpha)
{
color.a = alpha;
return color;
}
public static Color Lightness(this Color color, float lightness)
{
Color.RGBToHSV(color, out var hue, out var saturation, out var _);
return Color.HSVToRGB(hue, saturation, lightness);
}
}
[UnityEngine.Scripting.Preserve]
public static class ImageExtensions
{
public static void Alpha(this Image image, float alpha)
{
Color color = image.color;
color.a = alpha;
image.color = color;
}
}
[UnityEngine.Scripting.Preserve]
public static class AudioSourceExtensions
{
public static void SetSoundClip(this AudioSource audioSource, SoundClip soundClip, float volumeMul = 1f, bool play = false)
{
if (soundClip == null || soundClip.audioClip == null || audioSource == null)
{
return;
}
if (audioSource.clip != soundClip.audioClip)
{
audioSource.clip = soundClip.audioClip;
}
audioSource.volume = soundClip.volume * volumeMul;
if (play && !audioSource.isPlaying)
{
audioSource.Play();
}
}
public static void PlayOneShotSoundClip(this AudioSource audioSource, SoundClip soundClip, float volumeMul = 1f)
{
if (soundClip == null || soundClip.audioClip == null || audioSource == null)
{
return;
}
audioSource.PlayOneShot(soundClip.audioClip, soundClip.volume * volumeMul);
}
}
[UnityEngine.Scripting.Preserve]
public static class LayerMaskExtensions
{
public static bool CompareLayer(this LayerMask layerMask, int layer)
{
return layerMask == (layerMask | (1 << layer));
}
}
[UnityEngine.Scripting.Preserve]
public static class AnimatorExtensions
{
public static bool IsAnyPlaying(this Animator animator)
{
AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(0);
return (stateInfo.length + 0.1f > stateInfo.normalizedTime || animator.IsInTransition(0)) && !stateInfo.IsName("Default");
}
}
[UnityEngine.Scripting.Preserve]
public static class AngleExtensions
{
public static float FixAngle(this float angle, float min, float max)
{
if (angle < min)
{
angle += 360f;
}
if (angle > max)
{
angle -= 360f;
}
return angle;
}
public static float FixAngle180(this float angle)
{
if (angle < -180f)
{
angle += 360f;
}
if (angle > 180f)
{
angle -= 360f;
}
return angle;
}
public static float FixAngle(this float angle)
{
if (angle < -360f)
{
angle += 360f;
}
if (angle > 360f)
{
angle -= 360f;
}
return angle;
}
public static float FixAngle360(this float angle)
{
if (angle < 0f)
{
angle += 360f;
}
if (angle > 360f)
{
angle -= 360f;
}
return angle;
}
}
}