OldBlueWater/BlueWater/Assets/Distant Lands/Cozy Weather/Contents/Scripts/Data/FX/AudioFX.cs

132 lines
3.9 KiB
C#
Raw Normal View History

2023-09-13 05:07:40 +00:00
// Distant Lands 2022.
using System.Collections;
using UnityEngine;
2024-01-03 06:34:33 +00:00
using UnityEngine.Audio;
2023-09-13 05:07:40 +00:00
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace DistantLands.Cozy.Data
{
[System.Serializable]
[CreateAssetMenu(menuName = "Distant Lands/Cozy/FX/Audio FX", order = 361)]
public class AudioFX : FXProfile
{
public AudioClip clip;
2024-01-03 06:34:33 +00:00
[Tooltip("The audio mixer group that the COZY weather audio FX will use.")]
public AudioMixerGroup audioMixer;
2023-09-13 05:07:40 +00:00
private AudioSource runtimeRef;
public float maximumVolume = 1;
2024-01-03 06:34:33 +00:00
public override void PlayEffect(float weight)
2023-09-13 05:07:40 +00:00
{
if (!runtimeRef)
2024-01-03 06:34:33 +00:00
if (InitializeEffect(weatherSphere) == false)
2023-09-13 05:07:40 +00:00
return;
2024-01-03 06:34:33 +00:00
if (weight == 0)
2023-09-13 05:07:40 +00:00
{
2024-01-03 06:34:33 +00:00
runtimeRef.volume = 0;
runtimeRef.Stop();
2023-09-13 05:07:40 +00:00
return;
}
if (!runtimeRef.isPlaying && runtimeRef.isActiveAndEnabled)
runtimeRef.Play();
2024-01-03 06:34:33 +00:00
runtimeRef.volume = maximumVolume * transitionTimeModifier.Evaluate(weight);
2023-09-13 05:07:40 +00:00
}
2024-01-03 06:34:33 +00:00
public override bool InitializeEffect(CozyWeather weather)
2023-09-13 05:07:40 +00:00
{
2024-01-03 06:34:33 +00:00
if (!Application.isPlaying)
2023-09-13 05:07:40 +00:00
return false;
2024-01-03 06:34:33 +00:00
base.InitializeEffect(weather);
2023-09-13 05:07:40 +00:00
if (runtimeRef == null)
{
2024-01-03 06:34:33 +00:00
runtimeRef = weather.GetFXRuntimeRef<AudioSource>(name);
if (runtimeRef)
return true;
2023-09-13 05:07:40 +00:00
runtimeRef = new GameObject().AddComponent<AudioSource>();
runtimeRef.gameObject.name = name;
2024-01-03 06:34:33 +00:00
runtimeRef.transform.parent = weather.audioFXParent;
runtimeRef.transform.SetLocalPositionAndRotation(Vector3.zero, Quaternion.identity);
2023-09-13 05:07:40 +00:00
runtimeRef.clip = clip;
2024-01-03 06:34:33 +00:00
runtimeRef.outputAudioMixerGroup = audioMixer;
runtimeRef.playOnAwake = false;
2023-09-13 05:07:40 +00:00
runtimeRef.volume = 0;
runtimeRef.loop = true;
}
2024-01-03 06:34:33 +00:00
return true;
2023-09-13 05:07:40 +00:00
}
}
#if UNITY_EDITOR
[CustomEditor(typeof(AudioFX))]
[CanEditMultipleObjects]
public class E_AudioFX : E_FXProfile
{
void OnEnable()
{
}
public override void OnInspectorGUI()
{
serializedObject.Update();
EditorGUILayout.PropertyField(serializedObject.FindProperty("clip"));
2024-01-03 06:34:33 +00:00
EditorGUILayout.PropertyField(serializedObject.FindProperty("audioMixer"));
2023-09-13 05:07:40 +00:00
EditorGUILayout.PropertyField(serializedObject.FindProperty("maximumVolume"));
EditorGUILayout.Space();
EditorGUILayout.PropertyField(serializedObject.FindProperty("transitionTimeModifier"));
serializedObject.ApplyModifiedProperties();
}
public override void RenderInWindow(Rect pos)
{
float space = EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
var propPosA = new Rect(pos.x, pos.y + space, pos.width, EditorGUIUtility.singleLineHeight);
var propPosB = new Rect(pos.x, pos.y + space * 2, pos.width, EditorGUIUtility.singleLineHeight);
2024-01-03 06:34:33 +00:00
var propPosC = new Rect(pos.x, pos.y + space * 3, pos.width, EditorGUIUtility.singleLineHeight);
var propPosD = new Rect(pos.x, pos.y + space * 4, pos.width, EditorGUIUtility.singleLineHeight);
2023-09-13 05:07:40 +00:00
serializedObject.Update();
EditorGUI.PropertyField(propPosA, serializedObject.FindProperty("clip"));
2024-01-03 06:34:33 +00:00
EditorGUI.PropertyField(propPosB, serializedObject.FindProperty("audioMixer"));
EditorGUI.PropertyField(propPosC, serializedObject.FindProperty("maximumVolume"));
2023-09-13 05:07:40 +00:00
EditorGUI.PropertyField(propPosD, serializedObject.FindProperty("transitionTimeModifier"));
serializedObject.ApplyModifiedProperties();
}
public override float GetLineHeight()
{
2024-01-03 06:34:33 +00:00
return 4;
2023-09-13 05:07:40 +00:00
}
}
#endif
}