巧妙地批量修改unity本地文件的参数

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013452440/article/details/52415785

项目中有几百个音频文件,需要设置音频文件的格式,但Unity这块还不支持批量选中操作,一个一个修改太费劲

参考了http://www.manew.com/thread-46098-1-1.html

using UnityEngine;
using UnityEditor;
 
public class ChangeAudioClip
{
    [MenuItem ("Crusade Tools/AudioClip")]
     
    public static void CreatTool()
    {
        EditorWindow.GetWindow<AudioClip_Tool>(false ,"AudioClip_Tool",true).Show ();
    }
}
 
public class AudioClip_Tool:EditorWindow
{
    void OnGUI()
    {
        GUILayout.Space(20);
        GUILayout.BeginHorizontal();
 
        if(GUILayout.Button("Force To Mono - False", GUILayout.Width(150)))
        {
            SelectedChangeAudioFomat(false);
        }
        if(GUILayout.Button("Force To Mono - True", GUILayout.Width(150)))
        {
            SelectedChangeAudioFomat(true);
        }
        GUILayout.EndHorizontal();
        GUILayout.Space(20);
        GUILayout.BeginHorizontal();
        if(GUILayout.Button("LoadType - DecompressOnLoad", GUILayout.Width(150)))
        {
            SelectedChangeAudioTyle(AudioClipLoadType.DecompressOnLoad);
        }
        if(GUILayout.Button("LoadType - CompressedInMemory", GUILayout.Width(150)))
        {
            SelectedChangeAudioTyle(AudioClipLoadType.CompressedInMemory);
        }
        if(GUILayout.Button("LoadType - Streaming", GUILayout.Width(150)))
        {
            SelectedChangeAudioTyle(AudioClipLoadType.Streaming);
        }
        GUILayout.EndHorizontal();
    }
    void OnInspectorUpdate()
    {
        this.Repaint();
    }
    static void SelectedChangeAudioTyle(AudioClipLoadType ac) 
    { 
        Object[] audios = GetSelectedAudioClip(); 
        Selection.objects = new Object[0];
        foreach (AudioClip audioClips in audios)  {
            string path = AssetDatabase.GetAssetPath(audioClips); 
            AudioImporter audioImporter = AudioImporter.GetAtPath(path) as AudioImporter;
            AudioImporterSampleSettings ais=audioImporter.defaultSampleSettings;
            ais.loadType=ac;
            audioImporter.defaultSampleSettings=ais;
            AssetDatabase.ImportAsset(path); 
        }
    }
    static void SelectedChangeAudioFomat(bool  newFormat) 
    { 
        Object[] audios = GetSelectedAudioClip(); 
        Selection.objects = new Object[0];
        foreach (AudioClip audioClips in audios)  {
            string path = AssetDatabase.GetAssetPath(audioClips); 
            AudioImporter audioImporter = AudioImporter.GetAtPath(path) as AudioImporter; 
            audioImporter.forceToMono = newFormat;  
            AssetDatabase.ImportAsset(path); 
        }
    }
    static Object[] GetSelectedAudioClip() 
    { 
        return Selection.GetFiltered(typeof(AudioClip), SelectionMode.DeepAssets); 
    }
}
这在5.4中是没问题的,但是在4.6就会报错,而且二个版本的界面都不一样。这样就只能寻找其他的解决方法:

又找到了这个http://jingyan.baidu.com/article/48a42057c190d4a924250484.html

因为我发现这些信息是记录在.meta文件里的,只要修改meta文件就可以达到我想要的效果。于是就变成了批量修改多个文本中的字符串=。=


没有更多推荐了,返回首页