unity 设置texture2d的格式

本文介绍了一个用于设置Unity中2D纹理格式的接口方法。该方法允许开发者为不同平台(如PC、Android和iOS)指定特定的纹理格式及大小,同时还能控制是否可读等属性。

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

  最近需要设置2d texture的格式:

 

      写了个接口:

    private static void SetTextureImportFormat(UnityEngine.Object textureObject,
                                        int maxSize,
                                        bool isReadable,
                                        TextureImporterFormat pcFormat,
                                        TextureImporterFormat androidFormat,
                                        TextureImporterFormat iosFormat)
    {
        if (textureObject != null)
        {
            if (textureObject is Texture2D)
            {
                string path = AssetDatabase.GetAssetPath(textureObject);
                TextureImporter ti = TextureImporter.GetAtPath(path) as TextureImporter;
                ti.mipmapEnabled = false;
                ti.wrapMode = TextureWrapMode.Clamp;
                ti.textureType = TextureImporterType.Default;
                ti.filterMode = FilterMode.Point;
                ti.isReadable = isReadable;
                //--设置平台格式--
                TextureImporterPlatformSettings importerSettings_PC = new TextureImporterPlatformSettings();
                importerSettings_PC.overridden = true;
                importerSettings_PC.name = "Standalone";
                importerSettings_PC.textureCompression = TextureImporterCompression.Uncompressed;
                importerSettings_PC.maxTextureSize = maxSize;
                importerSettings_PC.format = pcFormat;

                TextureImporterPlatformSettings importerSettings_Andorid = new TextureImporterPlatformSettings();
                importerSettings_Andorid.overridden = true;
                importerSettings_Andorid.name = "Android";
                importerSettings_Andorid.textureCompression = TextureImporterCompression.Uncompressed;
                importerSettings_Andorid.maxTextureSize = maxSize;
                importerSettings_Andorid.format = androidFormat;

                TextureImporterPlatformSettings importerSettings_IOS = new TextureImporterPlatformSettings();
                importerSettings_IOS.overridden = true;
                importerSettings_IOS.name = "iPhone";
                importerSettings_IOS.textureCompression = TextureImporterCompression.Uncompressed;
                importerSettings_IOS.maxTextureSize = maxSize;
                importerSettings_IOS.format = iosFormat;
                //-----

                ti.SetPlatformTextureSettings(importerSettings_PC);
                ti.SetPlatformTextureSettings(importerSettings_Andorid);
                ti.SetPlatformTextureSettings(importerSettings_IOS);
                ti.SaveAndReimport();
                AssetDatabase.ImportAsset(path);
            }
        }
    }

 

转载于:https://www.cnblogs.com/sun-shadow/p/9324655.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值