Keyboard.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright (c) 2024 Vuplex Inc. All rights reserved.
  2. //
  3. // Licensed under the Vuplex Commercial Software Library License, you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // https://vuplex.com/commercial-library-license
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. using System;
  15. using UnityEngine;
  16. using UnityEngine.Serialization;
  17. using Vuplex.WebView.Internal;
  18. namespace Vuplex.WebView {
  19. /// <summary>
  20. /// A 3D, on-screen keyboard prefab that you can hook up to a webview for typing.
  21. /// You can add a Keyboard to your scene either by dragging the Keyboard.prefab file into it
  22. /// via the editor or by programmatically calling Keyboard.Instantiate().
  23. /// For use in a Canvas, please see CanvasKeyboard instead.
  24. /// </summary>
  25. /// <remarks>
  26. /// The keyboard UI is a React.js app that runs inside a WebViewPrefab and
  27. /// emits messages to C# to when keys are pressed.
  28. /// [The keyboard UI is open source and available on GitHub](https://github.com/vuplex/unity-keyboard).
  29. /// </remarks>
  30. /// <remarks>
  31. /// The keyboard supports layouts for the following languages and automatically sets the layout
  32. /// based on the operating system's default language: English, Spanish, French, German, Italian, Russian,
  33. /// Danish, Norwegian, and Swedish.
  34. /// </remarks>
  35. /// <remarks>
  36. /// Please note that 3D WebView's on-screen keyboard prefabs do not support Chinese, Japanese, or Korean.
  37. /// For those languages, please see [this article about IME support](https://support.vuplex.com/articles/chinese-japanese-and-korean)
  38. /// and [this section](https://support.vuplex.com/articles/chinese-japanese-and-korean#ime-unsupported)
  39. /// that describes how to enter characters for those languages programmatically.
  40. /// </remarks>///
  41. public class Keyboard : BaseKeyboard {
  42. /// <summary>
  43. /// Sets the keyboard's initial resolution in pixels per Unity unit.
  44. /// You can change the resolution to make the keyboard's content appear larger or smaller.
  45. /// For more information on scaling web content, see
  46. /// [this support article](https://support.vuplex.com/articles/how-to-scale-web-content).
  47. /// </summary>
  48. [Label("Resolution (px / Unity unit)")]
  49. [Tooltip("You can change this to make web content appear larger or smaller.")]
  50. [FormerlySerializedAs("InitialResolution")]
  51. public float Resolution = 1300;
  52. /// <summary>
  53. /// Gets the WebViewPrefab used for the keyboard UI, or `null` if
  54. /// the keyboard hasn't finished initializing yet.
  55. /// You can use WaitUntilInitialized() to detect when the WebViewPrefab property is ready to use.
  56. /// </summary>
  57. /// <example>
  58. /// <code>
  59. /// await keyboard.WaitUntilInitialized();
  60. /// keyboard.WebViewPrefab.Clicked += (sender, eventArgs) => {
  61. /// Debug.Log("Keyboard was clicked");
  62. /// };
  63. /// </code>
  64. /// </example>
  65. public WebViewPrefab WebViewPrefab { get => (WebViewPrefab)_webViewPrefab; }
  66. /// <summary>
  67. /// Creates an instance using the default width and height.
  68. /// </summary>
  69. /// <example>
  70. /// <code>
  71. /// // Add a keyboard under a WebViewPrefab.
  72. /// var keyboard = Keyboard.Instantiate();
  73. /// keyboard.transform.SetParent(webViewPrefab.transform, false);
  74. /// keyboard.transform.localPosition = new Vector3(0, -0.31f, 0);
  75. /// keyboard.transform.localEulerAngles = Vector3.zero;
  76. /// </code>
  77. /// </example>
  78. public static Keyboard Instantiate() => Instantiate(DEFAULT_KEYBOARD_WIDTH, DEFAULT_KEYBOARD_HEIGHT);
  79. /// <summary>
  80. /// Like Instantiate(), but creates an instance using the specified width and height.
  81. /// </summary>
  82. public static Keyboard Instantiate(float width, float height) {
  83. var prefabPrototype = (GameObject)Resources.Load("Keyboard");
  84. var gameObject = (GameObject)Instantiate(prefabPrototype);
  85. var keyboard = gameObject.GetComponent<Keyboard>();
  86. keyboard.transform.localScale = new Vector3(width, height, 1);
  87. return keyboard;
  88. }
  89. void _initKeyboard() {
  90. var size = transform.localScale;
  91. transform.localScale = Vector3.one;
  92. var webViewPrefab = WebViewPrefab.Instantiate(
  93. size.x,
  94. size.y,
  95. _webViewOptions
  96. );
  97. _webViewPrefab = webViewPrefab;
  98. webViewPrefab.Resolution = Resolution;
  99. // Set NativeOnScreenKeyboardEnabled = true because on iOS, disabling the keyboard
  100. // for one webview disables it for all webviews.
  101. webViewPrefab.NativeOnScreenKeyboardEnabled = true;
  102. _webViewPrefab.transform.SetParent(transform, false);
  103. _setLayerRecursively(_webViewPrefab.gameObject, gameObject.layer);
  104. // Shift the WebViewPrefab up by half its height so that it's in the same place
  105. // as the palceholder.
  106. _webViewPrefab.transform.localPosition = Vector3.zero;
  107. _webViewPrefab.transform.localEulerAngles = Vector3.zero;
  108. _init();
  109. // Disable the placeholder that is used in the editor.
  110. var placeholder = transform.Find("Placeholder");
  111. if (placeholder != null) {
  112. placeholder.gameObject.SetActive(false);
  113. }
  114. }
  115. void Start() => _initKeyboard();
  116. const float DEFAULT_KEYBOARD_WIDTH = 0.5f;
  117. const float DEFAULT_KEYBOARD_HEIGHT = 0.125f;
  118. // Added in v1.0, removed in v3.12.
  119. [Obsolete("Keyboard.Init() has been removed. The Keyboard script now initializes itself automatically, so Init() no longer needs to be called.", true)]
  120. public void Init(float width, float height) {}
  121. // Added in v3.12, deprecated in v4.0.
  122. [Obsolete("Keyboard.InitialResolution is now deprecated. Please use Keyboard.Resolution instead.")]
  123. public float InitialResolution {
  124. get => Resolution;
  125. set => Resolution = value;
  126. }
  127. }
  128. }