判断资源贴图是否有alpha

本文介绍了一个Unity编辑器脚本,该脚本可以自动调整选定的Texture2D资源的最大尺寸和导入格式。根据纹理是否有Alpha通道及其宽高比,脚本会相应地调整纹理的大小和压缩格式。

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

/*
  modfly selected textures`s maxSize and ImportFormat
  bool hasAlpha = true;
  if(hasAlpha)then(texture.size/2 and trueColor)else(16bit)

  2014.05.27
*/

using UnityEngine;
using System.Collections;
using UnityEditor;
public class modflyTextures : ScriptableObject{

    [MenuItem ("Custom/modflyTextures")]
    static void processTexture_Auto() { 
        processTexture();
    }

    static void processTexture() { 
        
        Object[] textures = GetSelectedTextures(); 
        Selection.objects = new Object[0];
        foreach (Texture2D tex in textures)  {
            string path = AssetDatabase.GetAssetPath(tex); 
            TextureImporter importerTexture = AssetImporter.GetAtPath(path) as TextureImporter; 

            if(importerTexture.DoesSourceTextureHaveAlpha() ==  true){
                if(tex.width >= tex.height )
                    importerTexture.maxTextureSize = tex.width/2;
                else importerTexture.maxTextureSize = tex.height/2;
                
                importerTexture.textureFormat = TextureImporterFormat.AutomaticTruecolor;
            }else{
                if(tex.width >= tex.height )
                    importerTexture.maxTextureSize = tex.width;
                else importerTexture.maxTextureSize = tex.height;

                importerTexture.textureFormat = TextureImporterFormat.AutomaticCompressed;
            }
            AssetDatabase.ImportAsset(path); 
        }
    }

    static Object[] GetSelectedTextures() 
    { 
        return Selection.GetFiltered(typeof(Texture2D), SelectionMode.DeepAssets); 
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值