AssetPostprocessor_Audio(Audio 최적화 설정 셋팅)
Unity3D/Tips / 2023. 4. 5. 18:33
Audio Import 시, 원하는 최적화 관련 설정 자동 셋팅하도록 소스 정리 함.
(AudioCompressionFormat은 심플하게 Vobis / Quality 40으로 통일함.)
using System.IO;
using UnityEditor;
using UnityEngine;
public class AssetPostprocessor_Audio : AssetPostprocessor
{
/*
* 오디오클립 파일 경로
* Resources - Sound ┬ BGM - MainBGM
* ├ InGame - SFX
* └ SoundEffect ┬ OutGame
* ├ UI
* └ Voice
* Resources_AssetBundles ┬ sound ┬ sound_effect
* │ ├ sound_main_bgm
* │ ├ sound_sub_bgm
* │ └ sound_voice
* ├ sound_scenario
* └ sound_scenario_StreamingAssets
*/
void OnPreprocessAudio()
{
bool bDefaultSettingChanged = false;
AudioImporter audioImporter = assetImporter as AudioImporter;
AudioImporterSampleSettings defaultSampleSettings = audioImporter.defaultSampleSettings;
AudioImporterSampleSettings iosSampleSettings = audioImporter.GetOverrideSampleSettings("iOS");
bool biOSSettingChanged = false;
UnityEditor.SerializedObject serializedObject = new UnityEditor.SerializedObject(audioImporter);
UnityEditor.SerializedProperty normalize = serializedObject.FindProperty("m_Normalize");
string strPath = audioImporter.assetPath;
Debug.LogFormat("[audioImporter.assetPath] {0}", strPath);
// Folder Name
if (strPath.StartsWith("Assets/Resources/Sound/BGM"))
{
audioImporter.forceToMono = true;
normalize.boolValue = false;
audioImporter.loadInBackground = true;
//audioImporter.ambisonic = false;
defaultSampleSettings.loadType = AudioClipLoadType.Streaming;
audioImporter.preloadAudioData = false;
defaultSampleSettings.compressionFormat = AudioCompressionFormat.Vorbis;
defaultSampleSettings.quality = 0.4f; // 40
defaultSampleSettings.sampleRateSetting = AudioSampleRateSetting.OptimizeSampleRate;
//iosSampleSettings.loadType = AudioClipLoadType.Streaming;
//iosSampleSettings.compressionFormat = AudioCompressionFormat.MP3;
//iosSettingChanged = true;
bDefaultSettingChanged = true;
}
else if (strPath.StartsWith("Assets/Resources/Sound/InGame/SFX"))
{
audioImporter.forceToMono = true;
normalize.boolValue = false;
audioImporter.loadInBackground = false;
//audioImporter.ambisonic = false;
defaultSampleSettings.loadType = AudioClipLoadType.DecompressOnLoad;
audioImporter.preloadAudioData = true;
defaultSampleSettings.compressionFormat = AudioCompressionFormat.Vorbis;
defaultSampleSettings.quality = 0.4f; // 40
defaultSampleSettings.sampleRateSetting = AudioSampleRateSetting.OptimizeSampleRate;
bDefaultSettingChanged = true;
}
else if (strPath.StartsWith("Assets/Resources/Sound/SoundEffect/OutGame"))
{
audioImporter.forceToMono = true;
normalize.boolValue = false;
audioImporter.loadInBackground = false;
//audioImporter.ambisonic = false;
defaultSampleSettings.loadType = AudioClipLoadType.DecompressOnLoad;
audioImporter.preloadAudioData = true;
defaultSampleSettings.compressionFormat = AudioCompressionFormat.Vorbis;
defaultSampleSettings.quality = 0.4f; // 40
defaultSampleSettings.sampleRateSetting = AudioSampleRateSetting.OptimizeSampleRate;
bDefaultSettingChanged = true;
}
else if (strPath.StartsWith("Assets/Resources/Sound/SoundEffect/UI"))
{
audioImporter.forceToMono = true;
normalize.boolValue = false;
audioImporter.loadInBackground = false;
//audioImporter.ambisonic = false;
defaultSampleSettings.loadType = AudioClipLoadType.DecompressOnLoad;
audioImporter.preloadAudioData = true;
defaultSampleSettings.compressionFormat = AudioCompressionFormat.Vorbis;
defaultSampleSettings.quality = 0.4f; // 40
defaultSampleSettings.sampleRateSetting = AudioSampleRateSetting.OptimizeSampleRate;
bDefaultSettingChanged = true;
}
else if (strPath.StartsWith("Assets/Resources/Sound/SoundEffect/Voice"))
{
audioImporter.forceToMono = true;
normalize.boolValue = false;
audioImporter.loadInBackground = false;
//audioImporter.ambisonic = false;
defaultSampleSettings.loadType = AudioClipLoadType.CompressedInMemory;
audioImporter.preloadAudioData = true;
defaultSampleSettings.compressionFormat = AudioCompressionFormat.Vorbis;
defaultSampleSettings.quality = 0.4f; // 40
defaultSampleSettings.sampleRateSetting = AudioSampleRateSetting.OptimizeSampleRate;
bDefaultSettingChanged = true;
}
else if (strPath.StartsWith("Assets/Resources_AssetBundles/sound/sound_effect"))
{
audioImporter.forceToMono = true;
normalize.boolValue = false;
audioImporter.loadInBackground = false;
//audioImporter.ambisonic = false;
defaultSampleSettings.loadType = AudioClipLoadType.DecompressOnLoad;
audioImporter.preloadAudioData = true;
defaultSampleSettings.compressionFormat = AudioCompressionFormat.Vorbis;
defaultSampleSettings.quality = 0.4f; // 40
defaultSampleSettings.sampleRateSetting = AudioSampleRateSetting.OptimizeSampleRate;
bDefaultSettingChanged = true;
}
else if (strPath.StartsWith("Assets/Resources_AssetBundles/sound/sound_main_bgm"))
{
audioImporter.forceToMono = true;
normalize.boolValue = false;
audioImporter.loadInBackground = true;
//audioImporter.ambisonic = false;
defaultSampleSettings.loadType = AudioClipLoadType.Streaming;
audioImporter.preloadAudioData = false;
defaultSampleSettings.compressionFormat = AudioCompressionFormat.Vorbis;
defaultSampleSettings.quality = 0.4f; // 40
defaultSampleSettings.sampleRateSetting = AudioSampleRateSetting.OptimizeSampleRate;
bDefaultSettingChanged = true;
}
else if (strPath.StartsWith("Assets/Resources_AssetBundles/sound/sound_sub_bgm"))
{
audioImporter.forceToMono = true;
normalize.boolValue = false;
audioImporter.loadInBackground = true;
//audioImporter.ambisonic = false;
defaultSampleSettings.loadType = AudioClipLoadType.Streaming;
audioImporter.preloadAudioData = false;
defaultSampleSettings.compressionFormat = AudioCompressionFormat.Vorbis;
defaultSampleSettings.quality = 0.4f; // 40
defaultSampleSettings.sampleRateSetting = AudioSampleRateSetting.OptimizeSampleRate;
bDefaultSettingChanged = true;
}
else if (strPath.StartsWith("Assets/Resources_AssetBundles/sound/sound_voice"))
{
audioImporter.forceToMono = true;
normalize.boolValue = false;
audioImporter.loadInBackground = false;
//audioImporter.ambisonic = false;
defaultSampleSettings.loadType = AudioClipLoadType.CompressedInMemory;
audioImporter.preloadAudioData = true;
defaultSampleSettings.compressionFormat = AudioCompressionFormat.Vorbis;
defaultSampleSettings.quality = 0.4f; // 40
defaultSampleSettings.sampleRateSetting = AudioSampleRateSetting.OptimizeSampleRate;
bDefaultSettingChanged = true;
}
else if (strPath.StartsWith("Assets/Resources_AssetBundles/sound_scenario"))
{
// File Name
if (strPath.Contains("/bg_")) // BGM
{
audioImporter.forceToMono = true;
normalize.boolValue = false;
audioImporter.loadInBackground = true;
//audioImporter.ambisonic = false;
defaultSampleSettings.loadType = AudioClipLoadType.Streaming;
audioImporter.preloadAudioData = false;
defaultSampleSettings.compressionFormat = AudioCompressionFormat.Vorbis;
defaultSampleSettings.quality = 0.4f; // 40
defaultSampleSettings.sampleRateSetting = AudioSampleRateSetting.OptimizeSampleRate;
bDefaultSettingChanged = true;
}
else if(strPath.Contains("/se_")) // SFX
{
audioImporter.forceToMono = true;
normalize.boolValue = false;
audioImporter.loadInBackground = false;
//audioImporter.ambisonic = false;
defaultSampleSettings.loadType = AudioClipLoadType.DecompressOnLoad;
audioImporter.preloadAudioData = true;
defaultSampleSettings.compressionFormat = AudioCompressionFormat.Vorbis;
defaultSampleSettings.quality = 0.4f; // 40
defaultSampleSettings.sampleRateSetting = AudioSampleRateSetting.OptimizeSampleRate;
bDefaultSettingChanged = true;
}
else // Etc(SFX?)
{
audioImporter.forceToMono = true;
normalize.boolValue = false;
audioImporter.loadInBackground = false;
//audioImporter.ambisonic = false;
defaultSampleSettings.loadType = AudioClipLoadType.DecompressOnLoad;
audioImporter.preloadAudioData = true;
defaultSampleSettings.compressionFormat = AudioCompressionFormat.Vorbis;
defaultSampleSettings.quality = 0.4f; // 40
defaultSampleSettings.sampleRateSetting = AudioSampleRateSetting.OptimizeSampleRate;
bDefaultSettingChanged = true;
}
}
else if (strPath.StartsWith("Assets/Resources_AssetBundles/sound_scenario_StreamingAssets"))
{
// File Name
if (strPath.Contains("/bg_")) // BGM(
{
audioImporter.forceToMono = true;
normalize.boolValue = false;
audioImporter.loadInBackground = true;
//audioImporter.ambisonic = false;
defaultSampleSettings.loadType = AudioClipLoadType.Streaming;
audioImporter.preloadAudioData = false;
defaultSampleSettings.compressionFormat = AudioCompressionFormat.Vorbis;
defaultSampleSettings.quality = 0.4f; // 40
defaultSampleSettings.sampleRateSetting = AudioSampleRateSetting.OptimizeSampleRate;
bDefaultSettingChanged = true;
}
else if (strPath.Contains("/se_")) // SFX
{
audioImporter.forceToMono = true;
normalize.boolValue = false;
audioImporter.loadInBackground = false;
//audioImporter.ambisonic = false;
defaultSampleSettings.loadType = AudioClipLoadType.DecompressOnLoad;
audioImporter.preloadAudioData = true;
defaultSampleSettings.compressionFormat = AudioCompressionFormat.Vorbis;
defaultSampleSettings.quality = 0.4f; // 40
defaultSampleSettings.sampleRateSetting = AudioSampleRateSetting.OptimizeSampleRate;
bDefaultSettingChanged = true;
}
else // Etc(SFX?)
{
audioImporter.forceToMono = true;
normalize.boolValue = false;
audioImporter.loadInBackground = false;
//audioImporter.ambisonic = false;
defaultSampleSettings.loadType = AudioClipLoadType.DecompressOnLoad;
audioImporter.preloadAudioData = true;
defaultSampleSettings.compressionFormat = AudioCompressionFormat.Vorbis;
defaultSampleSettings.quality = 0.4f; // 40
defaultSampleSettings.sampleRateSetting = AudioSampleRateSetting.OptimizeSampleRate;
bDefaultSettingChanged = true;
}
}
try
{
// 수정됐으면 Save
if (true == bDefaultSettingChanged)
{
audioImporter.defaultSampleSettings = defaultSampleSettings;
serializedObject.ApplyModifiedProperties();
if (true == biOSSettingChanged)
{
audioImporter.SetOverrideSampleSettings("iOS", iosSampleSettings);
}
audioImporter.SaveAndReimport();
EditorUtility.SetDirty(audioImporter);
Debug.LogFormat("[{0}] has been imported to [{1}]\n[Force To Mono] {2}\n[Normalize] {3}\n[Load In Background] {4}\n[Load Type] {5}\n[Compression Format] Default: {6}, iOS: {7}\n[Quality] {8}\n[Sample Rate Setting] {9}",
Path.GetFileName(strPath),
Path.GetDirectoryName(strPath),
audioImporter.forceToMono.ToString(),
normalize.boolValue,
audioImporter.loadInBackground,
defaultSampleSettings.loadType.ToString(),
defaultSampleSettings.compressionFormat.ToString(),
iosSampleSettings.compressionFormat.ToString(),
(defaultSampleSettings.quality * 100f),
defaultSampleSettings.sampleRateSetting.ToString()
);
}
// OnPreprocessAudio() 에서는 Refresh 안함
//AssetDatabase.Refresh();
}
catch (System.Exception e)
{
Debug.LogError(e);
}
}
// 파일이 이동한 경우 다시 임포트.
static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
{
try
{
foreach (string movedAsset in movedAssets)
{
if (movedAsset.StartsWith("Assets/Resources/Sound")
|| movedAsset.StartsWith("Assets/Resources_AssetBundles/sound")
|| movedAsset.StartsWith("Assets/Resources_AssetBundles/sound_scenario")
|| movedAsset.StartsWith("Assets/Resources_AssetBundles/sound_scenario_StreamingAssets")
)
{
AssetDatabase.ImportAsset(movedAsset);
}
}
}
catch (System.Exception e)
{
Debug.LogError(e);
}
}
}
[참조]
https://overworks.github.io/unity/2018/09/22/unity-audio-clip-import-guide.html
https://jwidaegi.blogspot.com/2019/07/unity-sound.html?sc=1680659862098#c4452839809969885205
https://www.jianshu.com/p/b389936ff1ad
https://blog.csdn.net/hururu20120502/article/details/121425411
https://drehzr.tistory.com/1016
반응형
'Unity3D > Tips' 카테고리의 다른 글
[Unity] dll 등 import 후, Revert Factory Settings 무한루프에 빠질 때 (0) | 2023.08.23 |
---|---|
[펌] 메모리 최적화 꿀팁 (0) | 2023.04.12 |
[펌] Preload Audio Data & How Unity Decides Which Audio Assets to Load to a Scene (0) | 2023.04.05 |
[펌] AudioImporter에서 Force to mono를 사용하도록 설정하면서 Normalize 만 사용하지 않도록 설정 (0) | 2023.04.05 |
[링크] 유니티 오디오 클립 임포트 설정 가이드 (0) | 2023.04.05 |