如何在 Unity for Android 中将 System.IO.Stream 转换为纹理?

How to convert System.IO.Stream into a Texture in Unity for Android?(如何在 Unity for Android 中将 System.IO.Stream 转换为纹理?)

本文介绍了如何在 Unity for Android 中将 System.IO.Stream 转换为纹理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Unity 中构建客户端 Android 应用程序,当它从 AWS S3 服务器下载 jpg 时,结果以 System.IO.Stream 的形式返回.

I'm building a client-side Android app in Unity, and when it downloads a jpg from an AWS S3 server, the result comes back as a System.IO.Stream.

但是,我对 Mono 和 .Net 的了解有限,这意味着我正在努力弄清楚如何将 System.IO.Stream 数据块转换为 Unity 中的纹理,然后我可以在场景中的四边形上进行设置.

However my limited knowledge of Mono and .Net means that I'm struggling to figure out how to turn this System.IO.Stream blob of data into a Texture in Unity, that I can then set on a quad in my scene.

我在网上看到了一些很有前途的代码示例,例如:var img = Bitmap.FromStream(stream);但据我所知,Unity for Android 不支持 System.Drawing.Bitmap - 有人有什么建议吗?

I've seen promising examples of code online like: var img = Bitmap.FromStream(stream); yet System.Drawing.Bitmap is not supported in Unity for Android as far as I can tell - does anyone have any suggestions?

提前致谢!

(我用来从 AWS S3 下载的确切示例代码是 GetObject() 函数,可以在此处找到 http://docs.aws.amazon.com/mobile/sdkforunity/developerguide/s3.html,但在他们的示例中,他们使用 System.IO.StreamReader仅适用于读取文本而不是图像的字节数据)

(The exact example code I'm using to download from AWS S3 is the GetObject() function that can be found here http://docs.aws.amazon.com/mobile/sdkforunity/developerguide/s3.html, but in their example they use a System.IO.StreamReader which only works with reading in text not byte data for images)

推荐答案

您正在寻找 LoadImage 函数来自 Texture2D 类.该函数将PNG/JPG图像字节数组转换为纹理.

You are looking for the LoadImage function from the Texture2D class. This function converts PNG/JPG image byte array into a texture.

Texture2D tex = new Texture2D(2, 2);
tex.LoadImage(stream);

stream 变量必须是来自互联网/AWS S3 服务的字节数组(byte[]).

The stream variable must be byte array(byte[]) from the internet / AWS S3 serve.

这篇关于如何在 Unity for Android 中将 System.IO.Stream 转换为纹理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在 Unity for Android 中将 System.IO.Stream 转换为纹理?

基础教程推荐