CreateOrAdopt.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. using System.Collections.Generic;
  2. using Newtonsoft.Json;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. using UnityEngine.UI;
  6. using UnityEngine.UIElements;
  7. using ZXing;
  8. using Button = UnityEngine.UIElements.Button;
  9. /* 第一次创建狗的时候选择创建或者是领养
  10. */
  11. public class CreateOrAdopt : MonoBehaviour
  12. {
  13. private Label messageLabel;
  14. private Button createBtn, adoptBtn, cancelBtn;
  15. GameObject canvasPlaceholder, initDogCanvas, createAdoptCanvas, cameraContent;
  16. private VisualElement backHome;
  17. public RawImage cameraTexture; // 摄像头画面显示区域
  18. private WebCamTexture webCamTexture; // 摄像头纹理
  19. private BarcodeReader barcodeReader; // 二维码识别器
  20. private bool isScanning = false; // 是否正在扫描
  21. private float interval = 0.5f; // 扫描间隔
  22. // Start is called once before the first execution of Update after the MonoBehaviour is created
  23. void Start()
  24. {
  25. var root = GetComponent<UIDocument>().rootVisualElement;
  26. messageLabel = root.Q<Label>("message");
  27. createBtn = root.Q<Button>("create");
  28. adoptBtn = root.Q<Button>("adopt");
  29. cancelBtn = root.Q<Button>("cancel");
  30. backHome = root.Q<VisualElement>("backHome");
  31. canvasPlaceholder = GameObject.Find("Canvas Placeholder");
  32. initDogCanvas = canvasPlaceholder.transform.Find("Init Dog Canvas").gameObject;
  33. createAdoptCanvas = canvasPlaceholder.transform.Find("Create Or Adopt Canvas").gameObject;
  34. createBtn.RegisterCallback<ClickEvent>(e => CreateClick(e));
  35. adoptBtn.RegisterCallback<ClickEvent>(e => AdoptClick(e));
  36. cancelBtn.RegisterCallback<ClickEvent>(e => CancelClick(e));
  37. backHome.RegisterCallback<ClickEvent>(e => BackHomeClick(e));
  38. string textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "InitDogUI", "button", "create", EnviromentSetting.languageCode });
  39. createBtn.text = textValue;
  40. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "InitDogUI", "button", "adopt", EnviromentSetting.languageCode });
  41. adoptBtn.text = textValue;
  42. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "InitDogUI", "button", "cancel", EnviromentSetting.languageCode });
  43. cancelBtn.text = textValue;
  44. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "InitDogUI", "label", "create or adopt msg", EnviromentSetting.languageCode });
  45. messageLabel.text = textValue;
  46. cameraContent = createAdoptCanvas.transform.Find("Camera Content").gameObject;
  47. cameraContent.SetActive(false);
  48. if (UserProperty.dogs.Count > 0)
  49. {
  50. // backHome按键显示
  51. backHome.style.display = DisplayStyle.Flex;
  52. }
  53. else
  54. {
  55. // backHome按键隐藏
  56. backHome.style.display = DisplayStyle.None;
  57. }
  58. }
  59. // Update is called once per frame
  60. void Update()
  61. {
  62. if (isScanning)
  63. {
  64. interval -= Time.deltaTime;
  65. if (interval <= 0)
  66. {
  67. interval = 0.25f; // 重置间隔
  68. ScanQRCode();
  69. }
  70. }
  71. }
  72. void SwitchToCreate()
  73. {
  74. initDogCanvas.SetActive(true);
  75. createAdoptCanvas.SetActive(false);
  76. }
  77. // void SwitchToAdopt()
  78. // {
  79. // }
  80. void CreateClick(ClickEvent e)
  81. {
  82. SwitchToCreate();
  83. }
  84. void AdoptClick(ClickEvent e)
  85. {
  86. // todo 扫码从别人那边继承狗
  87. if (!isScanning)
  88. {
  89. isScanning = true;
  90. InitCamera();
  91. }
  92. }
  93. void CancelClick(ClickEvent e)
  94. {
  95. if (webCamTexture != null)
  96. {
  97. webCamTexture.Stop();
  98. }
  99. isScanning = false;
  100. var cameraContent = GameObject.Find("Camera Content");
  101. cameraContent.SetActive(false);
  102. messageLabel.visible = true;
  103. // messageLabel.style.display = DisplayStyle.Flex;
  104. createBtn.style.display = DisplayStyle.Flex;
  105. adoptBtn.style.display = DisplayStyle.Flex;
  106. cancelBtn.style.display = DisplayStyle.None;
  107. }
  108. void BackHomeClick(ClickEvent e)
  109. {
  110. // 返回Home场景
  111. SceneManager.LoadScene("Home");
  112. }
  113. private void InitCamera()
  114. {
  115. WebCamDevice[] devices = WebCamTexture.devices;
  116. if (devices.Length > 0)
  117. {
  118. webCamTexture = new WebCamTexture(devices[0].name, 640, 480);
  119. cameraTexture.texture = webCamTexture;
  120. webCamTexture.Play();
  121. barcodeReader = new BarcodeReader();
  122. cameraContent.SetActive(true);
  123. messageLabel.visible = false;
  124. // messageLabel.style.display = DisplayStyle.None;
  125. createBtn.style.display = DisplayStyle.None;
  126. adoptBtn.style.display = DisplayStyle.None;
  127. cancelBtn.style.display = DisplayStyle.Flex;
  128. }
  129. else
  130. {
  131. Debug.LogError("No camera found!");
  132. }
  133. }
  134. private void ScanQRCode()
  135. {
  136. if (webCamTexture != null && webCamTexture.didUpdateThisFrame)
  137. {
  138. Color32[] pixels = webCamTexture.GetPixels32();
  139. Result result = barcodeReader.Decode(pixels, webCamTexture.width, webCamTexture.height);
  140. if (result != null)
  141. {
  142. Debug.Log("扫描结果:" + result.Text); // 将扫描结果输出到控制台
  143. string qrCode = result.Text;
  144. if (qrCode.Length > 18 && qrCode.Substring(0, 17) == "ARdog://transfer/")
  145. {
  146. // 处理二维码内容
  147. string transferCode = qrCode.Substring(17);
  148. Debug.Log("转移代码:" + transferCode);
  149. SubmitQRCodeRequest(transferCode); // 提交二维码请求
  150. isScanning = false;
  151. webCamTexture.Stop();
  152. }
  153. }
  154. }
  155. }
  156. private void OnDestroy()
  157. {
  158. if (webCamTexture != null)
  159. {
  160. webCamTexture.Stop();
  161. }
  162. }
  163. private void SubmitQRCodeRequest(string transferCode)
  164. {
  165. string url = "/api/transfer_dog/receive/";
  166. Dictionary<string, string> formData = new();
  167. WWWForm form = new();
  168. form.AddField("user_id", UserProperty.userId);
  169. form.AddField("transfer_code", transferCode);
  170. StartCoroutine(WebController.PostRequest(url, form, callback: SubmitQRCodeCallback));
  171. }
  172. private void SubmitQRCodeCallback(string json)
  173. {
  174. Debug.Log("QuickStartCallback: " + json);
  175. var data = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
  176. // 处理返回的结果
  177. if (data != null && data["status"].ToString() == "success")
  178. {
  179. // 成功处理逻辑
  180. string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "InitDogUI", "message", "receive_success", EnviromentSetting.languageCode });
  181. msg = msg.Replace("<<dog_name>>", data["dog_name"].ToString());
  182. MessageBoxController.ShowMessage(msg, () => AdoptionSuccessHandler());
  183. }
  184. else
  185. {
  186. // 失败处理逻辑
  187. string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "InitDogUI", "message", "receive_fail", EnviromentSetting.languageCode });
  188. MessageBoxController.ShowMessage(msg);
  189. ClickEvent e = new ClickEvent();
  190. CancelClick(e);
  191. }
  192. }
  193. private void AdoptionSuccessHandler()
  194. {
  195. // 领养成功后的处理逻辑
  196. // 以下三行代码不知道为什么要加,但是不加会报错
  197. var canvasPlaceholder = GameObject.Find("Canvas Placeholder");
  198. var loginCanvas = canvasPlaceholder.transform.Find("Login Canvas").gameObject;
  199. loginCanvas.SetActive(true);
  200. LoginController.instance.GetUserData();
  201. }
  202. }