需求:需要对音频做出修改,压缩音频质量,检测是否设置为Mono与Normalize,
超过10秒LoadType设为Streaming.
思路:遍历所有的音频文件,拿到所有的音频文件对象,针对不同平台的音频文件做修改。
1.遍历所有的文件夹
string projectPath = Application.dataPath; //获取Assets的路径
allAudioFile.Clear();
projectPath = projectPath.Substring(0,projectPath.IndexOf("Assets"));
try
{
GetAudiosPath(projectPath,FilePath,"AudioClip",ref allAudioFile); //这是一个封装的方法
}
//获取所有的音频文件的path信息
public static void GetAudiosPath(string projectPath,string targetFilePath,string searchType,ref List<string> array)
{
if (Directory.Exists(targetFilePath))
{
string[] guids;
//搜索
guids = AssetDatabase.FindAssets("t:" + searchType, new[] { targetFilePath }); //查到 targetFilePath路径下的这个searchType的guid
foreach (string guid in guids)
{
string source = AssetDatabase.GUIDToAssetPath(guid); //根据guid查找路径
string[] allStr = source.Split('.');
if(source.EndsWith(".ogg")|| source.EndsWith(".mp3") || source.EndsWith(".wav") || source.EndsWith(".aif")) //过滤音频文件
{
allAudioFile.Add(source); //所有的音频文件
}
}
}
}
2.[MenuItem("Assets/xxxx")] 这个是插件,这个功能放在了Assets下面。这句话需要卸载函数上面,且这个函数必须是静态函数
image.png3.核心代码AudioImporterSampleSettings 这个能拿到不同平台上的Audio的对象
4.OnGUI() 查找到之后需要做一个显示的界面,一遍快速的定位到,是否真的修改成功了。
后期改进,不需要改进的的加个过滤,可以提高效率
image.png修改后,还需要代码重新导入才能生效
image.png完整代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
public class ModifyAllAudioFileSetting : EditorWindow
{
public static string FilePath = "Assets";
public static List<string> allAudioFile = new List<string>();
private const bool IsForceToMono = true;
private const bool IsNormalize = true;
private const float QualityNum = 0.7f;
private const AudioClipLoadType LoadType = AudioClipLoadType.Streaming;
Vector2 mScroll = Vector2.zero;
void OnGUI()
{
if(allAudioFile == null)
{
return;
}
mScroll = GUILayout.BeginScrollView(mScroll);
if(allAudioFile != null && allAudioFile.Count>0)
{
if(NGUIEditorTools.DrawHeader("AudioClip"))
{
int count = 0;
foreach(var item in allAudioFile)
{
count +=1;
AudioClip clip = AssetDatabase.LoadAssetAtPath(item,typeof(AudioClip))as AudioClip;
GUIContent label2 = new GUIContent(count +"---"+item,item);
EditorGUIUtility.labelWidth = EditorGUIUtility.currentViewWidth*0.5f;
EditorGUILayout.ObjectField(label2,clip,typeof(AudioClip),false);
}
}
}
GUILayout.EndScrollView();
}
//遍历所有的文件
[MenuItem("Assets/设置所有音频的参数")]
public static async void scanAllFile()
{
string projectPath = Application.dataPath;
allAudioFile.Clear();
projectPath = projectPath.Substring(0,projectPath.IndexOf("Assets"));
try
{
GetAudiosPath(projectPath,FilePath,"AudioClip",ref allAudioFile);
}
catch (System.Exception e)
{
Debug.LogError(e);
}
for(int i = 0;i < allAudioFile.Count;i++)
{
EditorUtility.DisplayProgressBar("Check Audios","Current Audio :"+i+"/"+allAudioFile.Count,(float)i/allAudioFile.Count);
}
try
{
//获取每个音频的audiosetting
for(int i = 0;i<allAudioFile.Count;i++)
{
var import = AssetImporter.GetAtPath(allAudioFile[i]);
if (import == null || !(import is AudioImporter))
{
return;
}
var audioImporter = import as AudioImporter;
bool flag = false;
AudioImporterSampleSettings settingDefault = audioImporter.defaultSampleSettings;
AudioImporterSampleSettings settingAndroid = audioImporter.GetOverrideSampleSettings("Android");
AudioImporterSampleSettings settingIOS = audioImporter.GetOverrideSampleSettings("iPhone");
AudioImporterSampleSettings settingStandalong = audioImporter.GetOverrideSampleSettings("Standalone");
UnityEditor.SerializedObject serializedObject = new UnityEditor.SerializedObject(audioImporter);
UnityEditor.SerializedProperty normalize = serializedObject.FindProperty("m_Normalize");
if(audioImporter.forceToMono != true)
{
audioImporter.forceToMono = IsForceToMono;
flag = true;
}
if(normalize.boolValue != true)
{
normalize.boolValue = IsNormalize;
flag = true;
}
if(settingDefault.quality != QualityNum)
{
settingDefault.quality = QualityNum;
flag = true;
}
if(settingAndroid.quality != QualityNum)
{
settingAndroid.quality = QualityNum;
flag = true;
}
if(settingStandalong.quality != QualityNum)
{
settingStandalong.quality = QualityNum;
flag = true;
}
if(settingIOS.quality != QualityNum)
{
settingIOS.quality = QualityNum;
flag = true;
}
AudioClip audioClip = AssetDatabase.LoadAssetAtPath<AudioClip>(allAudioFile[i]);
if(audioClip.length>=10)
{
if(settingDefault.loadType != LoadType)
{
settingDefault.loadType = LoadType;
}
if(settingAndroid.loadType != LoadType)
{
settingAndroid.loadType = LoadType;
}
if(settingStandalong.loadType != LoadType)
{
settingStandalong.loadType = LoadType;
}
if(settingIOS.loadType != LoadType)
{
settingIOS.loadType = LoadType;
}
}
if(flag)
{
audioImporter.defaultSampleSettings = settingDefault;
serializedObject.ApplyModifiedProperties();
audioImporter.SetOverrideSampleSettings(BuildTargetGroup.iOS.ToString(),settingIOS);
audioImporter.SetOverrideSampleSettings(BuildTargetGroup.Android.ToString(),settingAndroid);
audioImporter.SetOverrideSampleSettings(BuildTargetGroup.Standalone.ToString(),settingStandalong);
audioImporter.SaveAndReimport();
EditorUtility.SetDirty(audioImporter);
}
}
//修改参数
AssetDatabase.Refresh();
}
catch (System.Exception e)
{
EditorUtility.ClearProgressBar();
Debug.LogError(e);
}
EditorUtility.ClearProgressBar();
EditorWindow.GetWindow<ModifyAllAudioFileSetting>(false,"ModifyAllAudioFileSetting",true).Show();
Debug.Log("修改成功");
}
//获取所有的音频文件的path信息
public static void GetAudiosPath(string projectPath,string targetFilePath,string searchType,ref List<string> array)
{
if (Directory.Exists(targetFilePath))
{
string[] guids;
//搜索
guids = AssetDatabase.FindAssets("t:" + searchType, new[] { targetFilePath });
foreach (string guid in guids)
{
string source = AssetDatabase.GUIDToAssetPath(guid);
string[] allStr = source.Split('.');
if(source.EndsWith(".ogg")|| source.EndsWith(".mp3") || source.EndsWith(".wav") || source.EndsWith(".aif"))
{
allAudioFile.Add(source);
}
}
}
}
}
网友评论