How to convert System.IO.Stream into a Texture in Unity for Android?(如何在 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 转换为纹理?
基础教程推荐
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- WPF 模态进度窗口 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
