Unity3d WWW-class 使用 android 非常慢 [英] Unity3d WWW-class is very slow with android
问题描述
我想使用 WWW-class 下载一个简单的 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.
从 这里 因为这个例子将统一ThreadPool
,如果你想使用Unity API,你需要UnityThread
作为来自另一个 的
.Text
组件线程
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.
这篇关于Unity3d WWW-class 使用 android 非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!