A few of the Textures in my game are downloaded at runtime from a website. Here is the code I use to download and store the texture:
/// <summary>
/// The texture used to render the sprite.
/// </summary>
private Texture2D mTexture;
/// <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);
mTexture = Texture2D.FromStream(GameObjectManager.pInstance.pGraphicsDevice, ms);
}
}
}, null);
}
I updated MonoGame.Android a while back to fix the issue with all textures turning black on resume. This one seems to be specific to these downloaded textures.
Here is what the texture data looks like after resuming the app:
Comparing that with the working version (prior to resume) I can see that glTexture has changed from 470 to -1 (the value on construction). So perhaps these textures get rebuilt or something behind the scenes?
I noticed this in the log:
[MonoGame] End reloading graphics content
Which seems to imply that graphic content had to be reloaded on resume. Perhaps I need to re-download all textures again too?