Unity导入图片到特定文件夹修改图片属性为Sprite

本文介绍如何使用Unity的TextureImporter类实现批量图片自动转换为Sprite,通过简单的脚本实现图片属性修改,便于游戏资源管理。将脚本放置于Editor文件夹,拖动图片至Unity即可自动转换。

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

在导入大批量图片的时候,每次要改图片属性,这个很麻烦,通过TextureImporter类能方便的修改各种属性。

真是爽,在网上找了一大堆,修改后都没有保存,导致无法拖动到Sprite里。

我贴上我自己的完整的代码吧。

我默认是ps文件夹,你可以按照文件类型或者别的什么。

这样直接拖动到Unity里就直接变了。很方便。

把下面的脚本放入Editor文件夹,没有的话手动建立一个。


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;



public class SpriteProcessor : AssetPostprocessor
{
    //拖入unity的图片自动修改图片的属性为Sprite
    private void OnPostprocessTexture(Texture2D texture)
    {
        if (assetPath.ToLower().IndexOf("/ps/") != -1)
        {
            TextureImporter textureImporter = (TextureImporter)assetImporter;
            if (textureImporter.textureType != TextureImporterType.Sprite)
            {
                textureImporter.textureType = TextureImporterType.Sprite;
                textureImporter.spriteImportMode = SpriteImportMode.Single;
                textureImporter.alphaIsTransparency = true;
                textureImporter.mipmapEnabled = false;
                textureImporter.SaveAndReimport();
                //Debug.Log("Texture Insert :"+ assetPath);
            }
        }
    }
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Thinbug

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值