First of all thank you for your courses! I’ve learned so much so far.
To my Problem:
I produce in Unity 6.0 a WebGL-Game that should work in a Browser on a desktop-PC and on mobile devices.
The mobile - joysticks should be auto-activated when the game is running on a mobile-device and deactivated, wenn its running on the PC.
I have found one possible solution via a .jslib - Plugin but the and one in C# via Application.isMobilePlatform, buit nothing worked for me.
This is all that is in there. It now probably gets built into the game
Then, I had a ‘bootstrap’ script that gets this value and sets a property but it also has a lot of other things, so here’s a quick script that should work
public class GlobalStuff
{
[DllImport("__Internal")]
private static extern bool IsMobileBrowser();
private static bool? _mobileBrowser;
public static bool MobileBrowser
{
get
{
if (!_mobileBrowser.HasValue)
_mobileBrowser = IsMobileBrowser();
return _mobileBrowser.Value;
}
}
}
This just waits for the first time the value is requested (lazy load) and then caches it so it doesn’t have to ask the jslib for the value each time. You should now be able to just do GlobalStuff.MobileBrowser to check if it’s a mobile browser or not.
I don’t know much about this but it worked for me. Here’s some related documentation
Thank you for your hints.
I tried it and everything seems to work.
But when I emulate the mobile Device in ChromeDev-Tools it works. But on my real Android mobile it does not work.
It may be that the user agent does not match any of the ones it’s checking for. Add another script that will give you the user agent and log it to the console. Then you can troubleshoot it from there. It will be something like
public class GlobalStuff
{
[DllImport("__Internal")]
private static extern bool IsMobileBrowser();
[DllImport("__Internal")]
private static extern string GetUserAgent();
private static bool? _mobileBrowser;
public static bool MobileBrowser
{
get
{
if (!_mobileBrowser.HasValue)
_mobileBrowser = IsMobileBrowser();
return _mobileBrowser.Value;
}
}
public static void DumpUserAgent()
{
Debug.Log($"Current User Agent: {GetUserAgent()}");
}
}
Call GlobalStuff.DumpUserAgent() from somewhere. It should write the user agent of your real Android mobile to the console and you could determine if it’s an agent you’re not looking for, or something else.