NoSDKScannerManager.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5. using UnityEngine;
  6. using UnityEngine.Events;
  7. public class NoSDKScannerManager : MonoBehaviour
  8. {
  9. public static NoSDKScannerManager Instance { get; private set; }
  10. public static UnityAction<string> OnInputFinished;
  11. private const float SCANTIME = 2f;
  12. private static StringBuilder inputBuffer = new StringBuilder();
  13. private static bool allowInput = false;
  14. private static bool hasPublish = false;
  15. // ==================================================
  16. private void Awake() => Instance = this;
  17. private void Start() => Init();
  18. private void Update() => InputAction();
  19. private void OnDestroy() => UnInit();
  20. // ==================================================
  21. private void Init()
  22. {
  23. return;
  24. }
  25. private void UnInit()
  26. {
  27. return;
  28. }
  29. private static void Reset()
  30. {
  31. inputBuffer.Clear();
  32. allowInput = false;
  33. hasPublish = false;
  34. return;
  35. }
  36. // ==================================================
  37. [ContextMenu(nameof(AllowScanner), true)]
  38. public static void AllowScanner(bool value)
  39. {
  40. if (value)
  41. {
  42. allowInput = value;
  43. }
  44. else
  45. {
  46. Reset();
  47. }
  48. return;
  49. }
  50. public string GetInput()
  51. {
  52. if (inputBuffer.Length > 0)
  53. {
  54. string input = inputBuffer.ToString();
  55. Debug.Log(@$"[NoSDKSM] {input}");
  56. if (OnInputFinished != null)
  57. {
  58. OnInputFinished(input);
  59. }
  60. Reset();
  61. return input;
  62. }
  63. return null;
  64. }
  65. private void InputAction()
  66. {
  67. if (!allowInput)
  68. {
  69. return;
  70. }
  71. if (inputBuffer.Length > 0 && !hasPublish)
  72. {
  73. hasPublish = true;
  74. StartCoroutine(nameof(DelayGetInput));
  75. }
  76. // 处理常规字符输入
  77. foreach (char c in Input.inputString)
  78. {
  79. // 只接受可打印ASCII字符
  80. if (c >= ' ' && c <= '~')
  81. {
  82. inputBuffer.Append(c);
  83. // Debug.Log("当前输入: " + inputBuffer.ToString());
  84. }
  85. }
  86. // // 处理退格键
  87. // if (Input.GetKeyDown(KeyCode.Backspace) && inputBuffer.Length > 0)
  88. // {
  89. // inputBuffer.Length--;
  90. // Debug.Log("当前输入: " + inputBuffer.ToString());
  91. // }
  92. return;
  93. }
  94. private IEnumerator DelayGetInput()
  95. {
  96. yield return new WaitForSeconds(SCANTIME);
  97. GetInput();
  98. }
  99. // ==================================================
  100. #region Tool
  101. public static string AnalyzeURL(string value)
  102. {
  103. string pattern = @"(https://project\.studiocapsule\.cn/nike_adt_2025/index\.html#/\?user_code=[A-Z]\d+)";
  104. Match match = Regex.Match(value, pattern);
  105. if (match.Success)
  106. {
  107. return match.Groups[1].Value;
  108. }
  109. else
  110. {
  111. return null;
  112. }
  113. }
  114. #endregion
  115. }