Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Detect explicity Android or iOS platform over WebGL on mobiles.

Discussion in 'WebGL' started by Tangerunity3d, Feb 5, 2021.

  1. Tangerunity3d

    Tangerunity3d

    Joined:
    Jun 9, 2020
    Posts:
    13
    Hello Everybody,

    Is there a way to detect which platform (either Android iOS or even Windows) and app is running over WebGL?

    I have made some tests without luck, I'm just able to detect if I'm running on Mobile or PC but not a specific platform.

    Any insights will be appreciated.

    Here is the code I have tried, I also use the javascript code from jslib, you can see the reference here:

    https://forum.unity.com/threads/how-to-detect-if-a-mobile-is-running-the-webgl-scene.440344/


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Platform : MonoBehaviour
    7. {
    8.  
    9.     public Text text1;
    10.     public Text textSO;
    11.     private void Start()
    12.     {
    13.         //text.text = "Mobile: " + IsMobileBrowser();
    14.  
    15.         if (IsMobileBrowser())
    16.         {
    17.             text1.text = "Mobile: " + Application.isMobilePlatform;          
    18.          
    19.             if (Application.platform == RuntimePlatform.Android)
    20.             {
    21.                 textSO.text = "Mobile Android: " + Application.platform;
    22.             }
    23.             else if (Application.platform == RuntimePlatform.IPhonePlayer)
    24.             {
    25.                 textSO.text = "Mobile IOS: " + Application.platform;
    26.             }
    27.             else
    28.             {
    29.                 textSO.text = "SO: " + Application.platform;
    30.             }
    31.         }
    32.         else
    33.         {
    34.             text1.text = "Mobile: " + Application.isMobilePlatform;
    35.             textSO.text = "SO: " + Application.platform;
    36.         }
    37.  
    38.     }
    39.     public static bool IsMobileBrowser()
    40.     {
    41.         #if UNITY_EDITOR
    42.                 return false; // value to return in Play Mode (in the editor)
    43.         #elif UNITY_WEBGL
    44.             return WebGLHandler.IsMobileBrowser(); // value based on the current browser
    45.         #else
    46.             return false; // value for builds other than WebGL
    47.         #endif
    48.     }
    49.  
    50. }
    Code (JavaScript):
    1.     mergeInto(LibraryManager.library, {
    2.       IsMobileBrowser: function () {
    3.         return (/iPhone|iPad|iPod|Android/i.test(navigator.userAgent));
    4.       }, [...]
     
    Last edited: Feb 5, 2021
  2. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    944
    The Application.platform field will say https://docs.unity3d.com/ScriptReference/RuntimePlatform.WebGLPlayer.html on WebGL builds.

    You can detect mobile browsers by adapting the JS library snippet that you linked:

    Code (JavaScript):
    1. mergeInto(LibraryManager.library, {
    2.       IsIOSBrowser: function () {
    3.         return (/iPhone|iPad|iPod/i.test(navigator.userAgent));
    4.       },
    5.       IsAndroidBrowser: function () {
    6.         return (/Android/i.test(navigator.userAgent));
    7.       },
    8.       IsDesktopBrowser__deps: ['IsIOSBrowser', 'IsAndroidBrowser'],
    9.       IsDesktopBrowser: function () {
    10.         return !_IsIOSBrowser() && !_IsAndroidBrowser();
    11.       }, [...]
     
    Julhiecio likes this.
  3. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    944
  4. johanepic

    johanepic

    Joined:
    Sep 21, 2021
    Posts:
    4
    Detecting mobile device using user agent isn't the best way to check if a user is using a mobile device since user agent strings can be spoofed easily. However, this method is still an easy way to check what device is being used by the user.

    There is a JavaScript API built-in for detecting media. The JavaScript window.matchMedia() method returns a MediaQueryList object representing the results of the specified CSS media query string. You can use this method to detect mobile device based on the CSS media query.

    Code (CSharp):
    1. <script>
    2. $(document).ready(function(){
    3.   let isMobileDevice = window.matchMedia("only screen and (max-width: 760px)").matches;
    4.   if(isMobileDevice){
    5.     // The viewport is less than 768 pixels wide
    6.     //Conditional script here
    7.   } else{
    8.     //The viewport is greater than 700 pixels wide
    9.     alert("This is not a mobile device.");
    10.   }
    11. });
    12. </script>
    You can use above script to do show/hide elements depending on the screen size.
     
  5. Amelieg45

    Amelieg45

    Joined:
    Oct 28, 2021
    Posts:
    1
    Hello johanepic,

    Where can we use this script in our Unity project :oops:?

    I have a 2D game with videos, exported in webGL, and I would need to identify if the game is playing in chrome on iphone or ipad to play the video without sound, this script would help me a lot.

    Thanks to you
     
  6. JayadevHaddadi

    JayadevHaddadi

    Joined:
    Nov 9, 2020
    Posts:
    12
    Enough with:

    Application.platform == RuntimePlatform.WebGLPlayer && Application.isMobilePlatform
     
    yaumito and ArchVizPRO like this.