123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- using System.Collections.Generic;
- using Newtonsoft.Json;
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.UIElements;
- using ZXing;
- using Button = UnityEngine.UIElements.Button;
- /* 第一次创建狗的时候选择创建或者是领养
- */
- public class CreateOrAdopt : MonoBehaviour
- {
- private Label messageLabel;
- private Button createBtn, adoptBtn, cancelBtn;
- GameObject canvasPlaceholder, initDogCanvas, createAdoptCanvas, cameraContent;
- public RawImage cameraTexture; // 摄像头画面显示区域
- private WebCamTexture webCamTexture; // 摄像头纹理
- private BarcodeReader barcodeReader; // 二维码识别器
- private bool isScanning = false; // 是否正在扫描
- private float interval = 0.5f; // 扫描间隔
- // Start is called once before the first execution of Update after the MonoBehaviour is created
- void Start()
- {
- var root = GetComponent<UIDocument>().rootVisualElement;
- messageLabel = root.Q<Label>("message");
- createBtn = root.Q<Button>("create");
- adoptBtn = root.Q<Button>("adopt");
- cancelBtn = root.Q<Button>("cancel");
- canvasPlaceholder = GameObject.Find("Canvas Placeholder");
- initDogCanvas = canvasPlaceholder.transform.Find("Init Dog Canvas").gameObject;
- createAdoptCanvas = canvasPlaceholder.transform.Find("Create Or Adopt Canvas").gameObject;
- createBtn.RegisterCallback<ClickEvent>(e => CreateClick(e));
- adoptBtn.RegisterCallback<ClickEvent>(e => AdoptClick(e));
- cancelBtn.RegisterCallback<ClickEvent>(e => CancelClick(e));
- string textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "InitDogUI", "button", "create", EnviromentSetting.languageCode });
- createBtn.text = textValue;
- textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "InitDogUI", "button", "adopt", EnviromentSetting.languageCode });
- adoptBtn.text = textValue;
- textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "InitDogUI", "button", "cancel", EnviromentSetting.languageCode });
- cancelBtn.text = textValue;
- textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "InitDogUI", "label", "create or adopt msg", EnviromentSetting.languageCode });
- messageLabel.text = textValue;
- cameraContent = createAdoptCanvas.transform.Find("Camera Content").gameObject;
- cameraContent.SetActive(false);
- // if (UserProperty.dogs.Count > 0)
- // {
- // SwitchToCreate();
- // }
- }
- // Update is called once per frame
- void Update()
- {
- if (isScanning)
- {
- interval -= Time.deltaTime;
- if (interval <= 0)
- {
- interval = 0.25f; // 重置间隔
- ScanQRCode();
- }
- }
- }
- void SwitchToCreate()
- {
- initDogCanvas.SetActive(true);
- createAdoptCanvas.SetActive(false);
- }
- // void SwitchToAdopt()
- // {
- // }
- void CreateClick(ClickEvent e)
- {
- SwitchToCreate();
- }
- void AdoptClick(ClickEvent e)
- {
- // todo 扫码从别人那边继承狗
- if (!isScanning)
- {
- isScanning = true;
- InitCamera();
- }
- }
- void CancelClick(ClickEvent e)
- {
- if (webCamTexture != null)
- {
- webCamTexture.Stop();
- }
- isScanning = false;
- var cameraContent = GameObject.Find("Camera Content");
- cameraContent.SetActive(false);
- messageLabel.visible = true;
- // messageLabel.style.display = DisplayStyle.Flex;
- createBtn.style.display = DisplayStyle.Flex;
- adoptBtn.style.display = DisplayStyle.Flex;
- cancelBtn.style.display = DisplayStyle.None;
- }
- private void InitCamera()
- {
- WebCamDevice[] devices = WebCamTexture.devices;
- if (devices.Length > 0)
- {
- webCamTexture = new WebCamTexture(devices[0].name, 640, 480);
- cameraTexture.texture = webCamTexture;
- webCamTexture.Play();
- barcodeReader = new BarcodeReader();
- cameraContent.SetActive(true);
- messageLabel.visible = false;
- // messageLabel.style.display = DisplayStyle.None;
- createBtn.style.display = DisplayStyle.None;
- adoptBtn.style.display = DisplayStyle.None;
- cancelBtn.style.display = DisplayStyle.Flex;
- }
- else
- {
- Debug.LogError("No camera found!");
- }
- }
- private void ScanQRCode()
- {
- if (webCamTexture != null && webCamTexture.didUpdateThisFrame)
- {
- Color32[] pixels = webCamTexture.GetPixels32();
- Result result = barcodeReader.Decode(pixels, webCamTexture.width, webCamTexture.height);
- if (result != null)
- {
- Debug.Log("扫描结果:" + result.Text); // 将扫描结果输出到控制台
- string qrCode = result.Text;
- if (qrCode.Length > 18 && qrCode.Substring(0, 17) == "ARdog://transfer/")
- {
- // 处理二维码内容
- string transferCode = qrCode.Substring(17);
- Debug.Log("转移代码:" + transferCode);
- SubmitQRCodeRequest(transferCode); // 提交二维码请求
- isScanning = false;
- webCamTexture.Stop();
- }
- }
- }
- }
- private void OnDestroy()
- {
- if (webCamTexture != null)
- {
- webCamTexture.Stop();
- }
- }
- private void SubmitQRCodeRequest(string transferCode)
- {
- string url = "/api/transfer_dog/receive/";
- Dictionary<string, string> formData = new();
- WWWForm form = new();
- form.AddField("user_id", UserProperty.userId);
- form.AddField("transfer_code", transferCode);
- StartCoroutine(WebController.PostRequest(url, form, callback: SubmitQRCodeCallback));
- }
- private void SubmitQRCodeCallback(string json)
- {
- Debug.Log("QuickStartCallback: " + json);
- var data = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
- // 处理返回的结果
- if (data != null && data["status"].ToString() == "success")
- {
- // 成功处理逻辑
- string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "InitDogUI", "message", "receive_success", EnviromentSetting.languageCode });
- msg = msg.Replace("<<dog_name>>", data["dog_name"].ToString());
- MessageBoxController.ShowMessage(msg, () => GameTool.ReloadCurrentScene());
- }
- else
- {
- // 失败处理逻辑
- string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "InitDogUI", "message", "receive_fail", EnviromentSetting.languageCode });
- MessageBoxController.ShowMessage(msg, () => GameTool.ReloadCurrentScene());
- ClickEvent e = new ClickEvent();
- CancelClick(e);
- }
- }
- }
|