Skip to content

Commit

Permalink
simplify func calling
Browse files Browse the repository at this point in the history
  • Loading branch information
Juniverse committed Jan 14, 2025
1 parent 773e168 commit fd6b1dc
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions Runtime/Scripts/Audio/InworldAudioCapture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public InworldModuleException(string moduleName) : base($"Module {moduleName} no
[RequireComponent(typeof(AudioSource))]
public class InworldAudioCapture : MonoBehaviour
{
const string k_UniqueModuleChecker = "Please ensure there is only one in the feature list.";
const string k_UniqueModuleChecker = "Find Multiple Modules with StartingAudio.\nPlease ensure there is only one in the feature list.";
[SerializeField] AudioCaptureStatus m_CurrentStatus = AudioCaptureStatus.Idle;
[SerializeField] List<InworldAudioModule> m_AudioModules;
[SerializeField] AudioEvent m_AudioEvent;
Expand Down Expand Up @@ -74,16 +74,9 @@ void Start()
{

}

public bool StartAudio()
{
List<IStartAudioHandler> modules = GetModules<IStartAudioHandler>();
if (modules.Count > 1)
InworldAI.LogWarning("Find Multiple Modules with StartingAudio.\n");
else if (modules.Count == 1)
return modules[0].OnStartAudio();
throw new InworldModuleException("StartAudio");
}

public bool StartAudio() => GetUniqueModule<IStartAudioHandler>().OnStartAudio();


public bool StopAudio()
{
Expand All @@ -105,6 +98,16 @@ public IEnumerator PushAudio()

public T GetModule<T>() => m_AudioModules.Select(module => module.GetComponent<T>()).FirstOrDefault(result => result != null);

public T GetUniqueModule<T>()
{
List<T> modules = GetModules<T>();
if (modules == null || modules.Count == 0)
throw new InworldModuleException(typeof(T).Name);
if (modules.Count > 1)
InworldAI.LogWarning(k_UniqueModuleChecker);
return modules[0];
}

public List<T> GetModules<T>() => m_AudioModules.Select(module => module.GetComponent<T>()).Where(result => result != null).ToList();

void _SetBoolWithEvent(ref bool flag, bool value, UnityEvent onEvent, UnityEvent offEvent)
Expand Down

0 comments on commit fd6b1dc

Please sign in to comment.