Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
962 views
in Technique[技术] by (71.8m points)

json - Unity3d WWW-class is very slow with android

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 ...

What did i done wrong ?

Here is my code :

string url = "SomeURL/app.Json";
WWW www = new WWW (url);
www.threadPriority = ThreadPriority.High;
DebugText.text = "get json started";
yield return www;
DebugText.text = "Json Downloaded";

PS1 : I used the same network for both devices.

PS2 : Json file is less than 1 KB.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...