当前位置:   article > 正文

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

unity multipartformdatasection

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);
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

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);
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

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);
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

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);
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

二.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]));
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

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"));
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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);
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/387557?site
推荐阅读
相关标签
  

闽ICP备14008679号