HttpClient 和 Unity 的 UnityWebRequest/WWW API 的区别 [英] Difference between HttpClient and Unity's UnityWebRequest/WWW API

查看:49
本文介绍了HttpClient 和 Unity 的 UnityWebRequest/WWW API 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Unity 现在支持 .NET 4.5,我可以使用 System.Net.Http 命名空间中的 HttpClient 类.这很好,因为我有现有的客户端库,因此很容易重用.

Unity now supports .NET 4.5 and I'm able to use the HttpClient class from the System.Net.Http namespace. That is great since I have existing client libraries which are therefore easy to reuse.

有人知道使用 mono HttpClientUnity 自己的网络 类之间的内部区别吗?

Does anyone know the internal difference between using the mono HttpClient and Unity's own networking classes?

恐怕 HttpClient 没有针对不同平台进行优化,因此可能会导致麻烦或速度较慢.

I'm afraid that the HttpClient is not as optimized for the different platforms and might therefore cause trouble or be slower.

推荐答案

两者有很多不同.

  • UnityWebRequest 不允许您使用更改某些标头.使用 HttpClient,您几乎可以更改任何标头.

  • UnityWebRequest won't allow you to use change some headers. With HttpClient, you can change almost any header.

UnityWebRequest 的使用无需担心线程或异步的东西.您所做的就是使用协程等待请求.整个线程的东西已经在原生端为你完成了.

UnityWebRequest is made to be used without worrying about threads or async stuff. All you do is use coroutine to wait for the request. The whole thread stuff is already done for you on the native side.

某些平台不支持 System.Net 命名空间中的任何内容.其中之一是 WebGL.这意味着当您将平台切换到 WebGL 时,HttpClient 甚至都不会编译.UnityWebRequest 与 WebGL 配合良好.

Some platforms don't support anything from the System.Net namespace. One of this is WebGL. This means that HttpClient will not even compile when you switch your platform to WebGL. UnityWebRequest works fine with WebGL.

UnityWebRequest 是为了方便在内存中下载数据并将数据转换为 Unity 资源,例如 AudioClipVideoClipAssetBundleTexture2D 等等.使用 HttpClient,您将不得不编写大量代码来检索这些数据,或者可能必须在收到数据后将数据保存在光盘上,以便能够将它们转换为 Unity 资源形式.

UnityWebRequest is made to make it easier to download data in memory and convert the data into Unity resources such as AudioClip, VideoClip, AssetBundle, Texture2D and more. With HttpClient, you will have to write lots of codes just retrieve such data or probably have to save the data on the disc after receiving them just to be able to convert them into Unity resources form.

想要通过 UnityWebRequest 使用 SSL?祝你好运!您最终只会遇到模棱两可的未知错误" 错误.当您的 HttpClient 中涉及 SSL 时是选项.

Want to use SSL with UnityWebRequest? Good luck with that! You will only end up with the ambiguous "unknown error" error. When SSL is involved in your HttpClient is the option.

对于我刚才提到的所有内容,您应该移植您的代码以使用 UnityWebRequest,除非您正在执行 UnityWebRequest 不支持的操作.

With everything I just mentioned, you should port your code to use UnityWebRequest unless you are doing something that's not supported with UnityWebRequest.

这篇关于HttpClient 和 Unity 的 UnityWebRequest/WWW API 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆