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

查看:85
本文介绍了如何在Android版Unity中将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()函数,可以在这里找到

(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)

推荐答案

您正在寻找 Texture2D 类中的> LoadImage 函数.该功能将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变量必须是Internet/AWS S3服务中的字节数组(byte[]).

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

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

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆