From 83163ba20be723c80daa9df824d88de581301ad9 Mon Sep 17 00:00:00 2001 From: Sammey Date: Thu, 2 Dec 2021 14:39:01 +0100 Subject: [PATCH 1/2] removed static javascriptmethod caller class, and moved the script references to the scripts that depend on them --- .../Netherlands3D/Plugins/netDxf/DxfFile.cs | 5 +- .../Scripts/Interface/OpenURL.cs | 12 +- .../Scripts/Interface/Sharing/ShareDialog.cs | 8 +- .../ChangePointerStyleHandler.cs | 8 +- .../DrawHTMLOverCanvas.cs | 31 ++--- .../JavascriptMethodCaller.cs | 106 ------------------ .../JavascriptMethodCaller.cs.meta | 11 -- .../Scripts/ModelExporting/ColladaFile.cs | 6 +- .../Scripts/Settings/ApplicationSettings.cs | 10 +- .../Scripts/Settings/CanvasSettings.cs | 13 ++- 10 files changed, 71 insertions(+), 139 deletions(-) delete mode 100644 3DAmsterdam/Assets/Netherlands3D/Scripts/JavascriptInteraction/JavascriptMethodCaller.cs delete mode 100644 3DAmsterdam/Assets/Netherlands3D/Scripts/JavascriptInteraction/JavascriptMethodCaller.cs.meta diff --git a/3DAmsterdam/Assets/Netherlands3D/Plugins/netDxf/DxfFile.cs b/3DAmsterdam/Assets/Netherlands3D/Plugins/netDxf/DxfFile.cs index 2c405a623..cdc8baa33 100644 --- a/3DAmsterdam/Assets/Netherlands3D/Plugins/netDxf/DxfFile.cs +++ b/3DAmsterdam/Assets/Netherlands3D/Plugins/netDxf/DxfFile.cs @@ -15,6 +15,9 @@ public class DxfFile private DxfDocument dxfDocument; private Layer dxfLayer; + [DllImport("__Internal")] + private static extern void DownloadFile(byte[] array, int byteLength, string fileName); + public void SetupDXF() { dxfDocument = new DxfDocument(); @@ -46,7 +49,7 @@ public void Save() { if (dxfDocument.Save(stream)) { - JavascriptMethodCaller.DownloadByteArrayAsFile(stream.ToArray(), stream.ToArray().Length, "testfile.dxf"); + DownloadFile(stream.ToArray(), stream.ToArray().Length, "testfile.dxf"); } else { diff --git a/3DAmsterdam/Assets/Netherlands3D/Scripts/Interface/OpenURL.cs b/3DAmsterdam/Assets/Netherlands3D/Scripts/Interface/OpenURL.cs index 452059aa5..defe204a8 100644 --- a/3DAmsterdam/Assets/Netherlands3D/Scripts/Interface/OpenURL.cs +++ b/3DAmsterdam/Assets/Netherlands3D/Scripts/Interface/OpenURL.cs @@ -4,11 +4,15 @@ using Netherlands3D.JavascriptConnection; using UnityEngine.EventSystems; using UnityEngine.UI; +using System.Runtime.InteropServices; namespace Netherlands3D.Interface { public class OpenURL : ChangePointerStyleHandler, IPointerDownHandler { + [DllImport("__Internal")] + private static extern string OpenURLInNewWindow(string openUrl = "https://"); + public void OnPointerDown(PointerEventData eventData) { /* @@ -24,7 +28,13 @@ private void OpenURLByGameObjectName() { var httpPosition = gameObject.name.IndexOf("http"); var url = gameObject.name.Substring(httpPosition, gameObject.name.Length-httpPosition); - JavascriptMethodCaller.OpenURL(url); + +#if !UNITY_EDITOR && UNITY_WEBGL + OpenURLInNewWindow(url); + +#else + Application.OpenURL(url); +#endif //Make sure to release everything manually (release event is blocked by our new browser window) EventSystem.current.SetSelectedGameObject(null); diff --git a/3DAmsterdam/Assets/Netherlands3D/Scripts/Interface/Sharing/ShareDialog.cs b/3DAmsterdam/Assets/Netherlands3D/Scripts/Interface/Sharing/ShareDialog.cs index b0a955619..6efdb04e6 100644 --- a/3DAmsterdam/Assets/Netherlands3D/Scripts/Interface/Sharing/ShareDialog.cs +++ b/3DAmsterdam/Assets/Netherlands3D/Scripts/Interface/Sharing/ShareDialog.cs @@ -32,6 +32,9 @@ public enum SharingState [DllImport("__Internal")] private static extern void SyncFilesToIndexedDB(); + [DllImport("__Internal")] + private static extern string SetUniqueShareURL(string token); + [SerializeField] private RectTransform shareOptions; @@ -197,8 +200,9 @@ private IEnumerator CompleteSharing() sharedURL.ShowURL(sharedSceneURL); - JavascriptMethodCaller.SetUniqueShareURLToken(currentSceneServerReturn.sceneId); - + #if !UNITY_EDITOR && UNITY_WEBGL + SetUniqueShareURL(currentSceneServerReturn.sceneId); + #endif yield return null; } diff --git a/3DAmsterdam/Assets/Netherlands3D/Scripts/JavascriptInteraction/ChangePointerStyleHandler.cs b/3DAmsterdam/Assets/Netherlands3D/Scripts/JavascriptInteraction/ChangePointerStyleHandler.cs index 6112aadd7..7f69248c2 100644 --- a/3DAmsterdam/Assets/Netherlands3D/Scripts/JavascriptInteraction/ChangePointerStyleHandler.cs +++ b/3DAmsterdam/Assets/Netherlands3D/Scripts/JavascriptInteraction/ChangePointerStyleHandler.cs @@ -1,11 +1,15 @@ using System.Collections; using System.Collections.Generic; +using System.Runtime.InteropServices; using UnityEngine; using UnityEngine.EventSystems; namespace Netherlands3D.JavascriptConnection { public class ChangePointerStyleHandler : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler { + [DllImport("__Internal")] + private static extern string SetCSSCursor(string cursorName = "pointer"); + public enum Style{ AUTO, POINTER, @@ -53,7 +57,9 @@ public static void ChangeCursor(Style type) cursorString = "progress"; break; } - JavascriptMethodCaller.ChangeCursor(cursorString); + #if !UNITY_EDITOR && UNITY_WEBGL + SetCSSCursor(cursorString); + #endif } public void OnPointerEnter(PointerEventData eventData) diff --git a/3DAmsterdam/Assets/Netherlands3D/Scripts/JavascriptInteraction/DrawHTMLOverCanvas.cs b/3DAmsterdam/Assets/Netherlands3D/Scripts/JavascriptInteraction/DrawHTMLOverCanvas.cs index fa6b12bd4..f22aa4b62 100644 --- a/3DAmsterdam/Assets/Netherlands3D/Scripts/JavascriptInteraction/DrawHTMLOverCanvas.cs +++ b/3DAmsterdam/Assets/Netherlands3D/Scripts/JavascriptInteraction/DrawHTMLOverCanvas.cs @@ -1,5 +1,6 @@ using System.Collections; using System.Collections.Generic; +using System.Runtime.InteropServices; using System.Security.AccessControl; using UnityEngine; using UnityEngine.UI; @@ -9,6 +10,10 @@ namespace Netherlands3D.JavascriptConnection { public class DrawHTMLOverCanvas : MonoBehaviour { + [DllImport("__Internal")] + private static extern void DisplayDOMObjectWithID(string id = "htmlID", string display = "none", float x = 0, float y = 0, float width = 0, float height = 0, float offsetX = 0, float offsetY = 0); + + [SerializeField] private string htmlObjectID = ""; @@ -24,11 +29,22 @@ private void Awake() image = GetComponent(); } + +#if !UNITY_EDITOR && UNITY_WEBGL private void Update() { if (alignEveryUpdate) AlignHTMLOverlay(); } + private void OnEnable() + { + AlignHTMLOverlay(); + } + private void OnDisable() + { + DisplayDOMObjectWithID(htmlObjectID,"none"); + } +#endif /// /// Tell JavaScript to make a DOM object with htmlObjectID to align with the Image component @@ -36,7 +52,8 @@ private void Update() private void AlignHTMLOverlay() { var screenSpaceRectangle = GetScreenSpaceRectangle(); - JavascriptMethodCaller.DisplayWithID(htmlObjectID, true, + + DisplayDOMObjectWithID(htmlObjectID, "inline", screenSpaceRectangle.x / Screen.width, screenSpaceRectangle.y / Screen.height, screenSpaceRectangle.width / Screen.width, @@ -56,17 +73,5 @@ private Rect GetScreenSpaceRectangle() screenSpaceRectangle.y -= ((1.0f - image.rectTransform.pivot.y) * size.y); return screenSpaceRectangle; } - -#if UNITY_WEBGL && !UNITY_EDITOR - private void OnEnable() - { - AlignHTMLOverlay(); - } - - private void OnDisable() - { - JavascriptMethodCaller.DisplayWithID(htmlObjectID,false); - } -#endif } } \ No newline at end of file diff --git a/3DAmsterdam/Assets/Netherlands3D/Scripts/JavascriptInteraction/JavascriptMethodCaller.cs b/3DAmsterdam/Assets/Netherlands3D/Scripts/JavascriptInteraction/JavascriptMethodCaller.cs deleted file mode 100644 index 8a561093a..000000000 --- a/3DAmsterdam/Assets/Netherlands3D/Scripts/JavascriptInteraction/JavascriptMethodCaller.cs +++ /dev/null @@ -1,106 +0,0 @@ -using UnityEngine; -using System.Runtime.InteropServices; - -namespace Netherlands3D.JavascriptConnection -{ - public class JavascriptMethodCaller - { - [DllImport("__Internal")] - private static extern void DisplayDOMObjectWithID(string id = "htmlID", string display = "none", float x = 0, float y = 0, float width = 0, float height = 0, float offsetX = 0, float offsetY = 0); - - [DllImport("__Internal")] - private static extern string SetUniqueShareURL(string token); - - [DllImport("__Internal")] - private static extern string SetCSSCursor(string cursorName = "pointer"); - - [DllImport("__Internal")] - private static extern string ShowAlertMessage(string alertMessage = ""); - - [DllImport("__Internal")] - private static extern string OpenURLInNewWindow(string openUrl = "https://"); - - [DllImport("__Internal")] - private static extern bool IsMobile(); - - /// - /// Some interface items are drawn as HTML DOM elements on top of the Unity3D canvas. - /// This methods scales those elements with the Unity canvas. - /// - /// The new multiplier value for the UI scale - /// - [DllImport("__Internal")] - private static extern string ChangeInterfaceScale(float scale); - - [DllImport("__Internal")] - private static extern void DownloadFile(byte[] array, int byteLength, string fileName); - - - [DllImport("__Internal")] - private static extern void LockCursorInternal(); - - public static void DownloadByteArrayAsFile(byte[] array, int byteLength, string fileName) - { - DownloadFile(array, byteLength, fileName); - } - - /// - /// This methods activates the html hitarea for the file upload button. - /// The user will click the hidden file input dialog in the index.html template file that is drawn on top of our WebGL canvas. - /// This overcomes the problem with browser security where a click is required to open a file upload dialog. - /// Faking a click through JavaScript is not allowed. - /// - /// Sets the hitarea CSS of the input HTML node to inline, or none - public static void DisplayWithID(string id, bool display, float x = 0, float y = 0, float width = 0, float height = 0, float offsetX = 0, float offsetY = 0) - { -#if UNITY_WEBGL && !UNITY_EDITOR - DisplayDOMObjectWithID(id,(display) ? "inline" : "none", x, y, width, height,offsetX,offsetY); -#endif - } - - public static void SetUniqueShareURLToken(string token) - { -#if UNITY_WEBGL && !UNITY_EDITOR - SetUniqueShareURL(token); -#endif - } - - public static bool IsMobileBrowser() - { -#if UNITY_WEBGL && !UNITY_EDITOR - return IsMobile(); -#endif - return false; - } - - public static void SetInterfaceScale(float scale) - { -#if UNITY_WEBGL && !UNITY_EDITOR - ChangeInterfaceScale(scale); -#endif - } - - public static void ChangeCursor(string cursorName) - { -#if UNITY_WEBGL && !UNITY_EDITOR - SetCSSCursor(cursorName); -#endif - } - - public static void Alert(string alertMessage) - { -#if UNITY_WEBGL && !UNITY_EDITOR - ShowAlertMessage(alertMessage); -#endif - } - - public static void OpenURL(string url) - { -#if UNITY_EDITOR - Application.OpenURL(url); -#elif UNITY_WEBGL && !UNITY_EDITOR - OpenURLInNewWindow(url); -#endif - } - } -} \ No newline at end of file diff --git a/3DAmsterdam/Assets/Netherlands3D/Scripts/JavascriptInteraction/JavascriptMethodCaller.cs.meta b/3DAmsterdam/Assets/Netherlands3D/Scripts/JavascriptInteraction/JavascriptMethodCaller.cs.meta deleted file mode 100644 index b3883d563..000000000 --- a/3DAmsterdam/Assets/Netherlands3D/Scripts/JavascriptInteraction/JavascriptMethodCaller.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: bfa5682a9a4e1874b9bdc5fb0efe012d -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/3DAmsterdam/Assets/Netherlands3D/Scripts/ModelExporting/ColladaFile.cs b/3DAmsterdam/Assets/Netherlands3D/Scripts/ModelExporting/ColladaFile.cs index 09d876bf9..089863e5c 100644 --- a/3DAmsterdam/Assets/Netherlands3D/Scripts/ModelExporting/ColladaFile.cs +++ b/3DAmsterdam/Assets/Netherlands3D/Scripts/ModelExporting/ColladaFile.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Globalization; using System.IO; +using System.Runtime.InteropServices; using System.Text; using System.Xml; using UnityEngine; @@ -17,6 +18,9 @@ /// public class ColladaFile { + [DllImport("__Internal")] + private static extern void DownloadFile(byte[] array, int byteLength, string fileName); + private XmlTextWriter writer; private StringWriter stringWriter; private List materials; @@ -402,7 +406,7 @@ public void Save(string filename = "") byte[] byteArray = Encoding.UTF8.GetBytes(stringWriter.ToString()); stringWriter = null; writer = null; - JavascriptMethodCaller.DownloadByteArrayAsFile(byteArray, byteArray.Length, (filename!="") ? filename : "ColladaExport.dae"); + DownloadFile(byteArray, byteArray.Length, (filename!="") ? filename : "ColladaExport.dae"); } else { diff --git a/3DAmsterdam/Assets/Netherlands3D/Scripts/Settings/ApplicationSettings.cs b/3DAmsterdam/Assets/Netherlands3D/Scripts/Settings/ApplicationSettings.cs index 79861bcbd..8137d5bbc 100644 --- a/3DAmsterdam/Assets/Netherlands3D/Scripts/Settings/ApplicationSettings.cs +++ b/3DAmsterdam/Assets/Netherlands3D/Scripts/Settings/ApplicationSettings.cs @@ -8,12 +8,16 @@ using Netherlands3D.LayerSystem; using System.Collections; using System.Collections.Generic; +using System.Runtime.InteropServices; using UnityEngine; using UnityEngine.UI; namespace Netherlands3D.Settings { public class ApplicationSettings : MonoBehaviour { + [DllImport("__Internal")] + private static extern bool IsMobile(); + [SerializeField] private bool forceMobileDevice = false; private bool isMobileDevice = false; @@ -58,7 +62,11 @@ public class ApplicationSettings : MonoBehaviour private void Awake() { Instance = this; - IsMobileDevice = (forceMobileDevice || JavascriptMethodCaller.IsMobileBrowser()); + IsMobileDevice = forceMobileDevice; + +#if !UNITY_EDITOR && UNITY_WEBGL + IsMobileDevice = IsMobile(); +#endif } void Start() diff --git a/3DAmsterdam/Assets/Netherlands3D/Scripts/Settings/CanvasSettings.cs b/3DAmsterdam/Assets/Netherlands3D/Scripts/Settings/CanvasSettings.cs index 36cc76b1c..588597c54 100644 --- a/3DAmsterdam/Assets/Netherlands3D/Scripts/Settings/CanvasSettings.cs +++ b/3DAmsterdam/Assets/Netherlands3D/Scripts/Settings/CanvasSettings.cs @@ -5,11 +5,16 @@ using UnityEngine; using UnityEngine.UI; using Netherlands3D.Cameras; +using System.Runtime.InteropServices; namespace Netherlands3D.Interface { public class CanvasSettings : MonoBehaviour { + [DllImport("__Internal")] + private static extern string ChangeInterfaceScale(float scale); + + [SerializeField] private CanvasScaler canvasScaler; private string canvasScaleFactorKey = "canvasScaleFactor"; @@ -35,7 +40,9 @@ public float DetectPreferedCanvasScale() { canvasScale = Mathf.Clamp(Screen.width / referenceWidth, 1.0f, maxAutoWidth); canvasScaler.scaleFactor = canvasScale; - JavascriptMethodCaller.SetInterfaceScale(canvasScale); + #if !UNITY_EDITOR && UNITY_WEBGL + ChangeInterfaceScale(canvasScale); + #endif return canvasScale; } @@ -48,7 +55,9 @@ public void ChangeCanvasScale(float scaleFactor) canvasScale = scaleFactor; canvasScaler.scaleFactor = canvasScale; PlayerPrefs.SetFloat(canvasScaleFactorKey, canvasScale); - JavascriptMethodCaller.SetInterfaceScale(canvasScale); + #if !UNITY_EDITOR && UNITY_WEBGL + ChangeInterfaceScale(canvasScale); + #endif } } } \ No newline at end of file From 11a45b3edfac9842b43fa41d0243410ed3d469d7 Mon Sep 17 00:00:00 2001 From: Sammey Date: Thu, 9 Dec 2021 10:04:00 +0100 Subject: [PATCH 2/2] added multifile select option to file input injector, and set to true for obj's, so .mtl's can be selected again --- .../Netherlands3D/Plugins/Jslib/FileUploads.jslib | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/3DAmsterdam/Assets/Netherlands3D/Plugins/Jslib/FileUploads.jslib b/3DAmsterdam/Assets/Netherlands3D/Plugins/Jslib/FileUploads.jslib index df921ae38..a4776a947 100644 --- a/3DAmsterdam/Assets/Netherlands3D/Plugins/Jslib/FileUploads.jslib +++ b/3DAmsterdam/Assets/Netherlands3D/Plugins/Jslib/FileUploads.jslib @@ -14,21 +14,22 @@ mergeInto(LibraryManager.library, { window.dbVersion = 21; //Inject our required html input fields - window.InjectHiddenFileInput = function InjectHiddenFileInput(type, acceptedExtentions) { + window.InjectHiddenFileInput = function InjectHiddenFileInput(type, acceptedExtentions, multiFileSelect) { var newInput = document.createElement("input"); newInput.id = type + '-input'; newInput.type = 'file'; newInput.accept = acceptedExtentions; + newInput.multiple = multiFileSelect; newInput.onchange = function () { window.ReadFiles(this.files); }; newInput.style.cssText = 'display:none; cursor:pointer; opacity: 0; position: fixed; bottom: 0; left: 0; z-index: 2; width: 0px; height: 0px;'; document.body.appendChild(newInput); }; - window.InjectHiddenFileInput('obj', '.obj,.mtl'); - window.InjectHiddenFileInput('csv', '.csv,.tsv'); - window.InjectHiddenFileInput('fzp', '.fzp'); - window.InjectHiddenFileInput('geojson', '.json,.geojson'); + window.InjectHiddenFileInput('obj', '.obj,.mtl', true); + window.InjectHiddenFileInput('csv', '.csv,.tsv', false); + window.InjectHiddenFileInput('fzp', '.fzp', false); + window.InjectHiddenFileInput('geojson', '.json,.geojson', false); //Support for dragging dropping files on browser window document.addEventListener("dragover", function (event) {