I want to download a simple json using WWW-class. My problem is that in android device it take 3 to 4 seconds to complete the task but in editor it done in milliseconds ...<
There is no settings to speed up WWW
. If it is slow then that means it is either implemented poorly on mobile devices or your device is old and slow. Note that your computer is faster than your mobile devices most of the time so that could be what you consider as being slow.
Alternatives:
1.Now, let's assume that WWW
is the problem. There is a new Unity's API that is supposed to replace WWW
. That's the UnityWebRequest API.
It's really easy to use:
IEnumerator makeReuest()
{
UnityWebRequest www = UnityWebRequest.Get("YourURL");
yield return www.Send();
string text = www.downloadHandler.text;
}
2.Use C# WebRequest
to make the request. Set the proxy variable to null
as this is known to speed up requests. You must use this in another Thread
or make the request with its async functions otherwise, you will freeze your game until the request is done.
Grab UnityThread
from here since this example will Unity ThreadPool
and you need UnityThread
if you want to use Unity API such as the Text component from another Thread
.
void Awake()
{
//UnityThread.initUnityThread();
downloadData();
}
void downloadData()
{
ThreadPool.QueueUserWorkItem(new WaitCallback(makeRequest));
}
private void makeRequest(object a)
{
string url = "";
string result = "";
var request = (HttpWebRequest)WebRequest.Create(url);
//Speed up
request.Proxy = null;
using (var response = (HttpWebResponse)request.GetResponse())
{
var encoding = Encoding.GetEncoding(response.CharacterSet);
using (var responseStream = response.GetResponseStream())
using (var reader = new StreamReader(responseStream, encoding))
result = reader.ReadToEnd();
}
UnityThread.executeInUpdate(() =>
{
//Use in Unity Thread
yourTextComponent.text = result;
});
}
Hopefully, one of those should speed up your request. If that did not happen then that's a limit on your device.