c# - 通过代码使 Texture2D 在 Unity 中可读

标签 c# unity3d texture2d

<分区>

我有一些要转换为 .png 图像文件的 AssetBundle。

它们是 Texture2D Assets ,但问题是它们不是 Read Enable,当 我尝试使用

将它们转换为 PNG
var _bytes = _texture2d.EncodeToPNG();

命令,我收到以下错误消息:

Texture 'name of a texture' is not readable, the texture memory can not be accessed from scripts. You can make the texture readable in the Texture Import Settings.

我真的无法访问纹理导入设置,因为它们来自 Assets 包,而且一切都是用代码制作的。

有人对此有解决方法吗?

谢谢

最佳答案

这是一个可行的解决方案:

public static void SetTextureImporterFormat( Texture2D texture, bool isReadable)
{
    if ( null == texture ) return;

    string assetPath = AssetDatabase.GetAssetPath( texture );
    var tImporter = AssetImporter.GetAtPath( assetPath ) as TextureImporter;
    if ( tImporter != null )
    {
        tImporter.textureType = TextureImporterType.Advanced;

        tImporter.isReadable = isReadable;

        AssetDatabase.ImportAsset( assetPath );
        AssetDatabase.Refresh();
    }
}

关于c# - 通过代码使 Texture2D 在 Unity 中可读,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25175864/

相关文章:

c# - native C# : Reconstructing a Decimal number from its bit representation

c# - 统一的 Firebase 远程配置仅获取默认值而不是真实值

c# - 如何淡出并破坏负载音频

c# - 如何翻转内存中的图像?

c# - 在 visual studio 2013 中设置发布项目

c# - 以编程方式完成时,http 添加 sslcert 失败

c# - 如何使用拇指使 WPF 弹出窗口可拖动?

c# - 如何在谷歌云平台控制台中启用 'Vector Tile API'?

c# - 如何检查 Texture2D 是否透明?

c# - 在 XNA 中调整和加载 texture2d