關於 Unity3D 網路請求的筆記,unity3d請求
Unity 指令碼 關於網路請求的方法有如下:
public WWW (string url, byte[] postData, Dictionary<string, string> headers)
public WWW (string url, byte[] postData, Hashtable headers) -----> deprecated
public WWW (string url, byte[] postData)
public WWW (string url, WWWForm form)
public WWW (string url)
很多方法呢,從文檔中可以看到。
但是正在我看書的過程中發現有個方法已經被棄置了,所以特意寫下筆記,增強記憶。
參考《Unity 3D/2D 手機遊戲開發》一書自學的。
但遺憾在Unity5 的時候有方法被棄置,本人的思路將會根據該書來記錄。
1.建立一個指令碼,選C#,名為WebManager。
2.將指令碼WebManager.cs 添加到一個對象上,即可觸發指令碼的事件。
3.開始編寫代碼。
3.1先編寫一個介面。
using UnityEngine;using System.Collections;public class WebManager : MonoBehaviour {//全域變數,用來接收資訊提示,初始化為“Nothing”。string m_info = "Nothing";void OnGUI() {GUI.BeginGroup (new Rect (Screen.width * 0.5f - 100, Screen.height * 0.5f - 100, 500, 200), "");//建立一個標籤,設定位置、大小,將接收資訊提示的全域變數作為標籤的常值內容。GUI.Label (new Rect (10, 10, 400, 30), m_info);//建立一個按鈕,設定位置、大小,按鈕上的標題為“Get Data”。if (GUI.Button (new Rect (10, 50, 150, 30), "Get Data")) {//這裡寫點擊按鈕所觸發的行為、事件。}//建立一個按鈕,設定位置、大小,按鈕上的標題為“Post Data”。if (GUI.Button (new Rect (10, 100, 150, 30), "Post Data")) {//這裡寫點擊按鈕所觸發的行為、事件。}GUI.EndGroup();}// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {}}
首先就來個簡單的方法吧。
Get請求是最簡單的,所以一般簡單的方法都是Get方法的。
3.2Get方法
先寫下這個IGetData() 函數。需要注意的是該函數傳回型別是迭代器 IEnumerator,通過這個可以進行協同調用。
IEnumerator IGetData() {//使用Get方式訪問HTTP地址WWW www = new WWW ("http://yococoxc.vicp.cc:9999/test/userprint.php?username=yococo&password=123456789");//等待伺服器的響應yield return www;//如果出現錯誤if (www.error != null) {//擷取伺服器的錯誤資訊m_info = www.error;yield return null;}//擷取伺服器的響應文本m_info = www.text;}
然後需要將上面的函數在Get對應的按鈕上設定好,以便觸發。
if (GUI.Button (new Rect (10, 50, 150, 30), "Get Data")) {StartCoroutine(IGetData());}說明:
StartCoroutine()方法的作用是啟動協同程式,執行指定的方法,當然執行的方法的傳回型別必須是IEnumerator。
關於程式所使用的PHP代碼將會在最後列出。
效果為:
3.3Post方法
Get方法就是如上那麼簡單,接下來就是Post方法,有些類似,但是比較麻煩,當然Post方法有很多優點的,預設你是知道的。
IEnumerator IPostData() {Dictionary<string,string> headers = new Dictionary<string, string> ();headers ["Content-Type"] = "application/x-www-form-urlencoded";//將要發送的Post常值內容string data = "username=yococo&password=123456789";//將文本轉為byte數組byte[] bs = System.Text.UTF8Encoding.UTF8.GetBytes (data);//向HTTP伺服器提交Post資料WWW www = new WWW ("http://yococoxc.vicp.cc:9999/test/userprint.php", bs, headers);//等待伺服器的響應yield return www;//如果出現錯誤if (www.error != null) {//擷取伺服器的錯誤資訊m_info = www.error;yield return null;}//擷取伺服器的響應文本m_info = www.text;}
特別提示:
用了 Dictionary 這個類,請引入 using System.Collections.Generic; 否則會出錯。
然後按鈕對應上執行的方法。
if (GUI.Button (new Rect (10, 100, 150, 30), "Post Data")) {StartCoroutine(IPostData());}
結果:
3.4棄置的方法。
public WWW (string url, byte[] postData, Hashtable headers)
被棄置了,這個編程中不罕見,有棄置就會有相對代替的方法,文檔是關鍵。
3.5另外的Post寫法,實現另外的方法。
IEnumerator IPostData() {//將要發送的Post常值內容string data = "username=yococo&password=123456789";//將文本轉為byte數組byte[] bs = System.Text.UTF8Encoding.UTF8.GetBytes (data);//向HTTP伺服器提交Post資料WWW www = new WWW ("http://yococoxc.vicp.cc:9999/test/userprint.php", bs);//等待伺服器的響應yield return www;//如果出現錯誤if (www.error != null) {//擷取伺服器的錯誤資訊m_info = www.error;yield return null;}//擷取伺服器的響應文本m_info = www.text;}此處少了添加頭資訊。
IEnumerator IPostData() {WWWForm form = new WWWForm ();//添加欄位(鍵,值)form.AddField ("username", "yococo");form.AddField ("password", "123456789");//向HTTP伺服器提交Post資料,提交表單WWW www = new WWW ("http://yococoxc.vicp.cc:9999/test/userprint.php", form);//等待伺服器的響應yield return www;//如果出現錯誤if (www.error != null) {//擷取伺服器的錯誤資訊m_info = www.error;yield return null;}//擷取伺服器的響應文本m_info = www.text;}
接下來就是PHP代碼:
<?phpif (isset($_GET['username']) && isset($_GET['password'])) {echo "GET -> username is " . $_GET['username'] . " and password is" . $_GET['password'];} else if (isset($_POST['username']) && isset($_POST['password'])) {echo "POST -> username is " . $_POST['username'] . " and password is" . $_POST['password'];} else {echo "error";}?>
作者:木子才
iOS開發人員俱樂部 232099237 有空可以加入這裡一起探討問題,由於群小,請輸入驗證資訊。