Unity C# 网络学习(十)——UnityWebRequest(一)

Unity C# 网络学习(十)——UnityWebRequest(一)

UnityWebRequest与WWW的区别

  • UnityWebRequest将各种资源进行了拆分
  • UnityWebRequest比WWW的效率更高,有很低的GC产生
  • 更方便的上传数据

一.UnityWebRequest类获取数据

1.下载文本和二进制数据

    private IEnumerator LoadText()
    {
        UnityWebRequest unityWebRequest = UnityWebRequest.Get("http://192.168.1.103:8080/Http_Server/zzs.txt");
        yield return unityWebRequest.SendWebRequest();
        if (unityWebRequest.result == UnityWebRequest.Result.Success)
        {
            string text = unityWebRequest.downloadHandler.text;
            byte[] bytes = unityWebRequest.downloadHandler.data;
            Debug.Log(text);
            Debug.Log(bytes.Length);
            Debug.Log("文本下载完成!");
        }
        else
        {
            Debug.Log("下载失败:" + unityWebRequest.result);
        }
    }

2.下载图片数据

    private IEnumerator LoadTexture()
    {
        UnityWebRequest unityWebRequest =
            UnityWebRequestTexture.GetTexture("http://192.168.1.103:8080/Http_Server/xxx.jpg");
        yield return unityWebRequest.SendWebRequest();
        if (unityWebRequest.result == UnityWebRequest.Result.Success)
        {
            //方式一
            Texture2D tex2D1 = (unityWebRequest.downloadHandler as DownloadHandlerTexture)?.texture;
            //方式二
            Texture2D tex2D2 = DownloadHandlerTexture.GetContent(unityWebRequest);
            image.texture = tex2D2;
            Debug.Log("图片下载完成!");
        }
        else
        {
            Debug.Log("下载失败:" + unityWebRequest.result);
        }
    }

3.下载AssetBundle数据

    private IEnumerator LoadAb()
    {
        UnityWebRequest unityWebRequest =
            UnityWebRequestAssetBundle.GetAssetBundle("http://192.168.1.103:8080/Http_Server/photo.ywj");
        unityWebRequest.SendWebRequest();
        while (!unityWebRequest.isDone)
        {
            Debug.Log(unityWebRequest.downloadProgress);
            Debug.Log(unityWebRequest.downloadedBytes);
            yield return null;
        }

        if (unityWebRequest.result == UnityWebRequest.Result.Success)
        {
            //方式一
            AssetBundle assetBundle1 = (unityWebRequest.downloadHandler as DownloadHandlerAssetBundle)?.assetBundle;
            //方式二
            AssetBundle assetBundle2 = DownloadHandlerAssetBundle.GetContent(unityWebRequest);

            if (assetBundle1 != null) Debug.Log(assetBundle1.name);
            if (assetBundle2 != null) Debug.Log(assetBundle2.name);
            Debug.Log("图片下载完成!");
        }
        else
        {
            Debug.Log("下载失败:" + unityWebRequest.result);
        }
    }

4.下载音频数据

    private IEnumerator LoadAudioClip()
    {
        UnityWebRequest unityWebRequest =
            UnityWebRequestMultimedia.GetAudioClip("http://192.168.1.103:8080/Http_Server/music.mp3", AudioType.MPEG);
        yield return unityWebRequest.SendWebRequest();
        if (unityWebRequest.result == UnityWebRequest.Result.Success)
        {
            AudioClip clip = DownloadHandlerAudioClip.GetContent(unityWebRequest);
            audioSource.clip = clip;
            audioSource.Play();
            Debug.Log("音频下载成功!");
        }
        else
        {
            Debug.Log("下载失败:"+unityWebRequest.result);
        }
    }

二.UnityWebRequest类上传数据

1.上传数据类MultipartFormDataSection

		//======MultipartFormDataSection======
        //1.二进制字节数组
        dataList.Add(new MultipartFormDataSection(Encoding.UTF8.GetBytes("zzs666")));
        //2.字符串
        dataList.Add(new MultipartFormDataSection("zzs666"));
        //3.参数名,参数值
        dataList.Add(new MultipartFormDataSection("Name","zzs"));
        dataList.Add(new MultipartFormDataSection("Msg",new byte[1024]));

2.上传数据类MultipartFormFileSection

        //======MultipartFormFileSection======
        //1.二进制字节数组
        dataList.Add(new MultipartFormFileSection(Encoding.UTF8.GetBytes("zzs666")));
        //2.文件名,字节数组(常用)
        dataList.Add(new MultipartFormFileSection("上传的文件.jpg",File.ReadAllBytes(Application.streamingAssetsPath +"/test.jpg")));
        //3.字符串数据,编码格式,文件名(常用)
        dataList.Add(new MultipartFormFileSection("zzs!zzs!zzs!",Encoding.UTF8, "zzsTest.txt"));

3.Post发送数据相关

    private IEnumerator UpLoad()
    {
        List<IMultipartFormSection> data = new List<IMultipartFormSection>
        {
            new MultipartFormDataSection("Name", "MrTang"),
            new MultipartFormFileSection("Unity上传的文件.jpg",
                File.ReadAllBytes(Application.streamingAssetsPath + "/test.jpg")),
            new MultipartFormFileSection("zzs!zzs!zzs!", Encoding.UTF8, "zzsTest.txt")
        };
        UnityWebRequest unityWebRequest = UnityWebRequest.Post("http://192.168.1.103:8080/Http_Server/", data);
        yield return unityWebRequest.SendWebRequest();

        if (unityWebRequest.result == UnityWebRequest.Result.Success)
        {
            Debug.Log("上传完成!");
        }
        else
        {
            Debug.Log("上传失败!" + unityWebRequest.result + unityWebRequest.error);
        }
    }

你可能感兴趣的:(Unity,c#,unity,学习)