Android的Unity3d WWW类非常慢 [英] Unity3d WWW-class is very slow with android
问题描述
我想使用WWW类下载一个简单的json. 我的问题是,在android设备中,它需要3到4秒才能完成任务,但是在编辑器中,它需要几毫秒才能完成...
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 ...
我做错了什么?
这是我的代码:
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:我为两个设备使用了相同的网络.
PS1 : I used the same network for both devices.
PS2:Json文件小于1 KB.
PS2 : Json file is less than 1 KB.
推荐答案
没有设置可以加快WWW
的速度.如果速度较慢,则说明它要么在移动设备上实施不佳,要么设备又旧又慢.请注意,您的计算机通常在大多数情况下都比移动设备快,因此这可能是您认为速度较慢的原因.
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.
替代品:
1 .现在,让我们假设问题出在WWW
上.有一个新的Unity API,应该替换WWW
.那就是 UnityWebRequest
API.
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.
这真的很容易使用:
IEnumerator makeReuest()
{
UnityWebRequest www = UnityWebRequest.Get("YourURL");
yield return www.Send();
string text = www.downloadHandler.text;
}
2 .使用C#WebRequest
发出请求.将代理变量设置为null
,因为这可以加快请求的速度.您必须在另一个Thread
中使用它,或者使用其异步功能发出请求,否则,您将冻结游戏直到请求完成.
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.
从UnityThread. com/ScriptReference/UI.Text.html"rel =" nofollow noreferrer> Text
组件来自另一个Thread
.
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.
这篇关于Android的Unity3d WWW类非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!