Skip to content

Commit

Permalink
Add file picker to WebGL #391
Browse files Browse the repository at this point in the history
  • Loading branch information
vanjac committed Jul 21, 2024
1 parent 8ba8d16 commit 6081663
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Assets/Game/GameInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public static class GameInput {
public static bool virtJump;
public static Vector2 virtLook;

#if UNITY_WEBGL
#if UNITY_WEBGL && !UNITY_EDITOR
[DllImport("__Internal")]
private static extern bool IsPointerLocked();
#else
Expand Down Expand Up @@ -42,7 +42,7 @@ public static Vector2 GetLookDelta() {
return virtLook;
} else if (IsPointerLocked()) {
var mouseVec = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
#if UNITY_WEBGL
#if UNITY_WEBGL && !UNITY_EDITOR
mouseVec /= 2;
#endif
return mouseVec;
Expand Down
30 changes: 30 additions & 0 deletions Assets/Menu/MenuGUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
using System.IO;
using UnityEngine;
using UnityEngine.SceneManagement;
#if UNITY_WEBGL
using System.Runtime.InteropServices;
#endif

public class MenuGUI : GUIPanel {
public TextAsset indoorTemplate, floatingTemplate;
Expand Down Expand Up @@ -36,6 +39,9 @@ void Start() {
}

public override void WindowGUI() {
#if UNITY_WEBGL
PlayerMenuGUI();
#else
if (worldPaths.Count == 0) {
GUILayout.FlexibleSpace();
GUILayout.BeginHorizontal();
Expand Down Expand Up @@ -85,6 +91,21 @@ public override void WindowGUI() {
}
GUILayout.EndScrollView();
}
#endif
}

private void PlayerMenuGUI() {
GUILayout.FlexibleSpace();
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();

if (GUILayout.Button("Select File", StyleSet.buttonLarge)) {
OpenFilePicker();
}

GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
GUILayout.FlexibleSpace();
}

public static void OpenWorld(string path, string scene) {
Expand Down Expand Up @@ -208,4 +229,13 @@ private void CopyWorld(string newName) {
} catch (IOException) { }
UpdateWorldList();
}

#if UNITY_WEBGL
[DllImport("__Internal")]
private static extern bool OpenFilePicker();

void LoadWebGLFile() {
OpenWorld("/tmp/mapsave", Scenes.GAME);
}
#endif
}
28 changes: 27 additions & 1 deletion Assets/Plugins/WebGL/plugin.jslib
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
mergeInto(LibraryManager.library, {
IsPointerLocked: function() {
IsPointerLocked: function () {
return document.pointerLockElement != null
},

OpenFilePicker: function () {
var fileSelect = document.querySelector('input')
if (fileSelect == null) {
fileSelect = document.createElement('input')
fileSelect.type = 'file'
fileSelect.style.display = 'none'
fileSelect.style.visibility = 'hidden'

fileSelect.addEventListener('change', function () {
if (fileSelect.files.length == 1) {
var reader = new FileReader()
reader.onload = function () {
if (reader.result instanceof ArrayBuffer) {
FS.writeFile('/tmp/mapsave', new Uint8Array(reader.result))
SendMessage('Main Camera', 'LoadWebGLFile')
}
}
reader.readAsArrayBuffer(fileSelect.files[0])
}
})

document.body.appendChild(fileSelect)
}
fileSelect.click()
},
})

0 comments on commit 6081663

Please sign in to comment.