CreateOrAdopt.cs 7.1 KB

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