I need to detect whether the user is running a webgl app in a mobile browser or on a desktop/laptop browser. Is this possible to detect using the Unity API, or you need to do some HTML hack?
4 Answers
4Found the answer. In case anyone else needs it:
in assets/plugins/webgl/MyPlugin.jslib
var MyPlugin = {
IsMobile: function()
{
return UnityLoader.SystemInfo.mobile;
}
};
mergeInto(LibraryManager.library, MyPlugin);
Then from unity:
[DllImport("__Internal")]
private static extern bool IsMobile();
public bool isMobile()
{
#if !UNITY_EDITOR && UNITY_WEBGL
return IsMobile();
#endif
return false;
}
Tested with Unity 2022.3:
bool isWebGLOnDesktop = !Application.isMobilePlatform
&& Application.platform == RuntimePlatform.WebGLPlayer;
bool isWebGLOnMobile = Application.isMobilePlatform
&& Application.platform == RuntimePlatform.WebGLPlayer;
Checking the docs about this property sadly is not always true: Due to privacy and anonymization reasons, this property might not always report accurate information on all web browsers. Note: On Universal Windows Platform, tablets are treated as desktop machines, therefore, this property returns true only when running on phones and IoT device family devices. https://docs.unity3d.com/6000.1/Documentation/ScriptReference/Application-isMobilePlatform.html
– NeogeneUsing Unity 2021.2.9f1 and I don’t see ‘assets/plugins/webgl/MyPlugin.jslib’ am I missing something? Can someone direct me toward the location? Or, is there a 2021 equivalent?
In your Unity project folder, you should have an "Assets/plugins" directory. The "WebGL" folder is one you can create yourself, and "MyPlugin.jslib" is a regular text file, but with a *.jslib extension (instead of *.txt, or *.cs).
– jamesbeanHi, @TOES , can we know with this method if we are on iphone or ipad in addition ? Thank you ![]()
(i need to know if i am on ipad or iphone to play video without sound
)
Since "allUnits" is an array you won't see any preformance differences between for and foreach since it would be implemented as for loop. Also the for loop is only executed once when you release the mouse button. So this isn't something where preformance plays any role
– Bunny83
Gosh, Thank you so much. It saved me a lot of time!
– hcjjeong@TOES I'm using exactly this solution, I attached the script to a empty game object. The game runs in the editor, but when I publish it to the browser I get this error: ![alt text][1] [1]: /storage/temp/168980-webgl-mobile.png Any ideas?
– Mikad0