WWW 资源下载与表单提交

在注册与验证用户信息,以及非即时通信的游戏中,我们可以使用WWW类使用短链接来完成客户端与服务器数据的通信,今天我们将使用用POST方法来完成的用户注册与登录,在最后介绍下其它资源的加载。

首先使用POST完成注册:
场景中两个InputField用于输入名字与密码,一个Button提交注册。
Button绑定方法如下:

 1 using UnityEngine;
 2 using System.Collections;
 3 using UnityEngine.UI;
 4  
 5 public class RegisterAndLogin : MonoBehaviour 
 6 {
 7     private string registerURL;
 8     private InputField _name;
 9     private InputField _pwd;
10      
11     void Awake()
12     {
13         _name = GameObject.Find("Name").GetComponent<InputField>();
14         _pwd = GameObject.Find("Pwd").GetComponent<InputField>();
15         registerURL = "http://..../wwwRegister.php";
16     }
17  
18     public void OnClickRegister () 
19     {
20         //定义WWWForm类用于存放POST的字段
21         WWWForm registerForm = new WWWForm();
22         //添加对应字段到表单,注意KEY要与服务器上一致
23         registerForm.AddField("name", _name.text);
24         registerForm.AddField("password", _pwd.text);
25         //协程开始注册
26         StartCoroutine(RegisterFun(registerURL, registerForm));
27     }
28  
29     IEnumerator RegisterFun(string url, WWWForm form)
30     {
31         WWW regi_www = new WWW(url, form);
32         yield return regi_www;
33         //得到服务器返回信息
34         string callBack = regi_www.text.Trim();
35         //服务器返回 “1”,注册成功
36         if (callBack.Equals("1"))
37         {
38             Debug.Log("congratulations! register OK! (call back code:" + callBack + ")");
39         }
40         //否则失败
41         else
42         {
43             Debug.Log("register failed! (call back code:" + callBack + ")");
44         }
45     }
46 }

运行场景,可以看到注册成功如下:
regiOk
重复注册导致失败:
regiFailed
数据库中已经有数据咯:
QQ截图20151008165646

登录和注意相似,只是需要在服务器上加以判断,并返回信息就行,这里就不写了。

再使用读取个纹理图片的:

 1 void Start()
 2 {
 3 string urlTexture = "https://www.baidu.com/img/bd_logo1.png";
 4 StartCoroutine (LoadTexture(urlTexture));
 5 }
 6  
 7 //加载一张图片纹理
 8 IEnumerator LoadTexture(string url)
 9 {
10 WWW www = new WWW (url);
11 yield return www;
12 this.renderer.material.mainTexture = www.texture;
13 }

最后用GET来个读个JSON吧:
服务器上根据请求查询数据后格式化如下:
[{“id”:”1″,”name”:”u5f6cu5f6c”,”sex”:”u5973″,”age”:”19″},
{“id”:”2″,”name”:”u8d85u8d85″,”sex”:”u7537″,”age”:”24″},
{“id”:”8″,”name”:”u5a01u5a01″,”sex”:”u7537″,”age”:”20″}]
脚本代码:

 1 using UnityEngine;
 2 using System.Collections;
 3 using LitJson;
 4  
 5 public class wwwScript : MonoBehaviour 
 6 {
 7     //JSON模板类
 8     public class Person
 9     {
10         public string id ;
11         public string name;
12         public string sex;
13         public string age;
14     }
15  
16     void Start ()
17     {
18     //通过“?+键值对”指定请求内容
19     StartCoroutine (LoadText ("http://.../wwwGet.php?tablename=person"));
20     }
21  
22     IEnumerator LoadText(string url)
23     {
24         WWW www = new WWW (url);
25         yield return www;
26         Debug.Log ("loaded total data:" + www.text);
27             //Json处理
28         Person[] persons = JsonMapper.ToObject <Person[]> (www.text);
29         foreach (Person item in persons)
30         {
31             Debug.Log (item.id + item.name + item.age);
32         }
33     }
34 }

OK咯,相信通过上面的例子,大家一定可以举一反三,应付绝大部分WWW类数据提交与请求的问题了^_^

原文地址:https://www.cnblogs.com/zhenlong/p/4874631.html