其他功能大致流程 === ```sequence UI->其他功能: 點擊某些區域,呼叫提供的方法 Note right of 其他功能: 確認資料 Note right of 其他功能: 邏輯處理 Note right of 其他功能: 準備Request資料 Note right of 其他功能: RequestwebRequestManager.SendRequest 其他功能->伺服器: Post Request Note right of 伺服器: 接收資料(PHP) Note right of 伺服器: 邏輯處理 Note right of 伺服器: 資料庫操作(PHP+DB) Note right of 伺服器: 回傳資料(PHP) 伺服器->其他功能: 回傳資料 Note left of 其他功能: 解讀資料 其他功能->UI: 操作結果 ``` UI和伺服器的部分交給 #設計 以及 #外部 處理 負責其他功能的組員應該提供一些資訊給其他部門處理: #設計:方法的名稱、可以傳入的參數,以及所有可能回傳的結果 #外部:Request 的方法以及內容、想要收到的資料格式 (e.g., 伺服器判斷抽卡成功,回傳結果應該要有卡片id等等) --- 其他功能的規格書 === 請見 #其他功能會議記錄&規格書提案 --- For 內部參考 === ## HTTP Request ### 大家可以一起使用的方法 ```c# webRequestManager.SendRequest(endpoint, method, requestData, callbackResponse); ``` 1. endpoint:表示伺服器上要執行的特定操作。伺服器上有多個不同的 API 端點,每個端點對應不同的操作或資源。這個參數指定了特定端點,以便伺服器知道應該執行什麼操作。 2. method:HTTP 請求的方法,通常是 "GET" 或 "POST"。 HTTP 方法指定了客戶端希望對伺服器執行的操作類型,由本組別設計。 3. requestData:這是包含要傳送到伺服器的數據的字符串。具體的內容和格式取決於不同的功能,由本組別設計。 4. callbackResponse:這是一個回調函數,它指定了當伺服器回應完成後應該執行的客戶端程式碼。回調函數接受一個字符串參數,該參數是從伺服器接收到的回應數據。 --- 一個小小的例子 ```c# using UnityEngine; public class LoginManager : MonoBehaviour { public WebRequestManager webRequestManager; // 當用戶按下登入按鈕時呼叫此函數,並傳入帳號和密碼 public void OnLoginButtonClick(string username, string password) { // 設定登入請求的相關參數 string endpoint = "login"; string method = "POST"; string requestData = "username=" + username + "&password=" + password; // 呼叫通用的HTTP請求函數,處理伺服器回應 webRequestManager.SendRequest(endpoint, method, requestData, HandleLoginResponse); } // 處理伺服器登入回應,返回不同的字符串值表示不同的登入結果 private string HandleLoginResponse(string response) { if (response == "Success") { // 登入成功,返回 "Success" return "Success"; } else if (response == "InvalidUsername") { // 帳號錯誤,返回 "InvalidUsername" return "InvalidUsername"; } else if (response == "InvalidPassword") { // 密碼錯誤,返回 "InvalidPassword" return "InvalidPassword"; } else if (response == "AccountNotFound") { // 無此帳號,返回 "AccountNotFound" return "AccountNotFound"; } else { // 其他情況,返回 "UnknownError" return "UnknownError"; } } } ``` --- WebRequestManager.cs ```C# using UnityEngine; using UnityEngine.Networking; using System; using System.Collections; public class WebRequestManager : MonoBehaviour { private string serverURL = ""; // 伺服器基本URL // 通用的HTTP請求函數 public void SendRequest(string endpoint, string method, string requestData, Action<string> callback) { StartCoroutine(SendRequestCoroutine(endpoint, method, requestData, callback)); } private IEnumerator SendRequestCoroutine(string endpoint, string method, string requestData, Action<string> callback) { string requestURL = serverURL + "/" + endpoint; UnityWebRequest www = null; if (method == "GET") { requestURL += requestData; www = UnityWebRequest.Get(requestURL); } else if (method == "POST") { www = UnityWebRequest.Post(requestURL, requestData); } if (www == null) { Debug.LogError("無效的HTTP請求方法:" + method); yield break; } // 設置安全性證書校驗,如果使用的是自簽名證書,需要將此設置為false www.certificateHandler = new BypassCertificate(); yield return www.SendWebRequest(); if (www.result == UnityWebRequest.Result.Success) { string response = www.downloadHandler.text; callback(response); } else { Debug.LogError("請求失敗:" + www.error); } } } ```