using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using System.Runtime.InteropServices; // for DllImport using AOT; using System; namespace WebGLSupport { class WebGLInputMobilePlugin { #if UNITY_WEBGL && !UNITY_EDITOR [DllImport("__Internal")] public static extern int WebGLInputMobileRegister(Action OnTouchEnd); [DllImport("__Internal")] public static extern void WebGLInputMobileOnFocusOut(int id, Action OnFocusOut); #else /// /// ID を割り振り /// /// public static int WebGLInputMobileRegister(Action OnTouchEnd) { return 0; } public static void WebGLInputMobileOnFocusOut(int id, Action OnFocusOut) { } #endif } public class WebGLInputMobile : MonoBehaviour, IPointerDownHandler { static Dictionary instances = new Dictionary(); int id = -1; private void Awake() { #if !(UNITY_WEBGL && !UNITY_EDITOR) // WebGL 以外、更新メソッドは動作しないようにします enabled = false; #endif } /// /// 押されたら、touchend イベントを登録する /// /// public void OnPointerDown(PointerEventData eventData) { if (id != -1) return; id = WebGLInputMobilePlugin.WebGLInputMobileRegister(OnTouchEnd); instances[id] = this; } [MonoPInvokeCallback(typeof(Action))] static void OnTouchEnd(int id) { var @this = instances[id]; @this.GetComponent().OnSelect(); @this.StartCoroutine(RegisterOnFocusOut(id)); } static IEnumerator RegisterOnFocusOut(int id) { yield return null; // wait one frame. WebGLInputMobilePlugin.WebGLInputMobileOnFocusOut(id, OnFocusOut); } [MonoPInvokeCallback(typeof(Action))] static void OnFocusOut(int id) { var @this = instances[id]; @this.StartCoroutine(ExecFocusOut(id)); } static IEnumerator ExecFocusOut(int id) { yield return null; // wait one frame. var @this = instances[id]; @this.GetComponent().DeactivateInputField(); // release @this.id = -1; instances.Remove(id); } } }