美文网首页
Unity-工具-资源批处理

Unity-工具-资源批处理

作者: 祝你万事顺利 | 来源:发表于2019-07-13 17:33 被阅读0次

对某个文件夹下的Asset进行批处理,设置这些资源的部分属性,这些属性需要通过代码进行设置
->目前版本可以批处理Texture和Audio类型的资产
都是通过设置Setting类对资产进行设置

->提高代码复用性,使用范型对AssetImporter进行设置,只要资源Importer继承AssetImporter,都可以对其进行设置。

->又进行了一层抽象,对编程人员暴露尽量精简的接口

->在这一版本中对资源进行的章检测,如果资源发生了改变再将AssetImporter进行保存

using UnityEditor;
using UnityEngine;

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System;
using System.Reflection;

/// <summary>
/// 批量设置Asset资源
/// </summary>
public class AssetImporterTool  : Texture{
    [MenuItem("Tools/Assets(Texture) Set Batch", false, 300)]
    static private void RunTextureAssetsSetBatch()
    {

        string filePath = AssetDatabase.GetAssetPath(Selection.activeObject);
        RunAssetsSetBatchCom<TextureImporter>((texImporter) =>
        {

            //Set TextureImporterSettings Here !!!
            if (texImporter.textureType == TextureImporterType.Default  && texImporter.textureShape == TextureImporterShape.TextureCube)
            {
            }
            else
            {
                if (texImporter.textureType != TextureImporterType.Sprite)
                {
                    texImporter.textureType = TextureImporterType.Sprite;
                }
                //texImporter.wrapMode = TextureWrapMode.Clamp;
                texImporter.mipmapEnabled = false;
            }
        }, filePath);
    }
    [MenuItem("Tools/Assets(Model) Set Batch", false, 301)]
    static private void RunModelAssetsSetBatch()
    {

        string filePath = AssetDatabase.GetAssetPath(Selection.activeObject);
        RunAssetsSetBatchCom<ModelImporter>((defModelImporter) =>
        {

            //Set Model Importer Here !!!
            defModelImporter.animationType = ModelImporterAnimationType.Generic;

        }, filePath);
    }
    [MenuItem("Tools/Assets(Audio) Set Batch", false, 302)]
    static private void RunAudioAssetsSetBatch()
    {
        string filePath = AssetDatabase.GetAssetPath(Selection.activeObject);
        RunAssetsSetBatchCom<AudioImporter>((audioImporter) =>
        {

            //Set Audio Importer Here !!!
            audioImporter.forceToMono = true;

        }, filePath);
    }

    

    private static void RunAssetsSetBatchCom<T>(assetSetImporterHandler<T> assetSetImporterHandler,string filePath) where T : AssetImporter 
    {
        string typeStr;
        string genericName = typeof(T).ToString();

        if (typeof(T) == typeof(ModelImporter))
        {
            typeStr = "t:Model";
        }
        else if (typeof(T) == typeof(TextureImporter))
        {
            typeStr = "t:Texture";
        }
        else if(typeof(T) == typeof(AudioImporter))
        {
            typeStr = "t:AudioClip";
        }
        else
        {
            typeStr = "";
        }

        string[] assetsPaths = AssetDatabase.FindAssets(typeStr, new string[] { filePath })
            .Select(guid => AssetDatabase.GUIDToAssetPath(guid)).ToArray();
        IEnumerator enumerator = AssetsSetBatch<T>(assetSetImporterHandler, assetsPaths).GetEnumerator();


        if (typeStr != "")
        {
            EditorApplicationUpdate(enumerator);
        }

    }

    public static void EditorApplicationUpdate(IEnumerator enumerator)
    {
        EditorApplication.update = delegate ()
        {
            if (!enumerator.MoveNext())
            {
                EditorUtility.ClearProgressBar();
                EditorApplication.update = null;
            }
        };
    }

    public delegate void assetSetImporterHandler<T>(T settings) where T : AssetImporter;
    public static IEnumerable AssetsSetBatch<T>(assetSetImporterHandler<T> setImporterHandler, string[] assetsPaths) where T : AssetImporter
    {
        for (int i = 0; i < assetsPaths.Length; i++)
        {
            if (EditorUtility.DisplayCancelableProgressBar("Setting... ...", assetsPaths[i], (float)i / (float)assetsPaths.Length))
            {
                yield break;
            }
            T assetImporter = (T)AssetImporter.GetAtPath(assetsPaths[i]);
            T tempImporter = assetImporter;

            setImporterHandler(assetImporter);
            
            bool ifDirty = AssetDatabase.WriteImportSettingsIfDirty(assetsPaths[i]);
            //Debug.Log(ifDirty);

            yield return null;
        }
    }
}

相关文章

网友评论

      本文标题:Unity-工具-资源批处理

      本文链接:https://www.haomeiwen.com/subject/ubtkkctx.html