UI的一些处理工具

本文介绍了一种在Unity中批量更改UI图片的方法。通过使用AssetPostprocessor改变图片类型为Sprite,并提供两种方式来快速替换UI预制体上的图片:一种是使用Resources加载资源,另一种是通过AssetDatabase操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值