我正在使用Monogame在iOS上开发游戏。是否可以使用从互联网下载的图像初始化texture2D?如果是的话,我可以效仿任何样品吗?非常感谢。
答案 0 :(得分:0)
我不确定这是否与iOS 1对1映射,但这是我在Android MonoGame上的方式。大部分代码来自Stack上的另一篇文章,但不幸的是我无法记住帖子!
/// <summary>
/// Downloads a texture based on a URL path, and stores it in this sprites texture.
/// </summary>
/// <param name="URL">The path to the file.</param>
private void DownloadTexture(String URL)
{
HttpWebRequest request = HttpWebRequest.Create(new Uri(URL)) as HttpWebRequest;
request.BeginGetResponse((ar) =>
{
HttpWebResponse response = request.EndGetResponse(ar) as HttpWebResponse;
using (Stream stream = response.GetResponseStream())
{
using (MemoryStream ms = new MemoryStream())
{
int count = 0;
do
{
byte[] buf = new byte[1024];
count = stream.Read(buf, 0, 1024);
ms.Write(buf, 0, count);
} while (stream.CanRead && count > 0);
ms.Seek(0, SeekOrigin.Begin);
/*Texture2D*/ mTexture = Texture2D.FromStream(GameObjectManager.pInstance.pGraphicsDevice, ms); //.PreMultiplyAlpha();
}
}
}, null);
}