using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; using UnityEngine.Networking; using System.Text.RegularExpressions; using System.Data.SqlClient; using TMPro; using System; using System.IO; using System.Net; using SimpleJSON; using System.ComponentModel; using System.Runtime.InteropServices; public class 個人帳號罐裡 : MonoBehaviour { [SerializeField] TextMeshProUGUI 姓名; [SerializeField] TextMeshProUGUI 帳號; [SerializeField] TMP_InputField 密碼; public CanvasScaler canvasScaler; float screenWidth; float screenHeight; [SerializeField] GameObject 彈跳面板; [SerializeField] TextMeshProUGUI 彈跳文字; JSONNode json; // Start is called before the first frame update void Start() { Screen.autorotateToPortrait = false; Screen.orientation = ScreenOrientation.Portrait; screenWidth = Screen.width; screenHeight = Screen.height; float rate = 900.0f / 1900.0f; if (canvasScaler == null) { canvasScaler = GetComponent(); } Main.Global.rate = screenWidth/screenHeight; if (Main.Global.rate>rate) { canvasScaler.matchWidthOrHeight = 1f; } else { canvasScaler.matchWidthOrHeight = 0f; } StartCoroutine(使用者資料()); } IEnumerator 使用者資料() { string strcon = "使用者資料"; string strUrl = string.Format(Main.Global.阿帕契路徑+"comm={0}", strcon); UnityWebRequest request = UnityWebRequest.Get(strUrl); yield return request.SendWebRequest(); if (request.result == UnityWebRequest.Result.ConnectionError) { Debug.Log(request.error); yield break; } string query = string.Format(request.downloadHandler.text.ToString(), Main.Global.人員); Debug.Log(query); using (SqlConnection connection = new SqlConnection(Main.Global.路徑)) { try { connection.Open(); using (SqlCommand command = new SqlCommand(query, connection)) { using (SqlDataReader reader = command.ExecuteReader()) { if (reader.Read()) { json = new JSONObject(); // 讀取每一欄位的值並存入 JSON 物件中 json["姓名"] = reader.GetString(0).Trim('"'); json["帳號"] = reader.GetString(1).Trim('"'); json["密碼"] = reader.GetString(2).Trim('"'); 姓名.text = json["姓名"]; 帳號.text = json["帳號"]; 密碼.text = json["密碼"]; } } } } catch (Exception ex) { Debug.LogError("Error connecting to the database: " + ex.Message); } finally { connection.Close(); } } } public void 修改密碼() { 彈跳面板.SetActive(true); if (密碼.text.ToString() != "") { string na1 = json[2].ToString().Trim('"'); string na2 = 密碼.text.ToString(); if (na1 == na2) { 彈跳文字.text = "未更改密碼"; } else { Main.Global.PB = 密碼.text; StartCoroutine(密碼修改()); 彈跳文字.text = "更改成功"; } } else { 彈跳文字.text = "未輸入密碼"; } } IEnumerator 密碼修改() { string strcon = "密碼修改"; string strUrl = string.Format(Main.Global.阿帕契路徑+"comm={0}", strcon); UnityWebRequest request = UnityWebRequest.Get(strUrl); yield return request.SendWebRequest(); if (request.result == UnityWebRequest.Result.ConnectionError) { Debug.Log(request.error); yield break; } string query = request.downloadHandler.text.ToString(); using (SqlConnection connection = new SqlConnection(Main.Global.路徑)) { try { connection.Open(); using (SqlCommand command = new SqlCommand(query, connection)) { command.Parameters.AddWithValue("@PA", Main.Global.人員); command.Parameters.AddWithValue("@PB", 密碼.text); int rowsAffected = command.ExecuteNonQuery(); Debug.Log("Rows affected: " + rowsAffected); } } catch (Exception ex) { Debug.LogError("Error connecting to the database or executing command: " + ex.Message); } finally { connection.Close(); } } yield return StartCoroutine(使用者資料()); } public void 彈跳面板關閉() { 彈跳面板.SetActive(false); 彈跳文字.text = ""; } public void back() { change_Secen(1); } public void change_Secen(int _screenNum) { SceneManager.LoadScene(_screenNum); } public void 截圖() { string fileName = string.Format("{0}/screenshot_{1}.png", Application.persistentDataPath, System.DateTime.Now.ToString("yyyyMMdd_HHmmss")); ScreenCapture.CaptureScreenshot(fileName); } }