unity www的get和post

untiy 的WWW请求大致分为三个,Get请求、Post请求、本地请求,使用时一般使用协程,请求默认走tcp协议

Get 请求

  • 特点:
    • 传递比较小的东西
    • 参数在 链接 里面 后面都是参数
    • 服务器不会自动分配空间 可能几k空间
Public IEnumerator SendGet (string url){
	//构造函数url代表下载地址
    WWW www = new WWW(url);
	
	//等待www响应(下载)完成
    yield return www;

    //判断下载是否有有错
    if (string.IsNullOrEmpty(www.error)){
		//TODO...
	}
}

Post 请求

  • 特点:
    • 传递比较大的东西
    • 参数在表单里面
    • 服务器在底层看见post 会自动分配一个大的空间 可能1M空间(video、move…)
Public IEnumerator SendPost (string url, WWWForm form){
	
	
    WWW www = new WWW(url, form);

    yield return www;

    if (string.IsNullOrEmpty(www.error)){//TODO..}
}

加载本地文件

  • 更具不同平台有不同目标目录

    • Windows:file:/// + path
    • Ios:file:// + path
    • Android:jar:file:// + path
  • 其中Application.platform可以判断当前运行环境在什么平台执行

//添加目标平台前缀
public string SelectionTargetPath (string _path) {
	//win
	if (Application.platform == RuntimePlatform.WindowsEditor ||
        Application.platform == RuntimePlatform.WindowsPlayer) {
        _path = "file:///" + _path;
    //android
    } else if (Application.platform == RuntimePlatform.Android) {
        _path = "jar:file://" + _path;
    //ios
    } else {
        _path = "file://" + _path;
    }
    return _path;
}

例如:

void Start () {
	var path = Application.dataPath + "/test.cs";
	path = SelectionTargetPath (path);
	StartCoroutine (SendGet (path));
}
  • 其中Application.dataPath :返回当前asset文件夹路径
  • test.cs 在asset文件中创建的测试脚本
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值