Keyboard.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /**
  2. * Copyright (c) 2021 Vuplex Inc. All rights reserved.
  3. *
  4. * Licensed under the Vuplex Commercial Software Library License, you may
  5. * not use this file except in compliance with the License. You may obtain
  6. * a copy of the License at
  7. *
  8. * https://vuplex.com/commercial-library-license
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. using System;
  17. using UnityEngine;
  18. namespace Vuplex.WebView
  19. {
  20. /// <summary>
  21. /// A 3D, on-screen keyboard that you can hook up to a webview for typing.
  22. /// You add a Keyboard to your scene by dragging Keyboard.prefab into it
  23. /// via the editor or by programmatically calling `Keyboard.Instantiate()`.
  24. /// For use in a `Canvas`, please see `CanvasKeyboard` instead.
  25. /// </summary>
  26. /// <remarks>
  27. /// The Keyboard's UI is a React.js app that runs inside a `WebViewPrefab` instance and
  28. /// emits messages to the C# to indicate when characters have been pressed.
  29. /// [The keyboard UI is open source and available on GitHub](https://github.com/vuplex/unity-keyboard).
  30. /// </remarks>
  31. /// <example>
  32. /// // First, create a WebViewPrefab for our main web content.
  33. /// var webViewPrefab = WebViewPrefab.Instantiate(0.6f, 0.3f);
  34. /// webViewPrefab.transform.parent = transform;
  35. /// webViewPrefab.transform.localPosition = new Vector3(0, 0f, 0.4f);
  36. /// webViewPrefab.transform.LookAt(transform);
  37. /// webViewPrefab.Initialized += (sender, e) => {
  38. /// webViewPrefab.WebView.LoadUrl("https://www.google.com");
  39. /// };
  40. /// // Add a Keyboard under the main webview.
  41. /// var keyboard = Keyboard.Instantiate();
  42. /// keyboard.transform.parent = webViewPrefab.transform;
  43. /// keyboard.transform.localPosition = new Vector3(0, -0.31f, 0);
  44. /// keyboard.transform.localEulerAngles = new Vector3(0, 0, 0);
  45. /// // Hook up the keyboard so that characters are routed to the main webview.
  46. /// keyboard.InputReceived += (sender, e) => {
  47. /// webViewPrefab.WebView.HandleKeyboardInput(e.Value);
  48. /// };
  49. /// </example>
  50. /// <remarks>
  51. /// The keyboard supports layouts for the following languages and automatically sets the layout
  52. /// based on the operating system's default language:
  53. /// - English
  54. /// - Spanish
  55. /// - French
  56. /// - German
  57. /// - Russian
  58. /// - Danish
  59. /// - Norwegian
  60. /// - Swedish
  61. /// </remarks>
  62. public class Keyboard : BaseKeyboard
  63. {
  64. /// <summary>
  65. /// Sets the keyboard's initial resolution in pixels per Unity unit.
  66. /// You can change the resolution to make the keyboard's content appear larger or smaller.
  67. /// For more information on scaling web content, see
  68. /// [this support article](https://support.vuplex.com/articles/how-to-scale-web-content).
  69. /// </summary>
  70. [Label("Initial Resolution (px / Unity unit)")]
  71. [Tooltip("You can change this to make web content appear larger or smaller.")]
  72. public float InitialResolution = 1300;
  73. /// <summary>
  74. /// The `WebViewPrefab` instance used for the keyboard UI.
  75. /// </summary>
  76. public WebViewPrefab WebViewPrefab
  77. {
  78. get
  79. {
  80. return (WebViewPrefab)_webViewPrefab;
  81. }
  82. }
  83. [Obsolete("Keyboard.Init() has been removed. The Keyboard script now initializes itself automatically, so Init() no longer needs to be called.", true)]
  84. public void Init(float width, float height) { }
  85. void _initKeyboard()
  86. {
  87. var size = transform.localScale;
  88. transform.localScale = Vector3.one;
  89. var webViewPrefab = gameObject.GetComponentInChildren<WebViewPrefab>();
  90. if (!webViewPrefab) return;
  91. _webViewPrefab = webViewPrefab;
  92. webViewPrefab.InitialResolution = InitialResolution;
  93. _webViewPrefab.transform.SetParent(transform, false);
  94. _webViewPrefab.gameObject.layer = gameObject.layer;
  95. // Shift the WebViewPrefab up by half its height so that it's in the same place
  96. // as the palceholder.
  97. _webViewPrefab.transform.localPosition = Vector3.zero;
  98. _webViewPrefab.transform.localEulerAngles = Vector3.zero;
  99. _init();
  100. // Disable the placeholder that is used in the editor.
  101. var placeholder = transform.Find("Placeholder");
  102. if (placeholder != null)
  103. {
  104. placeholder.gameObject.SetActive(false);
  105. }
  106. }
  107. void Start()
  108. {
  109. _initKeyboard();
  110. }
  111. const float DEFAULT_KEYBOARD_WIDTH = 0.5f;
  112. const float DEFAULT_KEYBOARD_HEIGHT = 0.125f;
  113. }
  114. }