Files
Pal3.Unity/Assets/Scripts/Engine/DataLoader/AudioClipLoader.cs
Jiaqi (0x7c13) Liu e870cf487a Add core protocol regression tests and copyright checks
Cover SCE command contracts, binary readers, resource layouts and full local CPK decompression for PAL3 and PAL3A. Fix CVD time rebasing, binary read bounds and short reads, and CPK/NAV validation.

Standardize first-party copyright headers, preserve correct creation years, and update console source generation. Validate both variants in Unity and verify all live CPK entries.
2026-11-12 13:37:27 +08:00

67 lines
2.5 KiB
C#

// ------------------------------------------------------------------------
// Copyright (c) 2021-2026, Jiaqi (0x7c13) Liu. All rights reserved.
// See LICENSE file in the project root for license information.
// ------------------------------------------------------------------------
namespace Engine.DataLoader
{
using System;
using System.Collections;
using Logging;
using UnityEngine;
using UnityEngine.Networking;
public static class AudioClipLoader
{
/// <summary>
/// Load Unity AudioClip from file.
/// </summary>
/// <param name="filePath">Audio file path</param>
/// <param name="audioType">Audio type</param>
/// <param name="streamAudio">Create streaming AudioClip</param>
/// <param name="onLoaded">AudioClip callback invoker when loaded</param>
/// <returns>IEnumerator</returns>
public static IEnumerator LoadAudioClipAsync(string filePath,
AudioType audioType,
bool streamAudio,
Action<AudioClip> onLoaded)
{
if (filePath.StartsWith("/")) filePath = filePath[1..];
string url = "file:///" + filePath;
using UnityWebRequest request = UnityWebRequestMultimedia.GetAudioClip(url, audioType);
yield return request.SendWebRequest();
if (request.result != UnityWebRequest.Result.Success)
{
EngineLogger.LogError($"Failed to load {url} with error: {request.error}");
yield break; // Skip loading
}
try
{
DownloadHandlerAudioClip audioClipHandler = (DownloadHandlerAudioClip)request.downloadHandler;
// Stream audio to avoid loading the entire AudioClip into memory at once,
// which can cause frame drops and stuttering during gameplay.
audioClipHandler.streamAudio = streamAudio;
if (!streamAudio)
{
// Compress audio to reduce memory usage,
// which is recommended for small sfx audio clips.
// StreamAudio must be false to compress audio.
audioClipHandler.compressed = true;
}
AudioClip audioClip = DownloadHandlerAudioClip.GetContent(request);
onLoaded?.Invoke(audioClip);
}
catch (Exception ex)
{
EngineLogger.LogException(ex);
}
}
}
}