Keyboard.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright (c) 2022 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's 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, Russian,
  33. /// Danish, Norwegian, and Swedish.
  34. /// </remarks>
  35. /// <example>
  36. /// <code>
  37. /// // First, create a WebViewPrefab for our main web content.
  38. /// var webViewPrefab = WebViewPrefab.Instantiate(0.6f, 0.3f);
  39. /// webViewPrefab.transform.parent = transform;
  40. /// webViewPrefab.transform.localPosition = new Vector3(0, 0f, 0.4f);
  41. /// webViewPrefab.transform.LookAt(transform);
  42. /// webViewPrefab.Initialized += (sender, e) => {
  43. /// webViewPrefab.WebView.LoadUrl("https://www.google.com");
  44. /// };
  45. /// // Add a Keyboard under the main webview.
  46. /// var keyboard = Keyboard.Instantiate();
  47. /// keyboard.transform.SetParent(webViewPrefab.transform, false);
  48. /// keyboard.transform.localPosition = new Vector3(0, -0.31f, 0);
  49. /// keyboard.transform.localEulerAngles = Vector3.zero;
  50. /// // Hook up the keyboard so that characters are routed to the main webview.
  51. /// keyboard.InputReceived += (sender, eventArgs) => {
  52. /// webViewPrefab.WebView.SendKey(eventArgs.Value);
  53. /// };
  54. /// </code>
  55. /// </example>
  56. public class Keyboard : BaseKeyboard {
  57. /// <summary>
  58. /// Sets the keyboard's initial resolution in pixels per Unity unit.
  59. /// You can change the resolution to make the keyboard's content appear larger or smaller.
  60. /// For more information on scaling web content, see
  61. /// [this support article](https://support.vuplex.com/articles/how-to-scale-web-content).
  62. /// </summary>
  63. [Label("Resolution (px / Unity unit)")]
  64. [Tooltip("You can change this to make web content appear larger or smaller.")]
  65. [FormerlySerializedAs("InitialResolution")]
  66. public float Resolution = 1300;
  67. /// <summary>
  68. /// Gets the WebViewPrefab used for the keyboard UI, or `null` if
  69. /// the keyboard hasn't finished initializing yet.
  70. /// You can use WaitUntilInitialized() to detect when the WebViewPrefab property is ready to use.
  71. /// </summary>
  72. /// <example>
  73. /// <code>
  74. /// await keyboard.WaitUntilInitialized();
  75. /// keyboard.WebViewPrefab.Clicked += (sender, eventArgs) => {
  76. /// Debug.Log("Keyboard was clicked");
  77. /// };
  78. /// </code>
  79. /// </example>
  80. public WebViewPrefab WebViewPrefab { get { return (WebViewPrefab)_webViewPrefab; }}
  81. /// <summary>
  82. /// Creates an instance using the default width and height.
  83. /// </summary>
  84. public static Keyboard Instantiate() => Instantiate(DEFAULT_KEYBOARD_WIDTH, DEFAULT_KEYBOARD_HEIGHT);
  85. /// <summary>
  86. /// Creates an instance using the specified width and height.
  87. /// </summary>
  88. public static Keyboard Instantiate(float width, float height) {
  89. var prefabPrototype = (GameObject) Resources.Load("Keyboard");
  90. var gameObject = (GameObject) Instantiate(prefabPrototype);
  91. var keyboard = gameObject.GetComponent<Keyboard>();
  92. keyboard.transform.localScale = new Vector3(width, height, 1);
  93. return keyboard;
  94. }
  95. void _initKeyboard() {
  96. var size = transform.localScale;
  97. transform.localScale = Vector3.one;
  98. var webViewPrefab = WebViewPrefab.Instantiate(
  99. size.x,
  100. size.y,
  101. _webViewOptions
  102. );
  103. _webViewPrefab = webViewPrefab;
  104. webViewPrefab.Resolution = Resolution;
  105. // Set NativeOnScreenKeyboardEnabled = true because on iOS, disabling the keyboard
  106. // for one webview disables it for all webviews.
  107. webViewPrefab.NativeOnScreenKeyboardEnabled = true;
  108. _webViewPrefab.transform.SetParent(transform, false);
  109. _setLayerRecursively(_webViewPrefab.gameObject, gameObject.layer);
  110. // Shift the WebViewPrefab up by half its height so that it's in the same place
  111. // as the palceholder.
  112. _webViewPrefab.transform.localPosition = Vector3.zero;
  113. _webViewPrefab.transform.localEulerAngles = Vector3.zero;
  114. _init();
  115. // Disable the placeholder that is used in the editor.
  116. var placeholder = transform.Find("Placeholder");
  117. if (placeholder != null) {
  118. placeholder.gameObject.SetActive(false);
  119. }
  120. }
  121. void Start() => _initKeyboard();
  122. const float DEFAULT_KEYBOARD_WIDTH = 0.5f;
  123. const float DEFAULT_KEYBOARD_HEIGHT = 0.125f;
  124. // Added in v1.0, removed in v3.12.
  125. [Obsolete("Keyboard.Init() has been removed. The Keyboard script now initializes itself automatically, so Init() no longer needs to be called.", true)]
  126. public void Init(float width, float height) {}
  127. // Added in v3.12, deprecated in v4.0.
  128. [Obsolete("Keyboard.InitialResolution is now deprecated. Please use Keyboard.Resolution instead.")]
  129. public float InitialResolution {
  130. get { return Resolution; }
  131. set { Resolution = value; }
  132. }
  133. }
  134. }