Skip to content

Commit 2c9ab15

Browse files
committed
Add interface to get navigator.userAgent in unity
Useful to check if you're on mobile
1 parent e147b91 commit 2c9ab15

File tree

3 files changed

+65
-5
lines changed

3 files changed

+65
-5
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') {

0 commit comments

Comments
 (0)