You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

61 lines
2.3 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
namespace ET
{
public class EditorCreateAudio
{
//音效资源路径
private static string audiosDir = "assets/Res/Sound";
//导出预制体路径
private static string prefabDir = "assets/Bundles/Sound";
[MenuItem("Tools/创建音效预设", priority = 1004)]
static void CreateAudioPrefab()
{
string[] _patterns = new string[] { "*.mp3","*.wav", "*.ogg" };//识别不同的后缀名
List<string> _allFilePaths = new List<string>();
foreach (var item in _patterns)
{
string[] _temp = Directory.GetFiles(audiosDir, item, SearchOption.AllDirectories);
_allFilePaths.AddRange(_temp);
}
foreach (var item in _allFilePaths)
{
System.IO.FileInfo _fi = new System.IO.FileInfo(item);
var _tempName = _fi.Name.Replace(_fi.Extension, "").ToLower();
AudioClip _clip = AssetDatabase.LoadAssetAtPath<AudioClip>(item);
if (null != _clip)
{
GameObject _go = new GameObject();
_go.name = _tempName;
AudioSource _as = _go.AddComponent<AudioSource>();
_as.playOnAwake = false;
SoundData _data = _go.AddComponent<SoundData>();
_data.audio = _as;
_data.audio.clip = _clip;
string path = $"{prefabDir}/{_tempName}.prefab";
var temp = PrefabUtility.SaveAsPrefabAsset(_go, path);
//添加ab标记
AssetImporter importer = AssetImporter.GetAtPath(path);
if (importer == null || temp == null)
{
Debug.LogError("error: " + path);
return;
}
importer.assetBundleName = "sound.unity3d";
GameObject.DestroyImmediate(_go);
EditorUtility.SetDirty(temp);
Resources.UnloadAsset(_clip);
}
}
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
}
}