untiy 中的www读取文件

五、 www
5.1. 简介
简单的访问一个页面,通过一个指定url来创建一个www请求
5.2. http
http : 每点击 一次,都会返回一个结果 。
tcp与udp都是底层socket通信,socket比他们更深层次的底层。
tcp: 长链接, 一直发送消息 。 有顺序。1 2
udp: 短链接,没有顺序的 。 2 1
5.2.1. get请求
?tn=baiduimage&ps=1&ct=201326592&lm=-1&cl=2&nc=1&ie=utf-8&word=3d旋转矩阵公式
参数 在 url 中
5.2.2. post请求
http://kun.show.ghostry.cn/
post 请求 参数在表单里面。当发生post请求时,服务器会认为客户端在传送一个大的数据,同时服务器底层会分配一个大的空间。
随着网络发展,传送的数据量越来越大,音频、图片、视频等等,get请求已经不能满足需求,因此传送大数据就用 post .
都表示 从服务器 获取 数据。
5.3. 本地文件的加载
5.3.1. streamingAssetsPath
Application.streamingAssetsPath : 默认在工程下的Assets文件夹下,只要放在这个文件夹里面的东西,都会打包到真机上 。
该目录与Apk. 里面 Assets的目录一致
只能用www 读且不能写 。
5.3.2. persistentDataPath
Application.persistentDataPath : 可读 写文件 。 用文件系统 。
File StreamReader streamWriter .

Windows :
file:/// + 路径

IOS:
file:// + 路径

Android :
jar:file:// + 路径
5.4. 代码实现

    void Start () {
        //执行get请求
        StartCoroutine(SendGet("http://kun.show.ghostry.cn/?int=5"));

        string url = "http://kun.show.ghostry.cn";
        //实例化一个表单
        WWWForm form = new WWWForm();
        form.AddField("int", 9);
        //执行post请求
        StartCoroutine(SendPost(url, form));

        Debug.Log(Application.streamingAssetsPath);

        Debug.Log(Application.persistentDataPath);

        string url2 = Application.dataPath + "/Resources/test.html";
        LoadLocalFile(url2);
    }
    /// <summary>
    /// get request
    /// </summary>
    /// <param name="url"></param>
    /// <returns></returns>
    public IEnumerator SendGet(string url)
    {
        WWW www = new WWW(url);
        yield return www;
        if (string.IsNullOrEmpty(www.error))
        {
            Debug.Log(www.text);
        }
    }
    /// <summary>
    /// post request
    /// </summary>
    /// <param name="url">url</param>
    /// <param name="tmpParams">表单</param>
    /// <returns></returns>
    public IEnumerator SendPost(string url, WWWForm tmpParams)
    {
        WWW www = new WWW(url, tmpParams);
        yield return www;
        if (string.IsNullOrEmpty(www.error))
        {
            Debug.Log(www.text);
        }
    }
    /// <summary>
    /// 加载本地文件
    /// </summary>
    /// <param name="url"></param>
    public void LoadLocalFile(string url)
    {
        string result = "";
        if(Application.platform == RuntimePlatform.IPhonePlayer)
        {
            result = "file://" + url;
        }
        else if (Application.platform == RuntimePlatform.Android)
        {

            result = "jar:file://" + url;
        }
        else if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer)
        {
            result = "file:///" + url;
        }
        StartCoroutine(SendGet(result));
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值