CreateOrAdopt.cs 6.0 KB

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