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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Help with unityInstance.SendMessage (2020.1.7f1)

Discussion in 'Web' started by bearhunter, Oct 21, 2020.

  1. bearhunter


    Mar 17, 2013
    I'm somewhat of an amateur when it comes to Unity, and there's one issue I've been trying to work through for two days now. I have a game made that is fully playable in browser (WebGL), no issues or anything, but I'd like to add a detection function that activates if the user is on a phone or tablet while playing. I've tried several different options, including creating a .jslib plugin, but the documentations are outdated and cause instant crashes as it seems the build files have been overhauled recently. I've found a few people talking about this in


    but playing around with index.html to find somewhere for the javascript to interact with unity code has gotten me nowhere so far. Right now I'm trying to just have the SendMessage function work at all, but it doesn't affect my unity script at all. From what I understand, the function is supposed to work if you put it here:

    Code (JavaScript):
    1. var script = document.createElement("script");
    2.      script.src = loaderUrl;
    3.      script.onload = () => {
    4.        createUnityInstance(canvas, config, (progress) => {
    5. = 100 * progress + "%";
    6.        }).then((unityInstance) => {
    7.        unityInstance.SendMessage("isitaphone", "IsPhoneFunc", 5);
    8. = "none";
    9.          fullscreenButton.onclick = () => {
    10.            unityInstance.SetFullscreen(1);
    11.          };
    12.        }).catch((message) => {
    13.          alert(message);
    14.        });
    15.      };
    16.      document.body.appendChild(script);
    17.    </script>
    18.   </body>
    19. </html>
    This is what my unity script looks like, attached to gameobject isitaphone (the bool is currently unused, tried that before I attempted adding a number to see if anything changed):

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.Runtime.InteropServices;
    6. public class SprayClick : MonoBehaviour
    7. {
    9.    public GameObject phoneRef;
    10.    public GameObject phoneRef2;
    12.    public bool isTouchies;
    13.    private int num2;
    15.    private void Start()
    16.    {
    18.        if (num2 == 5)
    19.        {
    20.            phoneRef.GetComponent<Gameplay>().isPhone2 = true;
    21.            phoneRef2.GetComponent<StartMenu>().isPhone = true;
    22.        }
    24.    }
    26.    public void Update()
    27.    {
    28.        if (num2 == 5)
    29.        {
    30.            phoneRef.GetComponent<Gameplay>().isPhone2 = true;
    31.            phoneRef2.GetComponent<StartMenu>().isPhone = true;
    32.        }
    33.    }
    35.    public void IsPhoneFunc(int num)
    36.    {
    37.        isTouchies = true;
    38.        num2 = num;
    39.    }
    41. }
    I'm wondering if there's any easier way to pass information from index.html to unity, or if my code is wrong somewhere. I thought perhaps the function being called too early, before gameobjects are loaded, could also be the reason it doesn't work, but in that case, is there a workaround? Greatly appreciate help.

    Edit: Before I forget: yes, I do upload the project (to itch) for playtesting, and yes, I am tired of exporting and editing the build files every time I do this.
  2. jukka_j


    Unity Technologies

    May 4, 2018
    Marks4 likes this.
  3. bearhunter


    Mar 17, 2013
    Unfortunately, this function doesn't work (at least for me) to detect mobile on WebGL. Can probably write a plugin to send information between WebGL and Unity - still trying to figure that one out - but for now I'm just adding an extra button for change inputs in the game. At least the game runs fine.
  4. jukka_j


    Unity Technologies

    May 4, 2018
    Can you be more specific on which mobile device(s) it fails to detect? Tried this out against an Android phone, where it did work properly. Maybe our mobile check is not good enough. I believe our check is based on searching navigator.userAgent for strings "Android", "iPad" and "iPhone".

    Alternatively you can write a .jslib file that computes the needed mobile check code. That is hundreds of times faster than using SendMessage, and simpler than creating a plugin. See on how to integrate JS code via .jslib files.