Skip to content

Commit 010faf3

Browse files
committed
Merge branch 'master' into 2021.3-urp
2 parents f4676c8 + 9de9184 commit 010faf3

File tree

12 files changed

+298
-62
lines changed

12 files changed

+298
-62
lines changed

Assets/Plugins/WebGL/WebGLTools/WebGlBridge.Commands.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,18 @@ public void ToggleInfoPanel()
122122
WebGlPlugins.ToggleInfoPanel();
123123
}
124124

125+
/// <summary>
126+
/// Log the user agent of the browser and if this agent is classified as mobile
127+
/// Browser Usage: <code>unityGame.SendMessage("WebGL", "LogUserAgent");</code>
128+
/// </summary>
129+
[WebGlCommand(Description = "Log User Agent and isMobileDevice")]
130+
public void LogUserAgent()
131+
{
132+
string userAgent = WebGlPlugins.GetUserAgent();
133+
bool isMobileDevice = WebGlPlugins.IsMobileDevice();
134+
Debug.Log($"<color=#4D65A4>User Agent:</color> '{userAgent}', <color=#4D65A4>IsMobileDevice:</color> '{isMobileDevice}'");
135+
}
136+
125137
/// <summary>
126138
/// Log example messages to show off unity rich text parsing to html & console styling
127139
/// </summary>

Assets/Plugins/WebGL/WebGLTools/WebGlPlugins.cs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public static class WebGlPlugins
2929
[DllImport("__Internal")]
3030
private static extern void _HideInfoPanel();
3131
[DllImport("__Internal")]
32+
private static extern string _GetUserAgent();
33+
[DllImport("__Internal")]
3234
private static extern uint _GetTotalMemorySize();
3335
[DllImport("__Internal")]
3436
private static extern uint _GetStaticMemorySize();
@@ -51,7 +53,7 @@ public static void SetVariable(string variableName, string value)
5153
#endif
5254
Debug.Log($"<color=#00CCCC>{nameof(WebGlPlugins)}.{nameof(SetVariable)} set {variableName}: {value}</color>");
5355
}
54-
56+
5557
/// <summary>
5658
/// Adds a time marker at the call time to the javascript map "unityTimeTrackers"
5759
/// The mapped value is performance.now() at the time of the call
@@ -65,7 +67,7 @@ public static void AddTimeTrackingEvent(string eventName)
6567
Debug.Log($"{nameof(WebGlPlugins)}.{nameof(AddTimeTrackingEvent)} called with {eventName}");
6668
#endif
6769
}
68-
70+
6971
/// <summary>
7072
/// Show the info panel in the top right corner
7173
/// By default triggered by <see cref="WebGlTimeTracker"/> in Awake
@@ -80,7 +82,7 @@ public static void ShowInfoPanel()
8082
Debug.Log($"{nameof(WebGlPlugins)}.{nameof(ShowInfoPanel)} called");
8183
#endif
8284
}
83-
85+
8486
/// <summary>
8587
/// Hide the info panel in the top right corner
8688
/// By default triggered by <see cref="WebGlTimeTracker"/> in Awake
@@ -111,6 +113,33 @@ public static void ToggleInfoPanel()
111113
}
112114
}
113115

116+
/// <summary>
117+
/// Get navigator.userAgent from the browser
118+
/// </summary>
119+
/// <returns>navigator.userAgent</returns>
120+
public static string GetUserAgent()
121+
{
122+
#if UNITY_WEBGL && !UNITY_EDITOR
123+
return _GetUserAgent();
124+
#else
125+
Debug.Log($"{nameof(WebGlPlugins)}.{nameof(GetUserAgent)} called");
126+
return "undefined";
127+
#endif
128+
}
129+
130+
/// <summary>
131+
/// Check user agent to determine if the game runs on a mobile device
132+
/// </summary>
133+
/// <returns>true if on phone or tablet</returns>
134+
public static bool IsMobileDevice()
135+
{
136+
string userAgent = GetUserAgent();
137+
return userAgent.Contains("iPhone") ||
138+
userAgent.Contains("iPad") ||
139+
userAgent.Contains("iPod") ||
140+
userAgent.Contains("Android");
141+
}
142+
114143
/// <summary>
115144
/// Get the total memory size used by the application in MB
116145
/// </summary>
@@ -194,7 +223,7 @@ public static float GetManagedMemorySize()
194223
var bytes = (uint) GC.GetTotalMemory(false);
195224
return GetMegaBytes(bytes);
196225
}
197-
226+
198227
/// <summary>
199228
/// Converts bytes (B) to mega bytes (MB)
200229
/// </summary>

Assets/Plugins/WebGL/WebGLTools/WebGlPlugins.jslib

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,26 @@ var WebGlPlugins =
6060
setInfoPanelVisible(false);
6161
}
6262
},
63-
63+
64+
_GetUserAgent: function () {
65+
var userAgent = navigator.userAgent;
66+
67+
//Get size of the string
68+
var bufferSize = lengthBytesUTF8(userAgent) + 1;
69+
//Allocate memory space
70+
var buffer = _malloc(bufferSize);
71+
72+
//Copy old data to the new one then return it
73+
if(typeof stringToUTF8 !== 'undefined') {
74+
stringToUTF8(userAgent, buffer, bufferSize);
75+
}
76+
else if(typeof writeStringToMemory !== 'undefined') {
77+
writeStringToMemory(userAgent, buffer);
78+
}
79+
80+
return buffer;
81+
},
82+
6483
_GetTotalMemorySize: function()
6584
{
6685
if(typeof TOTAL_MEMORY !== 'undefined') {

Assets/WebGLTemplates/Develop/debug-console.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -270,15 +270,33 @@ function setupConsoleLogPipe() {
270270
let defaultConsoleError = console.error;
271271

272272
// Overwrite log functions to parse and pipe to debug html console
273-
console.log = (message) => { parseMessageAndLog(message, 'log', defaultConsoleLog); };
274-
console.info = (message) => { parseMessageAndLog(message, 'info', defaultConsoleInfo); };
275-
console.debug = (message) => { parseMessageAndLog(message, 'debug', defaultConsoleDebug); };
276-
console.warn = (message) => { parseMessageAndLog(message, 'warn', defaultConsoleWarn); };
277-
console.error = (message) => { parseMessageAndLog(message, 'error', defaultConsoleError); errorReceived(); };
273+
console.log = (message) => { handleLog(message, 'log', defaultConsoleLog); };
274+
console.info = (message) => { handleLog(message, 'info', defaultConsoleInfo); };
275+
console.debug = (message) => { handleLog(message, 'debug', defaultConsoleDebug); };
276+
console.warn = (message) => { handleLog(message, 'warn', defaultConsoleWarn); };
277+
console.error = (message) => { handleLog(message, 'error', defaultConsoleError); errorReceived(); };
278278

279279

280-
parseMessageAndLog = (message, logLevel, consoleLogFunction) => {
280+
handleLog = (message, logLevel, consoleLogFunction) => {
281281
updateLogCounter(logLevel);
282+
if (typeof message === 'string') {
283+
// Only parse messages that are actual strings
284+
parseMessageAndLog(message, logLevel, consoleLogFunction);
285+
}
286+
else {
287+
consoleLogFunction(message);
288+
// Try to also log the object to the html console (does not always work in a meaningful manner)
289+
var htmlMessage;
290+
try {
291+
htmlMessage = JSON.stringify(message);
292+
} catch (error) {
293+
htmlMessage = `Can't convert message to JSON: ${error}`;
294+
}
295+
htmlLog(htmlMessage, logLevel);
296+
}
297+
};
298+
299+
parseMessageAndLog = (message, logLevel, consoleLogFunction) => {
282300
let styledTextParts = parseUnityRichText(message);
283301

284302
let consoleText = '';

Assets/WebGLTemplates/Develop/index.html

Lines changed: 85 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
<head>
55
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
6-
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
77
<title>{{{ PRODUCT_NAME }}}</title>
88
<style>
99
* {
@@ -24,26 +24,85 @@
2424
text-align: center;
2525
}
2626

27+
#unity-container {
28+
position: absolute;
29+
left: 0px;
30+
top: 0px;
31+
width: 100%;
32+
height: 100%;
33+
display: flex;
34+
align-items: center;
35+
justify-content: center;
36+
#if CONTAINER_BACKGROUND_COLOR
37+
background-color: {{{CONTAINER_BACKGROUND_COLOR}}};
38+
#endif
39+
}
40+
41+
/* Default values, might be overwritten by aspect ratio media queries */
2742
#unity-canvas {
43+
width: 100%;
44+
height: 100%;
45+
background-color: #fff;
46+
}
47+
48+
#if MIN_ASPECT_RATIO
49+
@media (min-aspect-ratio: {{{ MIN_ASPECT_RATIO }}}) {
50+
#unity-canvas {
51+
width: auto;
52+
height: 100%;
53+
aspect-ratio: {{{ MIN_ASPECT_RATIO }}};
54+
}
55+
}
56+
#endif
57+
58+
#if MAX_ASPECT_RATIO
59+
@media (max-aspect-ratio: {{{ MAX_ASPECT_RATIO }}}) {
60+
#unity-canvas {
61+
width: 100%;
62+
height: auto;
63+
aspect-ratio: {{{ MAX_ASPECT_RATIO }}};
64+
}
65+
}
66+
#endif
67+
68+
#unity-loading-container {
2869
position: absolute;
2970
left: 0px;
3071
top: 0px;
3172
width: 100%;
3273
height: 100%;
74+
background-color: #fff;
75+
display: flex;
76+
align-items: center;
77+
justify-content: center;
78+
flex-direction: column;
79+
gap: 40px;
80+
81+
opacity: 1;
82+
visibility: visible;
83+
transition: 800ms linear;
84+
}
85+
86+
#unity-loading-container.finished {
87+
opacity: 0;
88+
visibility: collapse;
89+
}
90+
91+
#unity-loading-container .logo {
92+
width: 15%;
93+
height: 15%;
3394
}
3495

3596
#unity-loading-bar {
36-
position: absolute;
37-
left: 30%;
38-
top: 50%;
97+
position: relative;
3998
width: 40%;
4099
height: 10px;
41100
background-color: #fff;
42101
border: 1px solid #ccc;
43102
border-radius: 10px;
44103
}
45104

46-
#unity-progress-bar {
105+
#unity-loading-bar-inner {
47106
position: absolute;
48107
left: 0%;
49108
top: 0%;
@@ -58,21 +117,17 @@
58117
</head>
59118

60119
<body>
61-
<script src="./debug-console.js"></script>
62-
<canvas id="unity-canvas"></canvas>
63-
<div id="unity-loading-bar">
64-
<div id="unity-progress-bar">
120+
<div id="unity-container">
121+
<canvas id="unity-canvas"></canvas>
122+
</div>
123+
<div id="unity-loading-container">
124+
<img src="logo.svg" class="logo" alt="Logo {{{ COMPANY_NAME }}}">
125+
<div id="unity-loading-bar">
126+
<div id="unity-loading-bar-inner"></div>
65127
</div>
66128
</div>
67129
<script src="Build/{{{ LOADER_FILENAME }}}"></script>
68130
<script>
69-
const maxPixelRatioMobile = 2.0;
70-
const maxPixelRatioDesktop = 1.5;
71-
72-
var isMobile = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);
73-
var maxDevicePixelRatio = isMobile? maxPixelRatioMobile: maxPixelRatioDesktop;
74-
var pixelRatio = Math.min(window.devicePixelRatio, maxDevicePixelRatio);
75-
76131
var buildUrl = "Build";
77132
var loaderUrl = buildUrl + "/{{{ LOADER_FILENAME }}}";
78133
var config = {
@@ -89,28 +144,36 @@
89144
companyName: "{{{ COMPANY_NAME }}}",
90145
productName: "{{{ PRODUCT_NAME }}}",
91146
productVersion: "{{{ PRODUCT_VERSION }}}",
92-
devicePixelRatio: pixelRatio
93147
};
94148

149+
var isMobile = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);
150+
if(isMobile) {
151+
// Define a maximum pixel ratio for mobile to avoid rendering at too high resolutions
152+
const maxPixelRatioMobile = 2.0;
153+
config.devicePixelRatio = Math.min(window.devicePixelRatio, maxPixelRatioMobile);
154+
}
155+
95156
var canvas = document.querySelector("#unity-canvas");
96-
var loadingBar = document.querySelector("#unity-loading-bar");
97-
var progressBar = document.querySelector("#unity-progress-bar");
157+
var loadingContainer = document.querySelector("#unity-loading-container");
158+
var loadingBar = document.querySelector("#unity-loading-bar-inner");
98159

99-
var unityGame; // This can be used to access the application with .SendMessage() commands
160+
var unityGame; // This variable can be used to access the application with .SendMessage() commands
100161
var script = document.createElement("script");
101162
script.src = loaderUrl;
102163
script.onload = function() {
103164
createUnityInstance(canvas, config, function(progress) {
104-
progressBar.style.width = 100 * progress + "%";
165+
loadingBar.style.width = 100 * progress + "%";
105166
}).then(function(unityInstance) {
106167
unityGame = unityInstance;
107-
loadingBar.style.display = "none";
168+
loadingContainer.classList.add("finished");
108169
}).catch(function(message) {
109170
alert(message);
110171
});
111172
};
112173
document.body.appendChild(script);
113174
</script>
175+
<!-- Add an html debug console and handle unity rich text styling for both html and browser console-->
176+
<script src="./debug-console.js"></script>
114177
</body>
115178

116179
</html>
Lines changed: 2 additions & 0 deletions
Loading

Assets/WebGLTemplates/Develop/logo.svg.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)