From a41f50f1383068e1eec9d70a568f0066c3557108 Mon Sep 17 00:00:00 2001 From: katboi01 Date: Thu, 5 Dec 2024 04:24:10 +0100 Subject: [PATCH] Added WebGL Mobile Input plugin Added 3D camera controls --- Assets/KeyboardWebGL.meta | 8 + Assets/Scenes/FateNew.unity | 58 ++- Assets/Scripts/CameraOrbit3D.cs | 9 +- Assets/Scripts/FateViewerMain.cs | 1 + Assets/Scripts/WebGLSupport.meta | 8 + Assets/Scripts/WebGLSupport/WebGLInput.meta | 8 + .../WebGLSupport/WebGLInput/Detail.meta | 8 + .../WebGLInput/Detail/RebuildChecker.cs | 64 +++ .../WebGLInput/Detail/RebuildChecker.cs.meta | 11 + .../WebGLSupport/WebGLInput/Mobile.meta | 8 + .../WebGLInput/Mobile/WebGLInputMobile.cs | 87 ++++ .../Mobile/WebGLInputMobile.cs.meta | 11 + .../WebGLInput/Mobile/WebGLInputMobile.jslib | 23 + .../Mobile/WebGLInputMobile.jslib.meta | 34 ++ .../WebGLSupport/WebGLInput/WebGLInput.cs | 460 ++++++++++++++++++ .../WebGLInput/WebGLInput.cs.meta | 11 + .../WebGLSupport/WebGLInput/WebGLInput.jslib | 184 +++++++ .../WebGLInput/WebGLInput.jslib.meta | 34 ++ .../WebGLSupport/WebGLInput/Wrapper.meta | 8 + .../WebGLInput/Wrapper/IInputField.cs | 45 ++ .../WebGLInput/Wrapper/IInputField.cs.meta | 11 + .../WebGLInput/Wrapper/WrappedInputField.cs | 110 +++++ .../Wrapper/WrappedInputField.cs.meta | 11 + .../Wrapper/WrappedTMPInputField.cs | 167 +++++++ .../Wrapper/WrappedTMPInputField.cs.meta | 11 + Assets/Scripts/WebGLSupport/WebGLWindow.meta | 8 + .../WebGLSupport/WebGLWindow/WebGLWindow.cs | 74 +++ .../WebGLWindow/WebGLWindow.cs.meta | 11 + .../WebGLWindow/WebGLWindow.jslib | 92 ++++ .../WebGLWindow/WebGLWindow.jslib.meta | 34 ++ Assets/discord-icon.png.meta | 15 +- ProjectSettings/ProjectSettings.asset | 2 +- 32 files changed, 1622 insertions(+), 4 deletions(-) create mode 100644 Assets/KeyboardWebGL.meta create mode 100644 Assets/Scripts/WebGLSupport.meta create mode 100644 Assets/Scripts/WebGLSupport/WebGLInput.meta create mode 100644 Assets/Scripts/WebGLSupport/WebGLInput/Detail.meta create mode 100644 Assets/Scripts/WebGLSupport/WebGLInput/Detail/RebuildChecker.cs create mode 100644 Assets/Scripts/WebGLSupport/WebGLInput/Detail/RebuildChecker.cs.meta create mode 100644 Assets/Scripts/WebGLSupport/WebGLInput/Mobile.meta create mode 100644 Assets/Scripts/WebGLSupport/WebGLInput/Mobile/WebGLInputMobile.cs create mode 100644 Assets/Scripts/WebGLSupport/WebGLInput/Mobile/WebGLInputMobile.cs.meta create mode 100644 Assets/Scripts/WebGLSupport/WebGLInput/Mobile/WebGLInputMobile.jslib create mode 100644 Assets/Scripts/WebGLSupport/WebGLInput/Mobile/WebGLInputMobile.jslib.meta create mode 100644 Assets/Scripts/WebGLSupport/WebGLInput/WebGLInput.cs create mode 100644 Assets/Scripts/WebGLSupport/WebGLInput/WebGLInput.cs.meta create mode 100644 Assets/Scripts/WebGLSupport/WebGLInput/WebGLInput.jslib create mode 100644 Assets/Scripts/WebGLSupport/WebGLInput/WebGLInput.jslib.meta create mode 100644 Assets/Scripts/WebGLSupport/WebGLInput/Wrapper.meta create mode 100644 Assets/Scripts/WebGLSupport/WebGLInput/Wrapper/IInputField.cs create mode 100644 Assets/Scripts/WebGLSupport/WebGLInput/Wrapper/IInputField.cs.meta create mode 100644 Assets/Scripts/WebGLSupport/WebGLInput/Wrapper/WrappedInputField.cs create mode 100644 Assets/Scripts/WebGLSupport/WebGLInput/Wrapper/WrappedInputField.cs.meta create mode 100644 Assets/Scripts/WebGLSupport/WebGLInput/Wrapper/WrappedTMPInputField.cs create mode 100644 Assets/Scripts/WebGLSupport/WebGLInput/Wrapper/WrappedTMPInputField.cs.meta create mode 100644 Assets/Scripts/WebGLSupport/WebGLWindow.meta create mode 100644 Assets/Scripts/WebGLSupport/WebGLWindow/WebGLWindow.cs create mode 100644 Assets/Scripts/WebGLSupport/WebGLWindow/WebGLWindow.cs.meta create mode 100644 Assets/Scripts/WebGLSupport/WebGLWindow/WebGLWindow.jslib create mode 100644 Assets/Scripts/WebGLSupport/WebGLWindow/WebGLWindow.jslib.meta diff --git a/Assets/KeyboardWebGL.meta b/Assets/KeyboardWebGL.meta new file mode 100644 index 0000000..9386170 --- /dev/null +++ b/Assets/KeyboardWebGL.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: ab2a82f202d82c54a827e95523f38850 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scenes/FateNew.unity b/Assets/Scenes/FateNew.unity index b2366b4..18edb22 100644 --- a/Assets/Scenes/FateNew.unity +++ b/Assets/Scenes/FateNew.unity @@ -38,7 +38,7 @@ RenderSettings: m_ReflectionIntensity: 1 m_CustomReflection: {fileID: 0} m_Sun: {fileID: 0} - m_IndirectSpecularColor: {r: 0.1839229, g: 0.22907549, b: 0.303225, a: 1} + m_IndirectSpecularColor: {r: 0.18386838, g: 0.22901553, b: 0.30311254, a: 1} m_UseRadianceAmbientProbe: 0 --- !u!157 &3 LightmapSettings: @@ -12505,6 +12505,7 @@ GameObject: - component: {fileID: 683251589} - component: {fileID: 683251588} - component: {fileID: 683251587} + - component: {fileID: 683251590} m_Layer: 5 m_Name: GifHeight m_TagString: Untagged @@ -12668,6 +12669,19 @@ CanvasRenderer: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 683251585} m_CullTransparentMesh: 1 +--- !u!114 &683251590 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 683251585} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: cc33a39070010f94fb1c2dd721c1286d, type: 3} + m_Name: + m_EditorClassIdentifier: + showHtmlElement: 0 --- !u!1 &697543529 GameObject: m_ObjectHideFlags: 0 @@ -19850,6 +19864,7 @@ GameObject: - component: {fileID: 1337976897} - component: {fileID: 1337976896} - component: {fileID: 1337976895} + - component: {fileID: 1337976898} m_Layer: 5 m_Name: SSHeight m_TagString: Untagged @@ -20013,6 +20028,19 @@ CanvasRenderer: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 1337976893} m_CullTransparentMesh: 1 +--- !u!114 &1337976898 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1337976893} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: cc33a39070010f94fb1c2dd721c1286d, type: 3} + m_Name: + m_EditorClassIdentifier: + showHtmlElement: 0 --- !u!1 &1340416476 GameObject: m_ObjectHideFlags: 0 @@ -20025,6 +20053,7 @@ GameObject: - component: {fileID: 1340416480} - component: {fileID: 1340416479} - component: {fileID: 1340416478} + - component: {fileID: 1340416481} m_Layer: 5 m_Name: GifWidth m_TagString: Untagged @@ -20188,6 +20217,19 @@ CanvasRenderer: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 1340416476} m_CullTransparentMesh: 1 +--- !u!114 &1340416481 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1340416476} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: cc33a39070010f94fb1c2dd721c1286d, type: 3} + m_Name: + m_EditorClassIdentifier: + showHtmlElement: 0 --- !u!1 &1345195446 GameObject: m_ObjectHideFlags: 0 @@ -25926,6 +25968,7 @@ GameObject: - component: {fileID: 1934371275} - component: {fileID: 1934371274} - component: {fileID: 1934371273} + - component: {fileID: 1934371276} m_Layer: 5 m_Name: SSWidth m_TagString: Untagged @@ -26089,6 +26132,19 @@ CanvasRenderer: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 1934371271} m_CullTransparentMesh: 1 +--- !u!114 &1934371276 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1934371271} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: cc33a39070010f94fb1c2dd721c1286d, type: 3} + m_Name: + m_EditorClassIdentifier: + showHtmlElement: 0 --- !u!1 &1936480421 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/CameraOrbit3D.cs b/Assets/Scripts/CameraOrbit3D.cs index f30953f..ef2aa35 100644 --- a/Assets/Scripts/CameraOrbit3D.cs +++ b/Assets/Scripts/CameraOrbit3D.cs @@ -56,7 +56,14 @@ public class CameraOrbit3D : MonoBehaviour Vector2 touchDelta = Vector2.zero; lastMouse = mouse; - TargetCenter = Vector3.zero; + if (eventSystem.currentSelectedGameObject == null) + { + var x = Input.GetAxis("Horizontal"); + var y = Input.GetAxis("Vertical"); + var delta = (transform.up * y + transform.right * x) * Time.deltaTime; + TargetCenter += delta; + transform.position += delta; + } cam.fieldOfView = fov; Vector3 position = transform.position; diff --git a/Assets/Scripts/FateViewerMain.cs b/Assets/Scripts/FateViewerMain.cs index 9fe7323..beeb46f 100644 --- a/Assets/Scripts/FateViewerMain.cs +++ b/Assets/Scripts/FateViewerMain.cs @@ -277,6 +277,7 @@ public class FateViewerMain : MonoBehaviour { Cam.GetComponent().enabled = false; Cam.GetComponent().orthographic = false; + Cam.GetComponent().TargetCenter = Vector3.zero; Cam.GetComponent().enabled = true; } } diff --git a/Assets/Scripts/WebGLSupport.meta b/Assets/Scripts/WebGLSupport.meta new file mode 100644 index 0000000..9613195 --- /dev/null +++ b/Assets/Scripts/WebGLSupport.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: cd9908edcf916f6488a97c001cd54c32 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/WebGLSupport/WebGLInput.meta b/Assets/Scripts/WebGLSupport/WebGLInput.meta new file mode 100644 index 0000000..f95881e --- /dev/null +++ b/Assets/Scripts/WebGLSupport/WebGLInput.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 23d83eab8f753b04fb7f1384ac6676a0 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/WebGLSupport/WebGLInput/Detail.meta b/Assets/Scripts/WebGLSupport/WebGLInput/Detail.meta new file mode 100644 index 0000000..58cc325 --- /dev/null +++ b/Assets/Scripts/WebGLSupport/WebGLInput/Detail.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: dd840bb14379149498902237a63bb794 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/WebGLSupport/WebGLInput/Detail/RebuildChecker.cs b/Assets/Scripts/WebGLSupport/WebGLInput/Detail/RebuildChecker.cs new file mode 100644 index 0000000..a61ca38 --- /dev/null +++ b/Assets/Scripts/WebGLSupport/WebGLInput/Detail/RebuildChecker.cs @@ -0,0 +1,64 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace WebGLSupport.Detail +{ + public class RebuildChecker + { + IInputField input; + + string beforeString; + int beforeCaretPosition; + int beforeSelectionFocusPosition; + int beforeSelectionAnchorPosition; + //Vector2 anchoredPosition; + + public RebuildChecker(IInputField input) + { + this.input = input; + } + + public bool NeedRebuild(bool debug = false) + { + var res = false; + + // any not same + if (beforeString != input.text) + { + if(debug) Debug.Log(string.Format("beforeString : {0} != {1}", beforeString, input.text)); + beforeString = input.text; + res = true; + } + + if (beforeCaretPosition != input.caretPosition) + { + if (debug) Debug.Log(string.Format("beforeCaretPosition : {0} != {1}", beforeCaretPosition, input.caretPosition)); + beforeCaretPosition = input.caretPosition; + res = true; + } + + if (beforeSelectionFocusPosition != input.selectionFocusPosition) + { + if (debug) Debug.Log(string.Format("beforeSelectionFocusPosition : {0} != {1}", beforeSelectionFocusPosition, input.selectionFocusPosition)); + beforeSelectionFocusPosition = input.selectionFocusPosition; + res = true; + } + + if (beforeSelectionAnchorPosition != input.selectionAnchorPosition) + { + if (debug) Debug.Log(string.Format("beforeSelectionAnchorPosition : {0} != {1}", beforeSelectionAnchorPosition, input.selectionAnchorPosition)); + beforeSelectionAnchorPosition = input.selectionAnchorPosition; + res = true; + } + + //if (anchoredPosition != input.TextComponentRectTransform().anchoredPosition) + //{ + // if (debug) Debug.Log(string.Format("anchoredPosition : {0} != {1}", anchoredPosition, input.TextComponentRectTransform().anchoredPosition)); + // anchoredPosition = input.TextComponentRectTransform().anchoredPosition; + // res = true; + //} + return res; + } + } +} diff --git a/Assets/Scripts/WebGLSupport/WebGLInput/Detail/RebuildChecker.cs.meta b/Assets/Scripts/WebGLSupport/WebGLInput/Detail/RebuildChecker.cs.meta new file mode 100644 index 0000000..3f04d6e --- /dev/null +++ b/Assets/Scripts/WebGLSupport/WebGLInput/Detail/RebuildChecker.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 3cb8b49a6bee2384b888ba951eb2bdbd +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/WebGLSupport/WebGLInput/Mobile.meta b/Assets/Scripts/WebGLSupport/WebGLInput/Mobile.meta new file mode 100644 index 0000000..e43f241 --- /dev/null +++ b/Assets/Scripts/WebGLSupport/WebGLInput/Mobile.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 8fcb4aaeec72ef54486eff0172891c0b +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/WebGLSupport/WebGLInput/Mobile/WebGLInputMobile.cs b/Assets/Scripts/WebGLSupport/WebGLInput/Mobile/WebGLInputMobile.cs new file mode 100644 index 0000000..2df8e8e --- /dev/null +++ b/Assets/Scripts/WebGLSupport/WebGLInput/Mobile/WebGLInputMobile.cs @@ -0,0 +1,87 @@ +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); + } + } +} + diff --git a/Assets/Scripts/WebGLSupport/WebGLInput/Mobile/WebGLInputMobile.cs.meta b/Assets/Scripts/WebGLSupport/WebGLInput/Mobile/WebGLInputMobile.cs.meta new file mode 100644 index 0000000..f78efeb --- /dev/null +++ b/Assets/Scripts/WebGLSupport/WebGLInput/Mobile/WebGLInputMobile.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 56393f797a0f7e94e95547f5052052a4 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/WebGLSupport/WebGLInput/Mobile/WebGLInputMobile.jslib b/Assets/Scripts/WebGLSupport/WebGLInput/Mobile/WebGLInputMobile.jslib new file mode 100644 index 0000000..c3fc619 --- /dev/null +++ b/Assets/Scripts/WebGLSupport/WebGLInput/Mobile/WebGLInputMobile.jslib @@ -0,0 +1,23 @@ +var WebGLInputMobile = { + $instances: [], + + WebGLInputMobileRegister: function (touchend) { + var id = instances.push(null) - 1; + + document.body.addEventListener("touchend", function () { + document.body.removeEventListener("touchend", arguments.callee); + Runtime.dynCall("vi", touchend, [id]); + }); + + return id; + }, + WebGLInputMobileOnFocusOut: function (id, focusout) { + document.body.addEventListener("focusout", function () { + document.body.removeEventListener("focusout", arguments.callee); + Runtime.dynCall("vi", focusout, [id]); + }); + }, +} + +autoAddDeps(WebGLInputMobile, '$instances'); +mergeInto(LibraryManager.library, WebGLInputMobile); \ No newline at end of file diff --git a/Assets/Scripts/WebGLSupport/WebGLInput/Mobile/WebGLInputMobile.jslib.meta b/Assets/Scripts/WebGLSupport/WebGLInput/Mobile/WebGLInputMobile.jslib.meta new file mode 100644 index 0000000..8d604b3 --- /dev/null +++ b/Assets/Scripts/WebGLSupport/WebGLInput/Mobile/WebGLInputMobile.jslib.meta @@ -0,0 +1,34 @@ +fileFormatVersion: 2 +guid: 4df3633103619754fb77d82a1d683868 +PluginImporter: + externalObjects: {} + serializedVersion: 2 + iconMap: {} + executionOrder: {} + isPreloaded: 0 + isOverridable: 0 + platformData: + - first: + Any: + second: + enabled: 0 + settings: {} + - first: + Editor: Editor + second: + enabled: 0 + settings: + DefaultValueInitialized: true + - first: + Facebook: WebGL + second: + enabled: 1 + settings: {} + - first: + WebGL: WebGL + second: + enabled: 1 + settings: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/WebGLSupport/WebGLInput/WebGLInput.cs b/Assets/Scripts/WebGLSupport/WebGLInput/WebGLInput.cs new file mode 100644 index 0000000..8593e43 --- /dev/null +++ b/Assets/Scripts/WebGLSupport/WebGLInput/WebGLInput.cs @@ -0,0 +1,460 @@ +#if UNITY_2018_2_OR_NEWER +#define TMP_WEBGL_SUPPORT +#endif + +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.UI; +using System; +using AOT; +using System.Runtime.InteropServices; // for DllImport +using System.Collections; +using UnityEngine.EventSystems; + +namespace WebGLSupport +{ + internal class WebGLInputPlugin + { +#if UNITY_WEBGL && !UNITY_EDITOR + [DllImport("__Internal")] + public static extern void WebGLInputInit(); + [DllImport("__Internal")] + public static extern int WebGLInputCreate(string canvasId, int x, int y, int width, int height, int fontsize, string text, string placeholder, bool isMultiLine, bool isPassword, bool isHidden, bool isMobile); + + [DllImport("__Internal")] + public static extern void WebGLInputEnterSubmit(int id, bool flag); + + [DllImport("__Internal")] + public static extern void WebGLInputTab(int id, Action cb); + + [DllImport("__Internal")] + public static extern void WebGLInputFocus(int id); + + [DllImport("__Internal")] + public static extern void WebGLInputOnFocus(int id, Action cb); + + [DllImport("__Internal")] + public static extern void WebGLInputOnBlur(int id, Action cb); + + [DllImport("__Internal")] + public static extern void WebGLInputOnValueChange(int id, Action cb); + + [DllImport("__Internal")] + public static extern void WebGLInputOnEditEnd(int id, Action cb); + + [DllImport("__Internal")] + public static extern int WebGLInputSelectionStart(int id); + + [DllImport("__Internal")] + public static extern int WebGLInputSelectionEnd(int id); + + [DllImport("__Internal")] + public static extern int WebGLInputSelectionDirection(int id); + + [DllImport("__Internal")] + public static extern void WebGLInputSetSelectionRange(int id, int start, int end); + + [DllImport("__Internal")] + public static extern void WebGLInputMaxLength(int id, int maxlength); + + [DllImport("__Internal")] + public static extern void WebGLInputText(int id, string text); + + [DllImport("__Internal")] + public static extern bool WebGLInputIsFocus(int id); + + [DllImport("__Internal")] + public static extern void WebGLInputDelete(int id); + + [DllImport("__Internal")] + public static extern void WebGLInputForceBlur(int id); + +#if WEBGLINPUT_TAB + [DllImport("__Internal")] + public static extern void WebGLInputEnableTabText(int id, bool enable); +#endif +#else + public static void WebGLInputInit() {} + public static int WebGLInputCreate(string canvasId, int x, int y, int width, int height, int fontsize, string text, string placeholder, bool isMultiLine, bool isPassword, bool isHidden, bool isMobile) { return 0; } + public static void WebGLInputEnterSubmit(int id, bool flag) { } + public static void WebGLInputTab(int id, Action cb) { } + public static void WebGLInputFocus(int id) { } + public static void WebGLInputOnFocus(int id, Action cb) { } + public static void WebGLInputOnBlur(int id, Action cb) { } + public static void WebGLInputOnValueChange(int id, Action cb) { } + public static void WebGLInputOnEditEnd(int id, Action cb) { } + public static int WebGLInputSelectionStart(int id) { return 0; } + public static int WebGLInputSelectionEnd(int id) { return 0; } + public static int WebGLInputSelectionDirection(int id) { return 0; } + public static void WebGLInputSetSelectionRange(int id, int start, int end) { } + public static void WebGLInputMaxLength(int id, int maxlength) { } + public static void WebGLInputText(int id, string text) { } + public static bool WebGLInputIsFocus(int id) { return false; } + public static void WebGLInputDelete(int id) { } + public static void WebGLInputForceBlur(int id) { } + +#if WEBGLINPUT_TAB + public static void WebGLInputEnableTabText(int id, bool enable) { } +#endif + +#endif + } + + public class WebGLInput : MonoBehaviour, IComparable + { + static Dictionary instances = new Dictionary(); + public static string CanvasId { get; set; } + +#if WEBGLINPUT_TAB + public bool enableTabText = false; +#endif + + static WebGLInput() + { +#if UNITY_2020_1_OR_NEWER + WebGLInput.CanvasId = "unity-container"; +#elif UNITY_2019_1_OR_NEWER + WebGLInput.CanvasId = "unityContainer"; +#else + WebGLInput.CanvasId = "gameContainer"; +#endif + WebGLInputPlugin.WebGLInputInit(); + } + public int Id { get { return id; } } + internal int id = -1; + public IInputField input; + bool blurBlock = false; + + [TooltipAttribute("show input element on canvas. this will make you select text by drag.")] + public bool showHtmlElement = false; + + private IInputField Setup() + { + if (GetComponent()) return new WrappedInputField(GetComponent()); +#if TMP_WEBGL_SUPPORT + if (GetComponent()) return new WrappedTMPInputField(GetComponent()); +#endif // TMP_WEBGL_SUPPORT + throw new Exception("Can not Setup WebGLInput!!"); + } + + private void Awake() + { + input = Setup(); +#if !(UNITY_WEBGL && !UNITY_EDITOR) + // WebGL 以外、更新メソッドは動作しないようにします + enabled = false; +#endif + // モバイルの入力対応 + if (Application.isMobilePlatform) + { + gameObject.AddComponent(); + } + } + + /// + /// Get the element rect of input + /// + /// + RectInt GetElemetRect() + { + var rect = GetScreenCoordinates(input.RectTransform()); + // モバイルの場合、強制表示する + if (showHtmlElement || Application.isMobilePlatform) + { + var x = (int)(rect.x); + var y = (int)(Screen.height - (rect.y + rect.height)); + return new RectInt(x, y, (int)rect.width, (int)rect.height); + } + else + { + var x = (int)(rect.x); + var y = (int)(Screen.height - (rect.y)); + return new RectInt(x, y, (int)rect.width, (int)1); + } + } + /// + /// 対象が選択されたとき + /// + /// + public void OnSelect() + { + if (id != -1) throw new Exception("OnSelect : id != -1"); + + var rect = GetElemetRect(); + bool isPassword = input.contentType == ContentType.Password; + + var fontSize = Mathf.Max(14, input.fontSize); // limit font size : 14 !! + + // モバイルの場合、強制表示する + var isHidden = !(showHtmlElement || Application.isMobilePlatform); + id = WebGLInputPlugin.WebGLInputCreate(WebGLInput.CanvasId, rect.x, rect.y, rect.width, rect.height, fontSize, input.text, input.placeholder, input.lineType != LineType.SingleLine, isPassword, isHidden, Application.isMobilePlatform); + + instances[id] = this; + WebGLInputPlugin.WebGLInputEnterSubmit(id, input.lineType != LineType.MultiLineNewline); + WebGLInputPlugin.WebGLInputOnFocus(id, OnFocus); + WebGLInputPlugin.WebGLInputOnBlur(id, OnBlur); + WebGLInputPlugin.WebGLInputOnValueChange(id, OnValueChange); + WebGLInputPlugin.WebGLInputOnEditEnd(id, OnEditEnd); + WebGLInputPlugin.WebGLInputTab(id, OnTab); + // default value : https://www.w3schools.com/tags/att_input_maxlength.asp + WebGLInputPlugin.WebGLInputMaxLength(id, (input.characterLimit > 0) ? input.characterLimit : 524288); + WebGLInputPlugin.WebGLInputFocus(id); +#if WEBGLINPUT_TAB + WebGLInputPlugin.WebGLInputEnableTabText(id, enableTabText); +#endif + if (input.OnFocusSelectAll) + { + WebGLInputPlugin.WebGLInputSetSelectionRange(id, 0, input.text.Length); + } + + WebGLWindow.OnBlurEvent += OnWindowBlur; + } + + void OnWindowBlur() + { + blurBlock = true; + } + + /// + /// 画面内の描画範囲を取得する + /// + /// + /// + Rect GetScreenCoordinates(RectTransform uiElement) + { + var worldCorners = new Vector3[4]; + uiElement.GetWorldCorners(worldCorners); + + // try to support RenderMode:WorldSpace + var canvas = uiElement.GetComponentInParent(); + var useCamera = (canvas.renderMode != RenderMode.ScreenSpaceOverlay); + if (canvas && useCamera) + { + var camera = canvas.worldCamera; + if (!camera) camera = Camera.main; + + for (var i = 0; i < worldCorners.Length; i++) + { + worldCorners[i] = camera.WorldToScreenPoint(worldCorners[i]); + } + } + + var min = new Vector3(float.MaxValue, float.MaxValue); + var max = new Vector3(float.MinValue, float.MinValue); + for (var i = 0; i < worldCorners.Length; i++) + { + min.x = Mathf.Min(min.x, worldCorners[i].x); + min.y = Mathf.Min(min.y, worldCorners[i].y); + max.x = Mathf.Max(max.x, worldCorners[i].x); + max.y = Mathf.Max(max.y, worldCorners[i].y); + } + + return new Rect(min.x, min.y, max.x - min.x, max.y - min.y); + } + + internal void DeactivateInputField() + { + if (!instances.ContainsKey(id)) return; + + WebGLInputPlugin.WebGLInputDelete(id); + input.DeactivateInputField(); + instances.Remove(id); + id = -1; // reset id to -1; + WebGLWindow.OnBlurEvent -= OnWindowBlur; + } + + [MonoPInvokeCallback(typeof(Action))] + static void OnFocus(int id) + { +#if UNITY_WEBGL && !UNITY_EDITOR + Input.ResetInputAxes(); // Inputの状態リセット + UnityEngine.WebGLInput.captureAllKeyboardInput = false; +#endif + } + + [MonoPInvokeCallback(typeof(Action))] + static void OnBlur(int id) + { +#if UNITY_WEBGL && !UNITY_EDITOR + UnityEngine.WebGLInput.captureAllKeyboardInput = true; + Input.ResetInputAxes(); // Inputの状態リセット +#endif + instances[id].StartCoroutine(Blur(id)); + } + + static IEnumerator Blur(int id) + { + yield return null; + if (!instances.ContainsKey(id)) yield break; + + var block = instances[id].blurBlock; // get blur block state + instances[id].blurBlock = false; // reset instalce block state + if (block) yield break; // if block. break it!! + instances[id].DeactivateInputField(); + } + + [MonoPInvokeCallback(typeof(Action))] + static void OnValueChange(int id, string value) + { + if (!instances.ContainsKey(id)) return; + + var instance = instances[id]; + if (!instance.input.ReadOnly) + { + instance.input.text = value; + } + + // InputField.ContentType.Name が Name の場合、先頭文字が強制的大文字になるため小文字にして比べる + if (instance.input.contentType == ContentType.Name) + { + if (string.Compare(instance.input.text, value, true) == 0) + { + value = instance.input.text; + } + } + + // InputField の ContentType による整形したテキストを HTML の input に再設定します + if (value != instance.input.text) + { + var start = WebGLInputPlugin.WebGLInputSelectionStart(id); + var end = WebGLInputPlugin.WebGLInputSelectionEnd(id); + // take the offset.when char remove from input. + var offset = instance.input.text.Length - value.Length; + + WebGLInputPlugin.WebGLInputText(id, instance.input.text); + // reset the input element selection range!! + WebGLInputPlugin.WebGLInputSetSelectionRange(id, start + offset, end + offset); + } + } + [MonoPInvokeCallback(typeof(Action))] + static void OnEditEnd(int id, string value) + { + if (!instances[id].input.ReadOnly) + { + instances[id].input.text = value; + } + } + [MonoPInvokeCallback(typeof(Action))] + static void OnTab(int id, int value) + { + WebGLInputTabFocus.OnTab(instances[id], value); + } + + void Update() + { + if (input == null || !input.isFocused) + { + CheckOutFocus(); + return; + } + + // 未登録の場合、選択する + if (!instances.ContainsKey(id)) + { + if (Application.isMobilePlatform) + { + return; + } else + { + OnSelect(); + } + } + else if (!WebGLInputPlugin.WebGLInputIsFocus(id)) + { + if (Application.isMobilePlatform) + { + //input.DeactivateInputField(); + return; + } + else + { + // focus this id + WebGLInputPlugin.WebGLInputFocus(id); + } + } + + var start = WebGLInputPlugin.WebGLInputSelectionStart(id); + var end = WebGLInputPlugin.WebGLInputSelectionEnd(id); + // 選択方向によって設定します + if (WebGLInputPlugin.WebGLInputSelectionDirection(id) == -1) + { + input.selectionFocusPosition = start; + input.selectionAnchorPosition = end; + } + else + { + input.selectionFocusPosition = end; + input.selectionAnchorPosition = start; + } + + input.Rebuild(); + } + + private void OnDestroy() + { + if (!instances.ContainsKey(id)) return; + +#if UNITY_WEBGL && !UNITY_EDITOR + UnityEngine.WebGLInput.captureAllKeyboardInput = true; + Input.ResetInputAxes(); // Inputの状態リセット +#endif + DeactivateInputField(); + } + + private void OnEnable() + { + WebGLInputTabFocus.Add(this); + } + private void OnDisable() + { + WebGLInputTabFocus.Remove(this); + } + public int CompareTo(WebGLInput other) + { + var a = GetScreenCoordinates(input.RectTransform()); + var b = GetScreenCoordinates(other.input.RectTransform()); + var res = b.y.CompareTo(a.y); + if (res == 0) res = a.x.CompareTo(b.x); + return res; + } + + public void CheckOutFocus() + { + if (!Application.isMobilePlatform) return; + if (!instances.ContainsKey(id)) return; + var current = EventSystem.current.currentSelectedGameObject; + if (current != null) return; + WebGLInputPlugin.WebGLInputForceBlur(id); // Input ではないし、キーボードを閉じる + } + + /// + /// to manage tab focus + /// base on scene position + /// + static class WebGLInputTabFocus + { + static List inputs = new List(); + + public static void Add(WebGLInput input) + { + inputs.Add(input); + inputs.Sort(); + } + + public static void Remove(WebGLInput input) + { + inputs.Remove(input); + } + + public static void OnTab(WebGLInput input, int value) + { + if (inputs.Count <= 1) return; + var index = inputs.IndexOf(input); + index += value; + if (index < 0) index = inputs.Count - 1; + else if (index >= inputs.Count) index = 0; + inputs[index].input.ActivateInputField(); + } + } + } +} diff --git a/Assets/Scripts/WebGLSupport/WebGLInput/WebGLInput.cs.meta b/Assets/Scripts/WebGLSupport/WebGLInput/WebGLInput.cs.meta new file mode 100644 index 0000000..fbcc9d6 --- /dev/null +++ b/Assets/Scripts/WebGLSupport/WebGLInput/WebGLInput.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: cc33a39070010f94fb1c2dd721c1286d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/WebGLSupport/WebGLInput/WebGLInput.jslib b/Assets/Scripts/WebGLSupport/WebGLInput/WebGLInput.jslib new file mode 100644 index 0000000..82d5951 --- /dev/null +++ b/Assets/Scripts/WebGLSupport/WebGLInput/WebGLInput.jslib @@ -0,0 +1,184 @@ +var WebGLInput = { + $instances: [], + WebGLInputInit : function() { + // Remove the `Runtime` object from "v1.37.27: 12/24/2017" + // if Runtime not defined. create and add functon!! + if(typeof Runtime === "undefined") Runtime = { dynCall : dynCall } + }, + WebGLInputCreate: function (canvasId, x, y, width, height, fontsize, text, placeholder, isMultiLine, isPassword, isHidden, isMobile) { + + var container = document.getElementById(UTF8ToString(canvasId)); + var canvas = document.getElementsByTagName('canvas')[0]; + + // if container is null and have canvas + if (!container && canvas) + { + // set the container to canvas.parentNode + container = canvas.parentNode; + } + + if(canvas) + { + var scaleX = container.offsetWidth / canvas.width; + var scaleY = container.offsetHeight / canvas.height; + + if(scaleX && scaleY) + { + x *= scaleX; + width *= scaleX; + y *= scaleY; + height *= scaleY; + } + } + + var input = document.createElement(isMultiLine?"textarea":"input"); + input.style.position = "absolute"; + + if(isMobile) { + input.style.bottom = 1 + "vh"; + input.style.left = 5 + "vw"; + input.style.width = 90 + "vw"; + input.style.height = (isMultiLine? 18 : 10) + "vh"; + input.style.fontSize = 5 + "vh"; + input.style.borderWidth = 5 + "px"; + input.style.borderColor = "#000000"; + } else { + input.style.top = y + "px"; + input.style.left = x + "px"; + input.style.width = width + "px"; + input.style.height = height + "px"; + input.style.fontSize = fontsize + "px"; + } + + input.style.outlineWidth = 1 + 'px'; + input.style.opacity = isHidden?0:1; + input.style.resize = 'none'; // for textarea + input.style.padding = '0px 1px'; + input.style.cursor = "default"; + input.style.touchAction = 'manipulation'; // for mobile + + input.spellcheck = false; + input.value = UTF8ToString(text); + input.placeholder = UTF8ToString(placeholder); + + if(isPassword){ + input.type = 'password'; + } + + if(isMobile) { + document.body.appendChild(input); + } else { + container.appendChild(input); + } + return instances.push(input) - 1; + }, + WebGLInputEnterSubmit: function(id, falg){ + var input = instances[id]; + // for enter key + input.addEventListener('keydown', function(e) { + if ((e.which && e.which === 13) || (e.keyCode && e.keyCode === 13)) { + if(falg) + { + e.preventDefault(); + input.blur(); + } + } + }); + }, + WebGLInputTab:function(id, cb) { + var input = instances[id]; + // for tab key + input.addEventListener('keydown', function (e) { + if ((e.which && e.which === 9) || (e.keyCode && e.keyCode === 9)) { + e.preventDefault(); + + // if enable tab text + if(input.enableTabText){ + var val = input.value; + var start = input.selectionStart; + var end = input.selectionEnd; + input.value = val.substr(0, start) + '\t' + val.substr(end, val.length); + input.setSelectionRange(start + 1, start + 1); + input.oninput(); // call oninput to exe ValueChange function!! + } else { + Runtime.dynCall("vii", cb, [id, e.shiftKey ? -1 : 1]); + } + } + }); + }, + WebGLInputFocus: function(id){ + var input = instances[id]; + input.focus(); + }, + WebGLInputOnFocus: function (id, cb) { + var input = instances[id]; + input.onfocus = function () { + Runtime.dynCall("vi", cb, [id]); + }; + }, + WebGLInputOnBlur: function (id, cb) { + var input = instances[id]; + input.onblur = function () { + Runtime.dynCall("vi", cb, [id]); + }; + }, + WebGLInputIsFocus: function (id) { + return instances[id] === document.activeElement; + }, + WebGLInputOnValueChange:function(id, cb){ + var input = instances[id]; + input.oninput = function () { + var intArray = intArrayFromString(input.value); + var value = (allocate.length <= 2) ? allocate(intArray, ALLOC_NORMAL):allocate(intArray, 'i8', ALLOC_NORMAL); + Runtime.dynCall("vii", cb, [id,value]); + }; + }, + WebGLInputOnEditEnd:function(id, cb){ + var input = instances[id]; + input.onchange = function () { + var intArray = intArrayFromString(input.value); + var value = (allocate.length <= 2) ? allocate(intArray, ALLOC_NORMAL):allocate(intArray, 'i8', ALLOC_NORMAL); + Runtime.dynCall("vii", cb, [id,value]); + }; + }, + WebGLInputSelectionStart:function(id){ + var input = instances[id]; + return input.selectionStart; + }, + WebGLInputSelectionEnd:function(id){ + var input = instances[id]; + return input.selectionEnd; + }, + WebGLInputSelectionDirection:function(id){ + var input = instances[id]; + return (input.selectionDirection == "backward")?-1:1; + }, + WebGLInputSetSelectionRange:function(id, start, end){ + var input = instances[id]; + input.setSelectionRange(start, end); + }, + WebGLInputMaxLength:function(id, maxlength){ + var input = instances[id]; + input.maxLength = maxlength; + }, + WebGLInputText:function(id, text){ + var input = instances[id]; + input.value = UTF8ToString(text); + }, + WebGLInputDelete:function(id){ + var input = instances[id]; + input.parentNode.removeChild(input); + instances[id] = null; + }, + WebGLInputEnableTabText:function(id, enable) { + var input = instances[id]; + input.enableTabText = enable; + }, + WebGLInputForceBlur:function(id) { + var input = instances[id]; + input.blur(); + }, +} + +autoAddDeps(WebGLInput, '$instances'); +mergeInto(LibraryManager.library, WebGLInput); diff --git a/Assets/Scripts/WebGLSupport/WebGLInput/WebGLInput.jslib.meta b/Assets/Scripts/WebGLSupport/WebGLInput/WebGLInput.jslib.meta new file mode 100644 index 0000000..942e191 --- /dev/null +++ b/Assets/Scripts/WebGLSupport/WebGLInput/WebGLInput.jslib.meta @@ -0,0 +1,34 @@ +fileFormatVersion: 2 +guid: 7df541bf7b903fb45b24fab892876393 +PluginImporter: + externalObjects: {} + serializedVersion: 2 + iconMap: {} + executionOrder: {} + isPreloaded: 0 + isOverridable: 0 + platformData: + - first: + Any: + second: + enabled: 0 + settings: {} + - first: + Editor: Editor + second: + enabled: 0 + settings: + DefaultValueInitialized: true + - first: + Facebook: WebGL + second: + enabled: 1 + settings: {} + - first: + WebGL: WebGL + second: + enabled: 1 + settings: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/WebGLSupport/WebGLInput/Wrapper.meta b/Assets/Scripts/WebGLSupport/WebGLInput/Wrapper.meta new file mode 100644 index 0000000..c4a5f78 --- /dev/null +++ b/Assets/Scripts/WebGLSupport/WebGLInput/Wrapper.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: dfaa1fbdc36ec0d45b321b02a2089b55 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/WebGLSupport/WebGLInput/Wrapper/IInputField.cs b/Assets/Scripts/WebGLSupport/WebGLInput/Wrapper/IInputField.cs new file mode 100644 index 0000000..6a2f53d --- /dev/null +++ b/Assets/Scripts/WebGLSupport/WebGLInput/Wrapper/IInputField.cs @@ -0,0 +1,45 @@ +using UnityEngine; +using UnityEngine.UI; + +namespace WebGLSupport +{ + public enum ContentType + { + Standard = 0, + Autocorrected = 1, + IntegerNumber = 2, + DecimalNumber = 3, + Alphanumeric = 4, + Name = 5, + EmailAddress = 6, + Password = 7, + Pin = 8, + Custom = 9 + } + public enum LineType + { + SingleLine = 0, + MultiLineSubmit = 1, + MultiLineNewline = 2 + } + public interface IInputField + { + ContentType contentType { get; } + LineType lineType { get; } + int fontSize { get; } + string text { get; set; } + string placeholder { get; } + int characterLimit { get; } + int caretPosition { get; } + bool isFocused { get; } + int selectionFocusPosition { get; set; } + int selectionAnchorPosition { get; set; } + bool ReadOnly { get; } + bool OnFocusSelectAll { get; } + + RectTransform RectTransform(); + void ActivateInputField(); + void DeactivateInputField(); + void Rebuild(); + } +} diff --git a/Assets/Scripts/WebGLSupport/WebGLInput/Wrapper/IInputField.cs.meta b/Assets/Scripts/WebGLSupport/WebGLInput/Wrapper/IInputField.cs.meta new file mode 100644 index 0000000..3a56bcf --- /dev/null +++ b/Assets/Scripts/WebGLSupport/WebGLInput/Wrapper/IInputField.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 8be52c4f26f76e04fbae3cb86e539bdb +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/WebGLSupport/WebGLInput/Wrapper/WrappedInputField.cs b/Assets/Scripts/WebGLSupport/WebGLInput/Wrapper/WrappedInputField.cs new file mode 100644 index 0000000..e25720c --- /dev/null +++ b/Assets/Scripts/WebGLSupport/WebGLInput/Wrapper/WrappedInputField.cs @@ -0,0 +1,110 @@ +using UnityEngine; +using UnityEngine.UI; +using WebGLSupport.Detail; + +namespace WebGLSupport +{ + /// + /// Wrapper for UnityEngine.UI.InputField + /// + class WrappedInputField : IInputField + { + InputField input; + RebuildChecker checker; + + public bool ReadOnly { get { return input.readOnly; } } + + public string text + { + get { return input.text; } + set { input.text = value; } + } + + public string placeholder + { + get + { + if (!input.placeholder) return ""; + var text = input.placeholder.GetComponent(); + return text ? text.text : ""; + } + } + + public int fontSize + { + get { return input.textComponent.fontSize; } + } + + public ContentType contentType + { + get { return (ContentType)input.contentType; } + } + + public LineType lineType + { + get { return (LineType)input.lineType; } + } + + public int characterLimit + { + get { return input.characterLimit; } + } + + public int caretPosition + { + get { return input.caretPosition; } + } + + public bool isFocused + { + get { return input.isFocused; } + } + + public int selectionFocusPosition + { + get { return input.selectionFocusPosition; } + set { input.selectionFocusPosition = value; } + } + + public int selectionAnchorPosition + { + get { return input.selectionAnchorPosition; } + set { input.selectionAnchorPosition = value; } + } + + public bool OnFocusSelectAll + { + get { return true; } + } + + public WrappedInputField(InputField input) + { + this.input = input; + checker = new RebuildChecker(this); + } + + public RectTransform RectTransform() + { + return input.GetComponent(); + } + + public void ActivateInputField() + { + input.ActivateInputField(); + } + + public void DeactivateInputField() + { + input.DeactivateInputField(); + } + + public void Rebuild() + { + if (checker.NeedRebuild()) + { + input.textComponent.SetAllDirty(); + input.Rebuild(CanvasUpdate.LatePreRender); + } + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/WebGLSupport/WebGLInput/Wrapper/WrappedInputField.cs.meta b/Assets/Scripts/WebGLSupport/WebGLInput/Wrapper/WrappedInputField.cs.meta new file mode 100644 index 0000000..23eb914 --- /dev/null +++ b/Assets/Scripts/WebGLSupport/WebGLInput/Wrapper/WrappedInputField.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0c8464791034e144ca224c37c1816e37 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/WebGLSupport/WebGLInput/Wrapper/WrappedTMPInputField.cs b/Assets/Scripts/WebGLSupport/WebGLInput/Wrapper/WrappedTMPInputField.cs new file mode 100644 index 0000000..a52f1bf --- /dev/null +++ b/Assets/Scripts/WebGLSupport/WebGLInput/Wrapper/WrappedTMPInputField.cs @@ -0,0 +1,167 @@ +#if UNITY_2018_2_OR_NEWER +#define TMP_WEBGL_SUPPORT +#endif + +#if TMP_WEBGL_SUPPORT +using UnityEngine; +using TMPro; +using WebGLSupport.Detail; +using UnityEngine.UI; + +namespace WebGLSupport +{ + /// + /// Wrapper for TMPro.TMP_InputField + /// + class WrappedTMPInputField : IInputField + { + TMP_InputField input; + RebuildChecker checker; + Coroutine delayedGraphicRebuild; + + public bool ReadOnly { get { return input.readOnly; } } + + public string text + { + get { return input.text; } + set { input.text = value; } + } + public string placeholder + { + get + { + if (!input.placeholder) return ""; + var text = input.placeholder.GetComponent(); + return text ? text.text : ""; + } + } + + public int fontSize + { + get { return (int)input.textComponent.fontSize; } + } + + public ContentType contentType + { + get { return (ContentType)input.contentType; } + } + + public LineType lineType + { + get { return (LineType)input.lineType; } + } + + public int characterLimit + { + get { return input.characterLimit; } + } + + public int caretPosition + { + get { return input.caretPosition; } + } + + public bool isFocused + { + get { return input.isFocused; } + } + + public int selectionFocusPosition + { + get { return input.selectionStringFocusPosition; } + set { input.selectionStringFocusPosition = value; } + } + + public int selectionAnchorPosition + { + get { return input.selectionStringAnchorPosition; } + set { input.selectionStringAnchorPosition = value; } + } + + public bool OnFocusSelectAll + { + get { return input.onFocusSelectAll; } + } + + public WrappedTMPInputField(TMP_InputField input) + { + this.input = input; + checker = new RebuildChecker(this); + } + + public RectTransform RectTransform() + { + // 表示範囲 + // MEMO : + // TMP では textComponent を移動させてクリッピングするため、 + // 表示範囲外になる場合があるので、自分の範囲を返す + return input.GetComponent(); + } + + public void ActivateInputField() + { + input.ActivateInputField(); + } + + public void DeactivateInputField() + { + input.DeactivateInputField(); + } + + public void Rebuild() + { +#if UNITY_2020_1_OR_NEWER + if (checker.NeedRebuild()) + { + input.textComponent.SetVerticesDirty(); + input.textComponent.SetLayoutDirty(); + input.Rebuild(CanvasUpdate.LatePreRender); + } +#else + if (input.textComponent.enabled && checker.NeedRebuild()) + { + //================================ + // fix bug for tmp + // TMPの不具合で、正しく座標を設定されてなかったため、試しに対応する + var rt = input.textComponent.GetComponent(); + var size = input.textComponent.GetPreferredValues(); + if (size.x < rt.rect.xMax) + { + // textComponent の座標を更新 + var pos = rt.anchoredPosition; + pos.x = 0; + rt.anchoredPosition = pos; + + // caret の座標更新 + var caret = input.GetComponentInChildren(); + var caretRect = caret.GetComponent(); + caretRect.anchoredPosition = rt.anchoredPosition; + } + //============================== + + // HACK : 1フレーム無効にする + // MEMO : 他にいい方法Rebuildがあれば対応する + // LayoutRebuilder.ForceRebuildLayoutImmediate(); で試してダメでした + input.textComponent.enabled = rectOverlaps(input.textComponent.rectTransform, input.textViewport); + input.textComponent.SetAllDirty(); + input.Rebuild(CanvasUpdate.LatePreRender); + //Debug.Log(input.textComponent.enabled); + } + else + { + input.textComponent.enabled = true; + } +#endif + } + + bool rectOverlaps(RectTransform rectTrans1, RectTransform rectTrans2) + { + Rect rect1 = new Rect(rectTrans1.localPosition.x, rectTrans1.localPosition.y, rectTrans1.rect.width, rectTrans1.rect.height); + Rect rect2 = new Rect(rectTrans2.localPosition.x, rectTrans2.localPosition.y, rectTrans2.rect.width, rectTrans2.rect.height); + + return rect1.Overlaps(rect2); + } + } +} + +#endif // TMP_WEBGL_SUPPORT diff --git a/Assets/Scripts/WebGLSupport/WebGLInput/Wrapper/WrappedTMPInputField.cs.meta b/Assets/Scripts/WebGLSupport/WebGLInput/Wrapper/WrappedTMPInputField.cs.meta new file mode 100644 index 0000000..f4ba3a8 --- /dev/null +++ b/Assets/Scripts/WebGLSupport/WebGLInput/Wrapper/WrappedTMPInputField.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e1629c3a135d89a45aca880fa8052f5d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/WebGLSupport/WebGLWindow.meta b/Assets/Scripts/WebGLSupport/WebGLWindow.meta new file mode 100644 index 0000000..9f247b2 --- /dev/null +++ b/Assets/Scripts/WebGLSupport/WebGLWindow.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: ef582d6b11c3602438e4a301923e45dc +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/WebGLSupport/WebGLWindow/WebGLWindow.cs b/Assets/Scripts/WebGLSupport/WebGLWindow/WebGLWindow.cs new file mode 100644 index 0000000..a3a9376 --- /dev/null +++ b/Assets/Scripts/WebGLSupport/WebGLWindow/WebGLWindow.cs @@ -0,0 +1,74 @@ +using System; +using AOT; +using System.Runtime.InteropServices; // for DllImport +using UnityEngine; + +namespace WebGLSupport +{ + static class WebGLWindowPlugin + { +#if UNITY_WEBGL && !UNITY_EDITOR + [DllImport("__Internal")] + public static extern void WebGLWindowOnFocus(Action cb); + + [DllImport("__Internal")] + public static extern void WebGLWindowOnBlur(Action cb); + + [DllImport("__Internal")] + public static extern void WebGLWindowOnResize(Action cb); + + [DllImport("__Internal")] + public static extern void WebGLWindowInjectFullscreen(); +#else + public static void WebGLWindowOnFocus(Action cb) { } + public static void WebGLWindowOnBlur(Action cb) { } + public static void WebGLWindowOnResize(Action cb) { } + public static void WebGLWindowInjectFullscreen() { } +#endif + + } + + public static class WebGLWindow + { + public static bool Focus { get; private set; } + public static event Action OnFocusEvent = () => { }; + public static event Action OnBlurEvent = () => { }; + public static event Action OnResizeEvent = () => { }; + + static string ViewportContent; + static void Init() + { + Focus = true; + WebGLWindowPlugin.WebGLWindowOnFocus(OnWindowFocus); + WebGLWindowPlugin.WebGLWindowOnBlur(OnWindowBlur); + WebGLWindowPlugin.WebGLWindowOnResize(OnWindowResize); + WebGLWindowPlugin.WebGLWindowInjectFullscreen(); + } + + [MonoPInvokeCallback(typeof(Action))] + static void OnWindowFocus() + { + Focus = true; + OnFocusEvent(); + } + + [MonoPInvokeCallback(typeof(Action))] + static void OnWindowBlur() + { + Focus = false; + OnBlurEvent(); + } + + [MonoPInvokeCallback(typeof(Action))] + static void OnWindowResize() + { + OnResizeEvent(); + } + + [RuntimeInitializeOnLoadMethod] + static void RuntimeInitializeOnLoadMethod() + { + Init(); + } + } +} diff --git a/Assets/Scripts/WebGLSupport/WebGLWindow/WebGLWindow.cs.meta b/Assets/Scripts/WebGLSupport/WebGLWindow/WebGLWindow.cs.meta new file mode 100644 index 0000000..f194838 --- /dev/null +++ b/Assets/Scripts/WebGLSupport/WebGLWindow/WebGLWindow.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 5fcbb4f34ed8e894896251ff74a4633d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/WebGLSupport/WebGLWindow/WebGLWindow.jslib b/Assets/Scripts/WebGLSupport/WebGLWindow/WebGLWindow.jslib new file mode 100644 index 0000000..0aac115 --- /dev/null +++ b/Assets/Scripts/WebGLSupport/WebGLWindow/WebGLWindow.jslib @@ -0,0 +1,92 @@ +var WebGLWindow = { + WebGLWindowOnFocus: function (cb) { + window.addEventListener('focus', function () { + Runtime.dynCall("v", cb, []); + }); + }, + WebGLWindowOnBlur: function (cb) { + window.addEventListener('blur', function () { + Runtime.dynCall("v", cb, []); + }); + }, + WebGLWindowOnResize: function(cb) { + window.addEventListener('resize', function () { + Runtime.dynCall("v", cb, []); + }); + }, + WebGLWindowInjectFullscreen : function () { + document.makeFullscreen = function (id, keepAspectRatio) { + // get fullscreen object + var getFullScreenObject = function () { + var doc = window.document; + var objFullScreen = doc.fullscreenElement || doc.mozFullScreenElement || doc.webkitFullscreenElement || doc.msFullscreenElement; + return (objFullScreen); + } + + // handle fullscreen event + var eventFullScreen = function (callback) { + document.addEventListener("fullscreenchange", callback, false); + document.addEventListener("webkitfullscreenchange", callback, false); + document.addEventListener("mozfullscreenchange", callback, false); + document.addEventListener("MSFullscreenChange", callback, false); + } + + var removeEventFullScreen = function (callback) { + document.removeEventListener("fullscreenchange", callback, false); + document.removeEventListener("webkitfullscreenchange", callback, false); + document.removeEventListener("mozfullscreenchange", callback, false); + document.removeEventListener("MSFullscreenChange", callback, false); + } + + var div = document.createElement("div"); + document.body.appendChild(div); + + var canvas = document.getElementById(id); + var beforeParent = canvas.parentNode; + var beforeStyle = window.getComputedStyle(canvas); + var beforeWidth = parseInt(beforeStyle.width); + var beforeHeight = parseInt(beforeStyle.height); + + // to keep element index after fullscreen + var index = Array.from(beforeParent.children).findIndex(function (v) { return v == canvas; }); + div.appendChild(canvas); + + // recv fullscreen function + var fullscreenFunc = function () { + if (getFullScreenObject()) { + if (keepAspectRatio) { + var ratio = Math.min(window.screen.width / beforeWidth, window.screen.height / beforeHeight); + var width = Math.floor(beforeWidth * ratio); + var height = Math.floor(beforeHeight * ratio); + + canvas.style.width = width + 'px'; + canvas.style.height = height + 'px';; + } else { + canvas.style.width = window.screen.width + 'px';; + canvas.style.height = window.screen.height + 'px';; + } + + } else { + canvas.style.width = beforeWidth + 'px';; + canvas.style.height = beforeHeight + 'px';; + beforeParent.insertBefore(canvas, Array.from(beforeParent.children)[index]); + + div.parentNode.removeChild(div); + + // remove this function + removeEventFullScreen(fullscreenFunc); + } + } + + // listener fullscreen event + eventFullScreen(fullscreenFunc); + + if (div.mozRequestFullScreen) div.mozRequestFullScreen(); + else if (div.webkitRequestFullScreen) div.webkitRequestFullScreen(); + else if (div.msRequestFullscreen) div.msRequestFullscreen(); + else if (div.requestFullscreen) div.requestFullscreen(); + } + }, +} + +mergeInto(LibraryManager.library, WebGLWindow); \ No newline at end of file diff --git a/Assets/Scripts/WebGLSupport/WebGLWindow/WebGLWindow.jslib.meta b/Assets/Scripts/WebGLSupport/WebGLWindow/WebGLWindow.jslib.meta new file mode 100644 index 0000000..3992b2a --- /dev/null +++ b/Assets/Scripts/WebGLSupport/WebGLWindow/WebGLWindow.jslib.meta @@ -0,0 +1,34 @@ +fileFormatVersion: 2 +guid: 5edef37b75c044e41a013a62fec2e1ff +PluginImporter: + externalObjects: {} + serializedVersion: 2 + iconMap: {} + executionOrder: {} + isPreloaded: 0 + isOverridable: 0 + platformData: + - first: + Any: + second: + enabled: 0 + settings: {} + - first: + Editor: Editor + second: + enabled: 0 + settings: + DefaultValueInitialized: true + - first: + Facebook: WebGL + second: + enabled: 1 + settings: {} + - first: + WebGL: WebGL + second: + enabled: 1 + settings: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/discord-icon.png.meta b/Assets/discord-icon.png.meta index 44acbfd..b70ecb6 100644 --- a/Assets/discord-icon.png.meta +++ b/Assets/discord-icon.png.meta @@ -3,7 +3,7 @@ guid: 606c8ef44552e594c8ba1b9ae8a7e46e TextureImporter: internalIDToNameTable: [] externalObjects: {} - serializedVersion: 11 + serializedVersion: 12 mipmaps: mipMapMode: 0 enableMipMap: 0 @@ -62,6 +62,7 @@ TextureImporter: textureFormatSet: 0 ignorePngGamma: 0 applyGammaDecoding: 0 + cookieLightType: 1 platformSettings: - serializedVersion: 3 buildTarget: DefaultTexturePlatform @@ -99,6 +100,18 @@ TextureImporter: overridden: 0 androidETC2FallbackOverride: 0 forceMaximumCompressionQuality_BC6H_BC7: 0 + - serializedVersion: 3 + buildTarget: Android + maxTextureSize: 256 + resizeAlgorithm: 0 + textureFormat: -1 + textureCompression: 1 + compressionQuality: 50 + crunchedCompression: 0 + allowsAlphaSplitting: 0 + overridden: 0 + androidETC2FallbackOverride: 0 + forceMaximumCompressionQuality_BC6H_BC7: 0 spriteSheet: serializedVersion: 2 sprites: [] diff --git a/ProjectSettings/ProjectSettings.asset b/ProjectSettings/ProjectSettings.asset index 5469ad2..8ebfbd7 100644 --- a/ProjectSettings/ProjectSettings.asset +++ b/ProjectSettings/ProjectSettings.asset @@ -155,7 +155,7 @@ PlayerSettings: androidMaxAspectRatio: 2.1 applicationIdentifier: Android: com.DefaultCompany.FateAnimationViewer - Standalone: com.Katboi01.FateAnimationViewer + Standalone: com.Katworks.Fate-Animation-Viewer buildNumber: Standalone: 0 iPhone: 0