WrappedUIToolkit.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using UnityEngine;
  2. using UnityEngine.UIElements;
  3. namespace WebGLSupport
  4. {
  5. /// <summary>
  6. /// Wrapper for UnityEngine.UIElements.TextField
  7. /// </summary>
  8. class WrappedUIToolkit : IInputField
  9. {
  10. TextField input;
  11. public bool ReadOnly { get { return input.isReadOnly; } }
  12. public string text
  13. {
  14. get { return input.value; }
  15. set { input.value = value; }
  16. }
  17. public string placeholder
  18. {
  19. get
  20. {
  21. #if UNITY_2023_1_OR_NEWER
  22. return input.textEdition.placeholder;
  23. #else
  24. return "";
  25. #endif
  26. }
  27. }
  28. public int fontSize
  29. {
  30. /// MEMO : how to get the fontsize?
  31. get { return 20; }
  32. }
  33. public ContentType contentType
  34. {
  35. get
  36. {
  37. if (input.isPasswordField)
  38. {
  39. return ContentType.Password;
  40. }
  41. #if UNITY_2022_1_OR_NEWER
  42. var keyboardType = input.keyboardType;
  43. #else
  44. var keyboardType = TouchScreenKeyboardType.Default;
  45. #endif
  46. return keyboardType switch
  47. {
  48. TouchScreenKeyboardType.Default => ContentType.Standard,
  49. TouchScreenKeyboardType.ASCIICapable => ContentType.Alphanumeric,
  50. TouchScreenKeyboardType.NumbersAndPunctuation => ContentType.Standard,
  51. TouchScreenKeyboardType.URL => ContentType.Standard,
  52. TouchScreenKeyboardType.NumberPad => ContentType.IntegerNumber,
  53. TouchScreenKeyboardType.PhonePad => ContentType.Standard,
  54. TouchScreenKeyboardType.NamePhonePad => ContentType.Standard,
  55. TouchScreenKeyboardType.EmailAddress => ContentType.EmailAddress,
  56. //TouchScreenKeyboardType.NintendoNetworkAccount => throw new System.NotImplementedException(),
  57. TouchScreenKeyboardType.Social => ContentType.Standard,
  58. TouchScreenKeyboardType.Search => ContentType.Standard,
  59. TouchScreenKeyboardType.DecimalPad => ContentType.DecimalNumber,
  60. TouchScreenKeyboardType.OneTimeCode => ContentType.Standard,
  61. _ => ContentType.Standard,
  62. };
  63. }
  64. }
  65. public LineType lineType
  66. {
  67. get
  68. {
  69. return input.multiline ? LineType.MultiLineNewline : LineType.SingleLine;
  70. }
  71. }
  72. public int characterLimit
  73. {
  74. get { return input.maxLength; }
  75. }
  76. public int caretPosition
  77. {
  78. get { return input.cursorIndex; }
  79. }
  80. public bool isFocused
  81. {
  82. get
  83. {
  84. return true;
  85. }
  86. }
  87. public int selectionFocusPosition
  88. {
  89. get { return input.cursorIndex; }
  90. #if UNITY_2022_1_OR_NEWER
  91. set { input.cursorIndex = value; }
  92. #else
  93. set { input.SelectRange(value, input.selectIndex); }
  94. #endif
  95. }
  96. public int selectionAnchorPosition
  97. {
  98. get { return input.selectIndex; }
  99. #if UNITY_2022_1_OR_NEWER
  100. set { input.selectIndex = value; }
  101. #else
  102. set { input.SelectRange(input.cursorIndex, value); }
  103. #endif
  104. }
  105. public bool OnFocusSelectAll
  106. {
  107. #if UNITY_2022_1_OR_NEWER
  108. get { return input.selectAllOnFocus || input.selectAllOnMouseUp; }
  109. #else
  110. get { return true; }
  111. #endif
  112. }
  113. public bool EnableMobileSupport
  114. {
  115. get
  116. {
  117. // return false to use unity mobile keyboard support
  118. return false;
  119. }
  120. }
  121. public WrappedUIToolkit(WebGLUIToolkitTextField input)
  122. {
  123. this.input = input.TextField;
  124. }
  125. public Rect GetScreenCoordinates()
  126. {
  127. var textInput = input.Q("unity-text-input");
  128. var rect = textInput.worldBound;
  129. return new Rect(rect.x, Screen.height - (rect.y + rect.height), rect.width, rect.height);
  130. }
  131. public void ActivateInputField()
  132. {
  133. }
  134. public void DeactivateInputField()
  135. {
  136. input.Blur();
  137. }
  138. public void Rebuild()
  139. {
  140. }
  141. }
  142. }