来自于:天地会舵主
一:Unity网络工具类:
Unity原来有个Http访问的工具类WWW,在新版本的Unity中,推出了新的工具类UnityWebRequest,可以代替原来的WWW类。我们之后尽量使用UnityWebRequest进行请求即可。
二:简单的Get访问
UnityWebRequest包含多个静态方法,可以创建一个UnityWebRequest对象,调用SendWebRequest正式发送请求。在协程返回结果后,需要判断是否有错误,再做处理。
IEnumerator GetText(){//直接使用Get方法即可UnityWebRequest www = UnityWebRequest.Get("http://127.0.0.1:9997/gameInit?uid=7");//设置请求超时时间www.timeout = 5;//send的时候,该请求才开始执行yield return www.SendWebRequest();if (www.isNetworkError || www.isHttpError){Debug.Log(www.error);}else{// Show results as textDebug.Log(www.downloadHandler.text);// Or retrieve results as binary databyte[] results = www.downloadHandler.data;}}
三:Http post访问
项目中post访问提交的数据都是json格式的,这里需要对提交的数据uploadHandler做处理,把提交的数据抓换为byte数组。
public IEnumerator PostRequest(string url, string jsonString, Action<string> callback){using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST")){byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(jsonString);webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();yield return webRequest.SendWebRequest();if (webRequest.isHttpError || webRequest.isNetworkError){Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text);if (callback != null){callback(null);}}else{if (callback != null){callback(webRequest.downloadHandler.text);}}}}
外面直接调用即可,本质上就是初始化一个WebRequest,然后设置对应的上传和下载Handler。最后请求完成调用对应的委托即可
四:下载AssetBundle
IEnumerator GetAssetBundle() {const string url1 = @"http://127.0.0.1:8080/mat";const string url2 = @"http://127.0.0.1:8080/image";UnityWebRequest request1 = UnityWebRequestAssetBundle.GetAssetBundle(url1); //Unity网络请求AssetBundle.获取资源(网络地址1)UnityWebRequest request2 = UnityWebRequestAssetBundle.GetAssetBundle(url2); //传入地址2yield return request1.SendWebRequest(); //发送Web请求1yield return request2.SendWebRequest(); //发送web请求2AssetBundle ab1 = DownloadHandlerAssetBundle.GetContent(request1); //下载资源,获取连接请求1,返回AssetBundle资源AssetBundle ab2 = DownloadHandlerAssetBundle.GetContent(request2); //获取连接请求2,返回AssetBundle资源//获取AsseBundle资源,之后可以做任意事情}
五:接收Texture
可以使用 UnityWebRequestTexture.GetTexture方法,从远程服务端获取一个Texture文件。这个功能和UnityWebRequest.GET方法非常类似,但是它对下载和存储Texture文件做了优化,提高了处理效率。UnityWebRequest.Texture同样需要一个字符串参数,这个参数就是要下载的资源的URL地址。
IEnumerator GetTexture(){//直接封装好下载图片的函数,不需要再讲字节转换为图片。该类内部自动绑定了DownloadHandlerTextureUnityWebRequest www = UnityWebRequestTexture.GetTexture("http://127.0.0.1:9999/BidCardBorder.png");yield return www.SendWebRequest();if (www.isHttpError || www.isNetworkError){Debug.LogError(www.error);}else{Texture myTexture = ((DownloadHandlerTexture)www.downloadHandler).texture;this.GetComponent<MeshRenderer>().material.mainTexture = myTexture;}}
六:Post键值对方式请求
有时候,我们在传递参数的时候,使用的是Key-Value的方式,代码如下所示
IEnumerator PostKeyValue(){WWWForm form = new WWWForm();//键值对form.AddField("name", "zhangsan");form.AddField("password", "123456");//请求链接,并将form对象发送到远程服务器UnityWebRequest webRequest = UnityWebRequest.Post("http://127.0.0.1:8080/login", form);yield return webRequest.SendWebRequest();if (webRequest.isHttpError || webRequest.isNetworkError){Debug.Log(webRequest.error);}else{Debug.Log("发送成功");}}
七:普通Get请求两种方式对比
使用系统提供的UnityWebRequest.Get的时候,已经默认帮我们绑定好了DownloadHandler,无需我们自己设置。当我们使用自己的时候,必须进行对应的设置,代码如下:
IEnumerator SendRequest(){Uri uri = new Uri("http://127.0.0.1:9997/gameInit?uid=7");//1:创建UnityWebRequest对象UnityWebRequest uwr = new UnityWebRequest(uri);uwr.method = "GET";uwr.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();//2:创建UnityWebRequest对象,与上面的等价// UnityWebRequest uwr = UnityWebRequest.Get("http://127.0.0.1:9997/gameInit?uid=7");uwr.timeout = 5;//设置请求超时时间yield return uwr.SendWebRequest(); //等待返回请求的信息if (uwr.isHttpError || uwr.isNetworkError) //如果其 请求失败,或是 网络错误{Debug.LogError(uwr.error); //打印错误原因}else //请求成功{Debug.Log("请求成功");//使用DownloadHandler下载数据Debug.Log(uwr.downloadHandler.text);//解析对应的Json数据即可}}
