No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

個人帳號罐裡.cs 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. using UnityEngine.UI;
  6. using UnityEngine.Networking;
  7. using System.Text.RegularExpressions;
  8. using System.Data.SqlClient;
  9. using TMPro;
  10. using System;
  11. using System.IO;
  12. using System.Net;
  13. using SimpleJSON;
  14. using System.ComponentModel;
  15. using System.Runtime.InteropServices;
  16. public class 個人帳號罐裡 : MonoBehaviour
  17. {
  18. [SerializeField] TextMeshProUGUI 姓名;
  19. [SerializeField] TextMeshProUGUI 帳號;
  20. [SerializeField] TMP_InputField 密碼;
  21. public CanvasScaler canvasScaler;
  22. float screenWidth;
  23. float screenHeight;
  24. [SerializeField] GameObject 彈跳面板;
  25. [SerializeField] TextMeshProUGUI 彈跳文字;
  26. JSONNode json;
  27. // Start is called before the first frame update
  28. void Start()
  29. {
  30. Screen.autorotateToPortrait = false;
  31. Screen.orientation = ScreenOrientation.Portrait;
  32. screenWidth = Screen.width;
  33. screenHeight = Screen.height;
  34. float rate = 900.0f / 1900.0f;
  35. if (canvasScaler == null)
  36. {
  37. canvasScaler = GetComponent<CanvasScaler>();
  38. }
  39. Main.Global.rate = screenWidth/screenHeight;
  40. if (Main.Global.rate>rate)
  41. {
  42. canvasScaler.matchWidthOrHeight = 1f;
  43. }
  44. else
  45. {
  46. canvasScaler.matchWidthOrHeight = 0f;
  47. }
  48. StartCoroutine(使用者資料());
  49. }
  50. IEnumerator 使用者資料()
  51. {
  52. string strcon = "使用者資料";
  53. string strUrl = string.Format(Main.Global.阿帕契路徑+"comm={0}", strcon);
  54. UnityWebRequest request = UnityWebRequest.Get(strUrl);
  55. yield return request.SendWebRequest();
  56. if (request.result == UnityWebRequest.Result.ConnectionError)
  57. {
  58. Debug.Log(request.error);
  59. yield break;
  60. }
  61. string query = string.Format(request.downloadHandler.text.ToString(), Main.Global.人員);
  62. Debug.Log(query);
  63. using (SqlConnection connection = new SqlConnection(Main.Global.路徑))
  64. {
  65. try
  66. {
  67. connection.Open();
  68. using (SqlCommand command = new SqlCommand(query, connection))
  69. {
  70. using (SqlDataReader reader = command.ExecuteReader())
  71. {
  72. if (reader.Read())
  73. {
  74. json = new JSONObject();
  75. // 讀取每一欄位的值並存入 JSON 物件中
  76. json["姓名"] = reader.GetString(0).Trim('"');
  77. json["帳號"] = reader.GetString(1).Trim('"');
  78. json["密碼"] = reader.GetString(2).Trim('"');
  79. 姓名.text = json["姓名"];
  80. 帳號.text = json["帳號"];
  81. 密碼.text = json["密碼"];
  82. }
  83. }
  84. }
  85. }
  86. catch (Exception ex)
  87. {
  88. Debug.LogError("Error connecting to the database: " + ex.Message);
  89. }
  90. finally
  91. {
  92. connection.Close();
  93. }
  94. }
  95. }
  96. public void 修改密碼()
  97. {
  98. 彈跳面板.SetActive(true);
  99. if (密碼.text.ToString() != "")
  100. {
  101. string na1 = json[2].ToString().Trim('"');
  102. string na2 = 密碼.text.ToString();
  103. if (na1 == na2)
  104. {
  105. 彈跳文字.text = "未更改密碼";
  106. }
  107. else
  108. {
  109. Main.Global.PB = 密碼.text;
  110. StartCoroutine(密碼修改());
  111. 彈跳文字.text = "更改成功";
  112. }
  113. }
  114. else
  115. {
  116. 彈跳文字.text = "未輸入密碼";
  117. }
  118. }
  119. IEnumerator 密碼修改()
  120. {
  121. string strcon = "密碼修改";
  122. string strUrl = string.Format(Main.Global.阿帕契路徑+"comm={0}", strcon);
  123. UnityWebRequest request = UnityWebRequest.Get(strUrl);
  124. yield return request.SendWebRequest();
  125. if (request.result == UnityWebRequest.Result.ConnectionError)
  126. {
  127. Debug.Log(request.error);
  128. yield break;
  129. }
  130. string query = request.downloadHandler.text.ToString();
  131. using (SqlConnection connection = new SqlConnection(Main.Global.路徑))
  132. {
  133. try
  134. {
  135. connection.Open();
  136. using (SqlCommand command = new SqlCommand(query, connection))
  137. {
  138. command.Parameters.AddWithValue("@PA", Main.Global.人員);
  139. command.Parameters.AddWithValue("@PB", 密碼.text);
  140. int rowsAffected = command.ExecuteNonQuery();
  141. Debug.Log("Rows affected: " + rowsAffected);
  142. }
  143. }
  144. catch (Exception ex)
  145. {
  146. Debug.LogError("Error connecting to the database or executing command: " + ex.Message);
  147. }
  148. finally
  149. {
  150. connection.Close();
  151. }
  152. }
  153. yield return StartCoroutine(使用者資料());
  154. }
  155. public void 彈跳面板關閉()
  156. {
  157. 彈跳面板.SetActive(false);
  158. 彈跳文字.text = "";
  159. }
  160. public void back()
  161. {
  162. change_Secen(1);
  163. }
  164. public void change_Secen(int _screenNum)
  165. {
  166. SceneManager.LoadScene(_screenNum);
  167. }
  168. public void 截圖()
  169. {
  170. string fileName = string.Format("{0}/screenshot_{1}.png", Application.persistentDataPath, System.DateTime.Now.ToString("yyyyMMdd_HHmmss"));
  171. ScreenCapture.CaptureScreenshot(fileName);
  172. }
  173. }