Added WebGL Mobile Input plugin

Added 3D camera controls
This commit is contained in:
2024-12-05 04:24:10 +01:00
parent a3e3cc450a
commit a41f50f138
32 changed files with 1622 additions and 4 deletions

View File

@@ -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();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5fcbb4f34ed8e894896251ff74a4633d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -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);

View File

@@ -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: