Initial card audio
This commit is contained in:
@@ -1,12 +1,16 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.Audio;
|
||||
using AppleHills.Core;
|
||||
using AppleHills.Core.Interfaces;
|
||||
using System.Collections.Generic;
|
||||
using AudioSourceEvents;
|
||||
using System;
|
||||
using Core;
|
||||
using Core.Lifecycle;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UI;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AddressableAssets;
|
||||
using UnityEngine.Audio;
|
||||
using UnityEngine.Playables;
|
||||
using UnityEngine.ResourceManagement.AsyncOperations;
|
||||
|
||||
public class AudioManager : ManagedBehaviour, IPausable
|
||||
{
|
||||
@@ -32,6 +36,12 @@ public class AudioManager : ManagedBehaviour, IPausable
|
||||
public List<AppleAudioSource> ambienceSources;
|
||||
public List<AppleAudioSource> SFXSources;
|
||||
|
||||
public AppleAudioSource uiAudioSource;
|
||||
public AppleAudioSource uiMusicSource;
|
||||
// Dictionary to track addressable handles by AudioManager
|
||||
private Dictionary<AppleAudioSource, AsyncOperationHandle<AudioResource>> _addressableHandles
|
||||
= new Dictionary<AppleAudioSource, AsyncOperationHandle<AudioResource>>();
|
||||
|
||||
private IAudioEventSource _eventSource;
|
||||
private bool wasInterrupted;
|
||||
|
||||
@@ -212,7 +222,7 @@ public class AudioManager : ManagedBehaviour, IPausable
|
||||
|
||||
private void OnApplicationQuit()
|
||||
{
|
||||
// TODO: Release the handles safely ReleaseAllHandles();
|
||||
ReleaseAllHandles();
|
||||
}
|
||||
|
||||
private void SetupNewAudioSource(AppleAudioSource audioSource)
|
||||
@@ -284,4 +294,54 @@ public class AudioManager : ManagedBehaviour, IPausable
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public void LoadAndPlayUIAudio(string key, bool isMusic)
|
||||
{
|
||||
var _targetAudioPlayer = uiAudioSource;
|
||||
if (isMusic)
|
||||
{
|
||||
_targetAudioPlayer = uiMusicSource;
|
||||
}
|
||||
// Load the asset via addressables
|
||||
var handle = Addressables.LoadAssetAsync<AudioResource>(key);
|
||||
var result = handle.WaitForCompletion();
|
||||
|
||||
// Store the handle for later release
|
||||
_addressableHandles[uiAudioSource] = handle;
|
||||
|
||||
Logging.Debug($"[CinematicsManager] Loaded addressable UI audio clip: {key}");
|
||||
_targetAudioPlayer.audioSource.resource = result;
|
||||
_targetAudioPlayer.Play(0);
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Releases all active addressable handles
|
||||
/// </summary>
|
||||
private void ReleaseAllHandles()
|
||||
{
|
||||
foreach (var handle in _addressableHandles.Values)
|
||||
{
|
||||
if (handle.IsValid())
|
||||
{
|
||||
Addressables.Release(handle);
|
||||
}
|
||||
}
|
||||
_addressableHandles.Clear();
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
// Subscribe to application quit event to ensure cleanup
|
||||
Application.quitting += OnApplicationQuit;
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
// Unsubscribe from application quit event
|
||||
Application.quitting -= OnApplicationQuit;
|
||||
|
||||
// Clean up any remaining addressable handles when disabled
|
||||
ReleaseAllHandles();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user