Files
2026-02-21 16:45:37 +08:00

101 lines
1.9 KiB
C#

using Crosstales.Radio.Util;
using UnityEngine;
namespace Crosstales.Radio.Tool
{
[HelpURLAttribute("https://www.crosstales.com/media/data/assets/radio/api/class_crosstales_1_1_radio_1_1_tool_1_1_internet_check.html")]
[ExecuteInEditMode]
public class CrossFader : MonoBehaviour
{
[Tooltip("Audio source A (e.g. left) to fade.")]
public AudioSource SourceA;
[Tooltip("Audio source B (e.g. right) to fade.")]
public AudioSource SourceB;
private float volumeA = 1f;
private float volumeB = 1f;
public float FaderPosition
{
get
{
float num = ((!(SourceA != null)) ? 1f : SourceA.volume) / volumeA;
float num2 = ((!(SourceB != null)) ? 1f : SourceB.volume) / volumeB;
float num3 = 0f;
float num4 = 0f;
if (num < 1f)
{
num3 = 1f - num;
}
if (num2 < 1f)
{
num4 = 0f - (1f - num2);
}
if (num3 > 0f && !(num4 < 0f))
{
return num3;
}
if (!(num3 > 0f) && num4 < 0f)
{
return num4;
}
return 0f;
}
set
{
float num = Mathf.Clamp(value, -1f, 1f);
if (num > 0f)
{
float num2 = 0f - (num - 1f);
if (SourceA != null)
{
SourceA.volume = volumeA * num2;
}
if (SourceB != null)
{
SourceB.volume = volumeB;
}
}
else if (num < 0f)
{
float num3 = 1f - (0f - num);
if (SourceA != null)
{
SourceA.volume = volumeA;
}
if (SourceB != null)
{
SourceB.volume = volumeB * num3;
}
}
else
{
if (SourceA != null)
{
SourceA.volume = volumeA;
}
if (SourceB != null)
{
SourceB.volume = volumeB;
}
}
}
}
public void Start()
{
if (SourceA != null && SourceB != null && SourceA != SourceB)
{
volumeA = SourceA.volume;
volumeB = SourceB.volume;
}
else if (!Helper.isEditorMode)
{
Debug.LogError("'SourceA' or 'SourceB' are null or equals!");
}
}
}
}