您的位置:首页 > 移动开发 > Unity3D

unity3d 代码改变贴图压缩格式

2016-03-28 17:07 549 查看
using UnityEngine;
using System.Collections;
using UnityEditor;
using System;
using System.Collections.Generic;
public class ChangeFormat : EditorWindow {
TextureImporterFormat format = TextureImporterFormat.AutomaticCompressed;
int size = 512;
string platform = "Android";
bool autoSize = false;
[MenuItem("Image/ChangeFormat")]
static void SetReadWriteTrue() {
ChangeFormat changeFormat = EditorWindow.GetWindow(typeof(ChangeFormat)) as ChangeFormat;
}

void OnGUI() {
GUILayout.BeginHorizontal();
GUILayout.Label("格式:");
format = (TextureImporterFormat)EditorGUILayout.EnumPopup(format);
GUILayout.EndHorizontal();

GUILayout.BeginHorizontal();
GUILayout.Label("贴图大小:");
size = EditorGUILayout.IntField(size);
GUILayout.EndHorizontal();

//GUILayout.BeginHorizontal();
//GUILayout.Label("使用贴图原始大小:");
//autoSize = EditorGUILayout.Toggle(autoSize);
//GUILayout.EndHorizontal();

GUILayout.BeginHorizontal();
GUILayout.Label("平台:");
platform = EditorGUILayout.TextField(platform);
GUILayout.EndHorizontal();

if (GUILayout.Button("开始")) {
Execute();
}

}

void Execute() {
Debug.LogWarning("开始");
UnityEngine.Object[] selectedAsset = Selection.GetFiltered(typeof(Texture), SelectionMode.DeepAssets);
int currentSize = size;
for (int i = 0; i < selectedAsset.Length; i++) {
Texture2D tex = selectedAsset[i] as Texture2D;
TextureImporter ti = (TextureImporter)TextureImporter.GetAtPath(AssetDatabase.GetAssetPath(tex));

//if (autoSize) {
//    currentSize = ti.maxTextureSize;
//}
ti.SetPlatformTextureSettings(platform, currentSize, format);
AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(tex));
}
Debug.LogWarning("结束");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: