How can I determine the type of device the WebGL game is running from?

Hello! I’m making a game for WebGL and I’m having trouble controlling my character.

He must turn right and left. Rotation is done using Input.GetAxis(“Horizontal”). Since I mean the game can be played from a phone, I made rotation buttons. But I need to define the device type so as not to call rotation = Input.GetAxis(“Horizontal”).

I tried through SystemInfo, but it didn’t work. Help me please!

Dont set your font to such a nasty colour, it doesnt show on white…

so, the web page itself detects some kinds of devices… from that you can engage touch controls or not (and there are tutorials on passing info into games and out)

1 Like

I’m unsure about more recent versions of Unity, they very well may have added some simple function to call.

But in the past when I’ve needed this, I do it as follows.

  1. create a plugins folder and create a “YourPluginName.jslib”:
    9862356--1420920--upload_2024-5-29_18-6-11.png
    (in my case I named it SPUnityLink.jslib, you can call it whatever)

  2. in said jslib file add this:

mergeInto(LibraryManager.library, {
  IsMobileBrowser: function () {
    return (/iPhone|iPad|iPod|Android/i.test(navigator.userAgent));
  },
  IsPreferredDesktopPlatform: function() {
    return (/Win64|Mac OS X|Linux x86_64/i.test(navigator.userAgent));
  }
  });
  1. configure the file in the editor like so:
    9862356--1420923--upload_2024-5-29_18-7-26.png

  2. Lastly in your project somewhere define these functions:

#if !UNITY_EDITOR && UNITY_WEBGL

        [System.Runtime.InteropServices.DllImport("__Internal")]
        public static extern bool IsMobileBrowser();
      
        [System.Runtime.InteropServices.DllImport("__Internal")]
        public static extern bool IsPreferredDesktopPlatform();
#else
        public static bool IsMobileBrowser() => false;
        public static bool IsPreferredDesktopPlatform() => true;
#endif

Note that I’ve used compiler directives so that this is only included if I’ve actually built to webgl.

3 Likes