// Copyright (c) 2015 - 2023 Doozy Entertainment. All Rights Reserved. // This code can only be used under the standard Unity Asset Store End User License Agreement // A Copy of the EULA APPENDIX 1 is available at http://unity3d.com/company/legal/as_terms using Doozy.Runtime.Common.Extensions; using Doozy.Runtime.Common.Utils; using Doozy.Runtime.Mody.Actions; using UnityEngine; // ReSharper disable MemberCanBePrivate.Global namespace Doozy.Runtime.Mody.Modules { /// Mody module used to control an AudioSource [AddComponentMenu("Doozy/Mody/AudioSource Module")] public class AudioSourceModule : ModyModule { #if UNITY_EDITOR [UnityEditor.MenuItem("GameObject/Doozy/Mody/AudioSource Module", false, 8)] private static void CreateComponent(UnityEditor.MenuCommand menuCommand) { GameObjectUtils.AddToScene("AudioSource Module", false, true); } #endif /// Default module name public const string DEFAULT_MODULE_NAME = "AudioSource"; /// AudioSource reference public AudioSource Source; /// Check if this module has an AudioSource reference public bool hasSource => Source != null; /// Simple action to Play sound public SimpleModyAction Play; /// Simple action to Stop playing sound public SimpleModyAction Stop; /// Simple action to Mute sound public SimpleModyAction Mute; /// Simple action to Unmute sound public SimpleModyAction Unmute; /// Simple action to Pause playing sound public SimpleModyAction Pause; /// Simple action to Unpause playing sound public SimpleModyAction Unpause; /// Construct an AudioSource module with the default name public AudioSourceModule() : this(DEFAULT_MODULE_NAME) { } /// Construct an AudioSource module with the given AudioSource reference /// AudioSource reference public AudioSourceModule(AudioSource audioSource) : this(DEFAULT_MODULE_NAME, audioSource) { } /// Construct an AudioSource module with the given module name and AudioSource reference /// Module name /// AudioSource reference public AudioSourceModule(string moduleName, AudioSource audioSource) : this(moduleName.IsNullOrEmpty() ? DEFAULT_MODULE_NAME : moduleName) { Source = audioSource; } /// Construct an AudioSource module with the given module name /// Module name public AudioSourceModule(string moduleName) : base(moduleName) { } /// Initialize the actions protected override void SetupActions() { this.AddAction(Play ??= new SimpleModyAction(this, nameof(Play), ExecuteSourcePlay)); this.AddAction(Stop ??= new SimpleModyAction(this, nameof(Stop), ExecuteSourceStop)); this.AddAction(Mute ??= new SimpleModyAction(this, nameof(Mute), ExecuteSourceMute)); this.AddAction(Unmute ??= new SimpleModyAction(this, nameof(Unmute), ExecuteSourceUnmute)); this.AddAction(Pause ??= new SimpleModyAction(this, nameof(Pause), ExecutePauseSource)); this.AddAction(Unpause ??= new SimpleModyAction(this, nameof(Unpause), ExecuteSourceUnpause)); } /// Execute Play on the AudioSource public void ExecuteSourcePlay() { if (!hasSource) return; Source.Play(); } /// Execute Stop on the AudioSource public void ExecuteSourceStop() { if (!hasSource) return; Source.Stop(); } /// Mute the AudioSource public void ExecuteSourceMute() { if (!hasSource) return; Source.mute = true; } /// Unmute the AudioSource public void ExecuteSourceUnmute() { if (!hasSource) return; Source.mute = false; } /// Execute Pause on the AudioSource public void ExecutePauseSource() { if (!hasSource) return; Source.Pause(); } /// Execute UnPause on the AudioSource public void ExecuteSourceUnpause() { if (!hasSource) return; Source.UnPause(); } } }