We are developing a mapping app, similar to Google Earth, on a 2D tile based system.
At any given time in view there are 30-50 tiles, each with a unique sprite and texture. The textures are downloaded from a provider and they should update on pan/zoom.
To make it fluid, all textures should be downloaded and rendered in under 1s, assuming good download speed.
The problem is that by using HTTPClient + HttpResponseMessage, there is a huge difference is speed using the same code in editor vs build, with the editor being >5 times faster (verified by logging and network chart in Task Manager). The issue arises on a particular line of code that calls await _clinet.GetAsync(url), which is editor seems to be parallelized, while in build is sequential, or at least way way slower.
foreach(var tile in tiles) //30-50 tiles
{
StartCoroutine(GetMapTile(tile));
}
public IEnumerator GetMapTile(MapTile tile)
{
//some checks here
GetTileTexture(tile);
yield return null;
}
private async void GetTileTexture(MapTile Mtile)
{
//url creation here
//_client is reused by all of them
using (HttpResponseMessage response = await _client.GetAsync(url)) //problem arises on this line
{
if (response.IsSuccessStatusCode)
{
// Get response as byte array
var bytes = await response.Content.ReadAsByteArrayAsync();
//process it and apply to tile
}
}
}
If using UnityWebRequest without awaits there are no more differences in performance, but it’s way slower than HTTPClient (at least 3 times slower)
Things i have tried:
-using Task.Run for each tile and putting the request inside that, using await on the task itself, while the request was/wasn’t waited (await or .Result) => no change in build
-using 1 Task.Run and having a foreach request all tiles inside that task with await => no change, with .Result => same performance in Editor and build but way to slow
-doing it on the main thread but with a single coroutine and a foreach with await for every tile =>no change
How would i be able to fix it so that the build has the same speed as the Editor given the code example above. What is causing this problem?