在unity中厘贼,有時(shí)候用ugui或者做2d的時(shí)候,需要導(dǎo)入大量的圖片素材蜒程,我們一個(gè)個(gè)設(shè)置會(huì)很麻煩,所以可以通過編輯器擴(kuò)展開發(fā)一個(gè)插件伺帘,供我們快速設(shè)置Sprite昭躺!
設(shè)置Sprite的類主要涉及的是“TextureImporter”和“TextureImporterPlatformSettings”!
下面上代碼伪嫁,基本都有注釋:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class ImporterSpriterSetting : EditorWindow
{
[MenuItem("Importer/ImporterSpriterSetting")]
static void ImporterSpriterSettingWindow()
{
//創(chuàng)建窗口
Rect wr = new Rect(500, 500, 500, 500);
ImporterSpriterSetting window = (ImporterSpriterSetting)EditorWindow.GetWindowWithRect(typeof(ImporterSpriterSetting), wr, false, "ImporterSpriterSetting");
window.Show();
}
TextureImporterType textureType = TextureImporterType.Sprite;
SpriteImportMode spriteMode = SpriteImportMode.Single;
string packingTag = string.Empty;
bool sRGBTexture = true;
bool alphaIsTransparency = true;
bool mipmapEnabled = false;
TextureWrapMode wrapMode = TextureWrapMode.Clamp;
FilterMode filterMode = FilterMode.Bilinear;
TextureImporterFormat textureImporterFormatAndroid = TextureImporterFormat.ETC2_RGBA8;
TextureImporterFormat textureImporterFormatiPhone = TextureImporterFormat.PVRTC_RGBA4;
TextureImporterFormat textureImporterFormatStandalone = TextureImporterFormat.DXT5;
void OnGUI()
{
textureType = (TextureImporterType)EditorGUILayout.EnumPopup("Texture Type", textureType);
EditorGUILayout.Space();//空一行
spriteMode = (SpriteImportMode)EditorGUILayout.EnumPopup("Sprite Mode", spriteMode);
EditorGUILayout.Space();//空一行
packingTag = EditorGUILayout.TextField("Packing Tag", packingTag);
EditorGUILayout.Space();//空一行
sRGBTexture = EditorGUILayout.Toggle("S RGB Texture", sRGBTexture);
EditorGUILayout.Space();//空一行
alphaIsTransparency = EditorGUILayout.Toggle("Alpha Is Transparency", alphaIsTransparency);
EditorGUILayout.Space();//空一行
mipmapEnabled = EditorGUILayout.Toggle("Generate Mip Map", mipmapEnabled);
EditorGUILayout.Space();//空一行
wrapMode = (TextureWrapMode)EditorGUILayout.EnumPopup("Wrap Mode", wrapMode);
EditorGUILayout.Space();//空一行
filterMode = (FilterMode)EditorGUILayout.EnumPopup("Filter Mode", filterMode);
EditorGUILayout.Space();//空一行
textureImporterFormatAndroid = (TextureImporterFormat)EditorGUILayout.EnumPopup("Texture Importer Format For Android", textureImporterFormatAndroid);
EditorGUILayout.Space();//空一行
textureImporterFormatiPhone = (TextureImporterFormat)EditorGUILayout.EnumPopup("Texture Importer Format For iPhone", textureImporterFormatiPhone);
EditorGUILayout.Space();//空一行
textureImporterFormatStandalone = (TextureImporterFormat)EditorGUILayout.EnumPopup("Texture Importer Format For Standalone", textureImporterFormatStandalone);
EditorGUILayout.Space();//空一行
if (GUILayout.Button("設(shè)置Sprite", GUILayout.ExpandWidth(true)))
{
Object[] targetObj = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.DeepAssets);//得到選中的文件领炫,包括選中文件夾的子文件和子文件夾
if (targetObj != null && targetObj.Length > 0)
{
for (int i = 0; i < targetObj.Length; i++)
{
if (targetObj[i] is Texture) //檢測(cè)是不是Texture
{
string path = AssetDatabase.GetAssetPath(targetObj[i]); //得到資源的路徑
TextureImporter texture = AssetImporter.GetAtPath(path) as TextureImporter; //通過路徑得到資源
texture.textureType = textureType; //TextureType (Enum)
texture.spriteImportMode = spriteMode; //SpriteMode (Enum)
texture.spritePackingTag = packingTag; //packingTag (String)
texture.spritePixelsPerUnit = 100; //PixelsPerUnit (flot ps:像素)
texture.sRGBTexture = sRGBTexture; //sRGBTexture (bool)
texture.alphaIsTransparency = alphaIsTransparency; //AlphaIsTransparency (bool)
texture.filterMode = filterMode; //FilterMode (Enum)
texture.mipmapEnabled = mipmapEnabled; //mipmapEnabled (Bool)
texture.wrapMode = wrapMode; //WrapMode (Enum)
TextureImporterPlatformSettings platformSetting = new TextureImporterPlatformSettings();//用來單獨(dú)設(shè)置單獨(dú)的平臺(tái)信息
platformSetting.name = "Android"; //平臺(tái)名字
platformSetting.format = textureImporterFormatAndroid; //Format屬性
platformSetting.overridden = true; //True為勾選Override選項(xiàng)
platformSetting.maxTextureSize = 2048; //最大紋理尺寸
texture.SetPlatformTextureSettings(platformSetting); //將平臺(tái)信息設(shè)置給圖片
//TextureImporterPlatformSettings platformSettingIP = new TextureImporterPlatformSettings();
//platformSettingIP.name = "iPhone";
//platformSettingIP.format = textureImporterFormatiPhone;
//platformSettingIP.overridden = true;
//platformSettingIP.maxTextureSize = 2048;
//texture.SetPlatformTextureSettings(platformSettingIP);
TextureImporterPlatformSettings platformSettingStandalone = new TextureImporterPlatformSettings();
platformSettingStandalone.name = "Standalone";
platformSettingStandalone.format = textureImporterFormatStandalone;
platformSettingStandalone.overridden = true;
platformSettingStandalone.maxTextureSize = 2048;
texture.SetPlatformTextureSettings(platformSettingStandalone);
AssetDatabase.ImportAsset(path);
}
}
}
}
}
}
UI的截圖:
最后上一下設(shè)置好的圖片: