Created
June 20, 2020 13:32
-
-
Save am1tanaka/d73718a1865e663fc0ce24e44a9dcf2c to your computer and use it in GitHub Desktop.
UnityのWebGL用日本語入力対応クラスのTextMeshPro版
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| using UnityEngine.UI; | |
| using UnityEngine.EventSystems; | |
| using System.Collections; | |
| using TMPro; | |
| public class WebGLNativeInputFieldTMPro : TMPro.TMP_InputField | |
| { | |
| public enum EDialogType | |
| { | |
| PromptPopup, | |
| OverlayHtml, | |
| } | |
| public string m_DialogTitle = "Input Text"; | |
| public string m_DialogOkBtn = "OK"; | |
| public string m_DialogCancelBtn = "Cancel"; | |
| public EDialogType m_DialogType = EDialogType.OverlayHtml; | |
| #if UNITY_WEBGL && !UNITY_EDITOR | |
| string limitText(string tx) | |
| { | |
| if (this.characterLimit > 0) | |
| { | |
| return tx.Substring(0, this.characterLimit); | |
| } | |
| return tx; | |
| } | |
| public override void OnSelect(BaseEventData eventData) | |
| { | |
| switch( m_DialogType ){ | |
| case EDialogType.PromptPopup: | |
| this.text = limitText(WebNativeDialog.OpenNativeStringDialog(m_DialogTitle, this.text)); | |
| StartCoroutine(this.DelayInputDeactive()); | |
| break; | |
| case EDialogType.OverlayHtml: | |
| WebNativeDialog.SetUpOverlayDialog(m_DialogTitle, this.text , m_DialogOkBtn , m_DialogCancelBtn ); | |
| StartCoroutine(this.OverlayHtmlCoroutine()); | |
| break; | |
| } | |
| } | |
| private IEnumerator DelayInputDeactive() | |
| { | |
| yield return new WaitForEndOfFrame(); | |
| this.DeactivateInputField(); | |
| EventSystem.current.SetSelectedGameObject(null); | |
| } | |
| private IEnumerator OverlayHtmlCoroutine() | |
| { | |
| yield return new WaitForEndOfFrame(); | |
| this.DeactivateInputField(); | |
| EventSystem.current.SetSelectedGameObject(null); | |
| WebGLInput.captureAllKeyboardInput = false; | |
| while (WebNativeDialog.IsOverlayDialogActive()) | |
| { | |
| yield return null; | |
| } | |
| WebGLInput.captureAllKeyboardInput = true; | |
| if (!WebNativeDialog.IsOverlayDialogCanceled()) | |
| { | |
| this.text = limitText(WebNativeDialog.GetOverlayDialogValue()); | |
| } | |
| } | |
| #endif | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment