Unity3D批量修改Texture属性

Unity3D批量修改Texture属性

  • Unity3D批量修改Texture属性
    • 功能
    • 思路
    • 代码

功能

批量读取选择的文件夹下的Texture类型文件,按照需求更改Texture的属性。

思路

读取所选择文件夹下的某类型文件,包括所有子文件夹,有一个很强大的函数。

public static Object[] GetFiltered(Type type, SelectionMode mode); 

其中SelectionMode可选择如下:

Variables Description
Unfiltered Return the whole selection.
TopLevel Only return the topmost selected transform. A selected child of another selected transform will be filtered out.
Deep Return the selection and all child transforms of the selection.
ExcludePrefab Excludes any prefabs from the selection.
Editable Excludes any objects which shall not be modified.
Assets Only return objects that are assets in the Asset directory.
DeepAssets If the selection contains folders, also include all assets and subfolders within that folder in the file hierarchy.

只要设置为DeepAssets ,就可递归读取所有子文件夹。

代码

思路很简单,直接放代码。

public class SetTextureInfo : EditorWindow
{
    /// 
    /// 循环设置选择的图片
    /// 
    [MenuItem("SetTextureInfo/SetTextureInfo")]
    private static void LoopSetTexture()
    {
        Object[] textures = Selection.GetFiltered(typeof(Texture2D), SelectionMode.DeepAssets);

        foreach (Texture2D texture in textures)
        {
            string path = AssetDatabase.GetAssetPath(texture);
            TextureImporter texImporter = AssetImporter.GetAtPath(path) as TextureImporter;
            //不处理类型为“Lightmap”的Texture
            if ("Lightmap" != texImporter.textureType.ToString())
            {
                //修改Texture Type
                texImporter.textureType = TextureImporterType.Advanced;
                //修改Aniso Level
                texImporter.anisoLevel = 0;
                //修改Read/Write enabled 
                texImporter.isReadable = false;
                //修改Generate Mip Maps
                texImporter.mipmapEnabled = false;

                string texName = texture.name;
                int maxSize[2]; 
                TextureImporterFormat texFormat;
                texImporter.GetPlatformTextureSettings("Android", out maxSize[0], out texFormat);
                texImporter.GetPlatformTextureSettings("iPhone", out maxSize[1], out texFormat);
                if (texName.Contains("alpha"))
                {                    
                    texImporter.SetPlatformTextureSettings("Android", maxSize[0], TextureImporterFormat.ETC2_RGBA8);
                    texImporter.SetPlatformTextureSettings("iPhone", maxSize[1], TextureImporterFormat.PVRTC_RGBA4);
                }
                else
                {
                    texImporter.SetPlatformTextureSettings("Android", maxSize[0], TextureImporterFormat.ETC2_RGB4);
                    texImporter.SetPlatformTextureSettings("iPhone", maxSize[1], TextureImporterFormat.PVRTC_RGB4);
                }
                AssetDatabase.ImportAsset(path);
            }
        }
    }
}

你可能感兴趣的:(unity/C#)