Unity与服务区交互数据
2024-08-29 21:02:14
Unity与服务区交互数据
Unity可能在用的时候使用到登陆等需要与服务器交互数据。今天尝试使用了WWW类和WWWForm类来实现Get请求与Post请求。
1.WWW
Unity圣典解释:
WWW会返回一个新的WWW对象。当它被下载,其结果可以从返回对象中获取。这个函数创建和发送一个GET请求,流将自动开始下载响应。流创建之后,你必须等待它完成,然而可以访问已下载的数据。作为一个方面的流可以被中断,因此你可以容易的告诉Unity等待下载完成。你可以检查isDone属性来查看是否已经下载完成,或者yield自动等待下载物体,直到它被下载完成(不会影响游戏的其余部分)。
Variables变量
-
Returns the contents of the fetched web page as a string (Read Only).
通过网页获取并以字符串的形式返回内容(只读)。 -
Returns the contents of the fetched web page as a byte
array (Read Only).
以字节组的形式返回获取到的网络页面中的内容(只读)。 -
Returns an error message if there was an error during the
download (Read Only).
返回一个错误消息,在下载期间如果产生了一个错误的话。(只读) -
Is the download already finished? (Read
Only)
判断下载是否已经完成(只读)? -
How far has the download progressed (Read
Only).
下载进度有多少(只读)? -
How far has the upload progressed (Read
Only).
上传进度有多少(只读) -
Load an Ogg Vorbis file into the audio clip.
加载一个Ogg
Vorbis文件到音频剪辑。 -
The URL of this WWW request (Read Only).
该WWW请求的URL(只读)。 -
Streams an AssetBundle that can contain any kind of asset
from the project folder.
AssetBundle的数据流,可以包含项目文件夹中的任何类型资源。 -
Priority of AssetBundle decompression thread.
AssetBundle解压缩线程的优先级。
Constructors构造器
-
Creates a WWW request with the given URL.
用给定的URL创建一个WWW请求。
Functions函数
-
Replaces the contents of an existing Texture2D with an
image from the downloaded data.
利用一个从下载数据中的图像来替换现有Texture2D。 -
Loads the new web player data file.
加载新的web播放器数据文件。
Class Functions类函数
-
Encodes string into an URL-friendly
format.
字符串编码成一个URL的格式。 -
Decodes string from an URL-friendly
format.
从一个URL格式解码字符串。 -
Loads an assetBundle from the cache, or downloads it, in
case it is not cached.
从缓存加载一个资源包,如果没有被缓存,或从下载加载。
2.WWWForm
辅助类。用来生成表单数据,使用WWW类传递到web服务器。
Variables变量
-
(Read Only) Returns the correct request headers for posting the form using the WWW class.
(只读)为使用WWW类传递的表单返回一个正确的请求头。 -
(Read Only) The raw data to pass as the POST request body
when sending the form.
(只读)在发送表单的时,原始数据作为POST请求发送。
Constructors构造器
-
Creates an empty WWWForm object.
创建一个空的网页表单对象。
Functions函数
-
Add a simple field to the form.
添加一个简单的域到表单。 -
Add binary data to the form.
添加二进制数据到表单。
3.使用方法
这里试着从网上下载一个图片替换物体的贴图。
using System;
using UnityEngine;
using System.Collections;using System.Net;public class WWWZ : MonoBehaviour {void Start()
{string path= "http://a3.att.hudong.com/72/37/01200000194734134393377005543_s.jpg";
StartCoroutine(GoForm());
}
IEnumerator GoStart()
{
WWW www = new WWW(path);
yield return www; if (www.isDone)
{
GetComponent<Renderer>().material.mainTexture = www.texture;
}
}
}
接下来是想URL传递表单数据。
using System;
using UnityEngine;
using System.Collections;
using System.Net;
using System.Text;
using LitJson; public class WWWZ : MonoBehaviour { public string url = "http://192.168.16.118:8080/google";//接受表单的地址 void Start()
{ form = new WWWForm(); form.AddField("action","login");
form.AddField("username","abc");
form.AddField("password", ""); StartCoroutine(GoForm()); } IEnumerator GoForm()
{
WWW w=new WWW(url, form);
yield return w; if (w.error != null)
print(w.error);
else
Debug.Log(“提交成功!”); }
}
当然在可以和服务器简单的下载和提交信息后,我们甚至可以向服务器传递或从服务器获取xml,Json等信息,方便我们使用。
最新文章
- 掌握SortSet接口和Set接口的关系,以及常用方法。
- ecshop添加上传图片
- explict关键字
- 【翻译】Kinect v2程序设计(C++-) AudioBeam篇
- AngularJs学习笔记--directive
- [CODEVS3299]有序数组合并求第K大问题
- TalkingData Cocos2dx在android平台使用总结
- 接上一篇博客(解决-Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HOME environment variable and mvn script match. )
- Spring Boot1.5.4 连接池 和 事务
- PHP执行linux命令mkdir权限问题
- 基于SSM框架的文件上传
- PHP Ajax JavaScript Json 实现天气信息获取
- iOS9中如何注册远程通知
- mongodb 参数优化
- 集合之LinkedHashMap(含JDK1.8源码分析)
- AtCoder Grand Contest 026 (AGC026) E - Synchronized Subsequence 贪心 动态规划
- 移动端h5页面的那些坑
- Hive记录-配置远程连接(JAVA/beeline)
- kbmMW 5.08.10试用报告
- ubuntu-docker入门到放弃(三)images镜像管理