Unity网络(二)-Unity3D中的网络

本文介绍了Unity3D中的网络通信,包括弃用的WWW模块的Get和Post请求,UnityWebRequest的使用及其GET、POST案例,以及HttpWebRequest的代码演示。内容覆盖网络下载、Json解析、斗鱼API实战和项目API请求。
摘要由CSDN通过智能技术生成

正所谓:没有网络是没有灵魂的

  • 欢迎关注公众号:雷潮课堂
    公众号二维码.jpg

Unity3D中的网络

一、WWW

1-1 Get
1-2 Post
1-3 案例(网络下载,本地存储与读取)

二、UnityWebRequest

2-1 Get
2-2 Post
2-3 案例(Json解析,展示)
2-4 斗鱼API请求实战(Get)
2-5 项目API请求实战(Post)

三、HttpWebRequest

3-1 代码演示

一、WWW

Unity中的WWW(Unity2017以后已标记为弃用状态,但是仍然可以用它来入门)
WWW用于下载资源和打开Web网站,包含:Get/Post请求,本地资源文件下载,服务器资源下载。关于使用www方式加载AssetBundle资源,可以关注文章AssetBundle使用

1-1.Get

1.一个链接中,参数是包括在链接中的,链接中的?后面都是参数;
2.服务器不会自动分配空间,传输的内容会比较小。

    string urlPath = "http://www.....";//资源网络路径
    string file_SaveUrl = @"D:\test.rar";//资源保路径
    FileInfo file; 
    void Start ()
    {
        file = new FileInfo(file_SaveUrl);
        Debug.Log(file_SaveUrl);
        StartCoroutine(DownFile(urlPath));
    }

   
    /// <summary>
    /// 下载文件
    /// </summary>
    IEnumerator DownFile(string url)
    {
        WWW www = new WWW(url);
        yield return www;
        if (www.isDone)
        {
            Debug.Log("下载完成");
            byte[] bytes = www.bytes;
            CreatFile(bytes);
        }
    }

    /// <summary>
    /// 创建文件
    /// </summary>
    /// <param name="bytes"></param>
    void CreatFile(byte[] bytes)
    {
        Stream stream;
        stream = file.Create();
        stream.Write(bytes, 0, bytes.Length);
        stream.Close();
        stream.Dispose();
}
1-2.Post

1.参数在表单里面。
2.服务器在底层检测到是Post请求,则会自动分配一个大的空间
3.例如声音,压缩包,视频等比较大的数据

  • WWW的使用
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class LoginWWW : MonoBehaviour {

    string url;
    public Image img;
    //public InputField put;
	void Start () {
        url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1540877217182&di=9d56ef557a007c78b543278db41cbb28&imgtype=0&src=http%3A%2F%2Fb.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2Fd6ca7bcb0a46f21fd757a52ffb246b600c33ae6f.jpg";
        // Get
        StartCoroutine(GetLoadBg(url));

        // Post  请求头,请求体
        //Dictionary<string, string> dic = new Dictionary<string, string>();
        //dic.Add("uEmail", "136087399@qq.com");
        //dic.Add("uPwd", "1314521");
        //StartCoroutine(PostLoadLogin(url,dic));
    }

    // Get
    IEnumerator GetLoadBg(string url)
    {
        WWW w = new WWW(url);
        yield return w;
        // 返回字节数的长度
        //Debug.Log(w.size);
        // 打印响应头
        //foreach (var item in w.responseHeaders)
        //{
        //    Debug.Log(item);
        //}
        //  错误为空 ||下载完毕 || 图片下载完毕
        if (w.error == null || w.isDone == true || w.progress == 1)
        {
            img.sprite = Sprite.Create(w.texture, new Rect(0, 0, Screen.width, Screen.height), new Vector2(0.5f, 0.5f));
        }
        else
        {
            Debug.Log(w.error);
        }
    }

    // Post
    IEnumerator PostLoadLogin(string url, Dictionary<string, string> dic)
    {
        WWWForm f = new WWWForm();
        foreach (KeyValuePair<string,string> item in dic)
        {
            f.AddField(item.Key, item.Value);
        }
        WWW w = new WWW(url,f);
        yield return w;
        if (w.isDone)
        {
            if (w.error == null)
            {
                Debug.Log("加载成功");
            }
            else
            {
                Debug.Log(w.error);
            }
        }
        else
        {
            Debug.Log("正在下载");
        }
    }
}
1-3.案例
  • 网络下载文件,增加本地读取
    string url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1594893269883&di=8acc2ec9ae60c906032b15feafe988ee&imgtype=0&src=http%3A%2F%2Fi1.hdslb.com%2Fbfs%2Farchive%2Fd42ccebee956f1c275d33f12179d804a22590e92.jpg";
    public RawImage raw;
    int index = 0;
  
    private void OnGUI()
    {
        if (GUILayout.Button("网络加载图片"))
        {
            index++;
            StartCoroutine(LoadImage(url));
        }
        if (GUILay
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值