代码改变世界

unity修改所选路径下的,对象的importer属性

2020-02-19 19:24  kk20161206  阅读(646)  评论(0编辑  收藏  举报

遍历文件夹下的对象,并修改其导入设置:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Diagnostics;
using System;
using System.Text;


public class TextureOperation
{
    [MenuItem("Assets/Texture/DisableMipmapAndRW")]
    static void DisableMipmapAndRW()
    {
        UnityEngine.Object[] objs = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.DeepAssets);
        for (int i = 0; i < objs.Length; i++)
        {
            UnityEngine.Object obj = objs[i];
            Texture2D tex = obj as Texture2D;
            if (tex)
            {
                string texturePath = AssetDatabase.GetAssetPath(tex);
                TextureImporter ti = AssetImporter.GetAtPath(texturePath) as TextureImporter;
                if (ti)
                {
                    ti.mipmapEnabled = false;
                    ti.isReadable = false;
                    ti.SaveAndReimport();
                }
                else
                {
                    UnityEngine.Debug.LogError("========Fail to batch " + texturePath);
                }
            }
        }

    } }

  遍历所选文件夹下的 对象

 UnityEngine.Object[] objs = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.DeepAssets);
通过对象,拿到它的路径
 string texturePath = AssetDatabase.GetAssetPath(tex);
通过它的路径,拿到它的importer,设置其属性;然后,保存并重新导入。

AssetsProcessor类也能处理对象,然后运行reimportAll也可以好像。