1.导入图片,图片类型更改为 TextureImporterType.Sprite
using UnityEditor;
using UnityEngine;
public class TextureImporterPostprocessor : AssetPostprocessor{
void OnPreprocessTexture(){
TextureImporter textureImporter = (TextureImporter)assetImporter;
textureImporter.textureType = TextureImporterType.Sprite;
Debug.LogError(textureImporter.name);
}
}
2.更换UI预制体上的图片
游戏研发期,会使用多种UI风格的尝试,此功能是为了快速替换UI图片。
public static string prefabPath = "Image";
[MenuItem("UI/ChangeUI_UseResources")]
public static void ChangeUIVersions() {
GameObject target = Resources.Load(prefabPath) as GameObject;
Image[] images = target.GetComponents<Image>();
foreach (var item in images) {
string path = AssetDatabase.GetAssetPath(item.sprite);
string replacePath = path.Replace("1", "2");
Sprite replaceImage = AssetDatabase.LoadAssetAtPath<Sprite>(replacePath);
item.sprite = replaceImage;
}
}
public static string targetPrefabPath = "Assets/Image.prefab";
[MenuItem("UI/ChangeUI__UseAssetDataBase")]
public static void ChangeUIVersionsExt() {
GameObject target = AssetDatabase.LoadAssetAtPath<GameObject>(targetPrefabPath);
Image[] images = target.GetComponents<Image>();
foreach (var item in images){
string path = AssetDatabase.GetAssetPath(item.sprite);
string replacePath = path.Replace("2", "1");
Sprite replaceImage = AssetDatabase.LoadAssetAtPath<Sprite>(replacePath);
item.sprite = replaceImage;
}
}