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

    bearhunter

    Joined:
    Mar 17, 2013
    Posts:
    3
    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

    https://forum.unity.com/threads/unity-2020-1-sendmessage-no-longer-works-help.842209/

    and

    https://forum.unity.com/threads/cha...-templates-introduced-in-unity-2020-1.817698/

    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.          progressBarFull.style.width = 100 * progress + "%";
    6.        }).then((unityInstance) => {
    7.        unityInstance.SendMessage("isitaphone", "IsPhoneFunc", 5);
    8.          loadingBar.style.display = "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;
    5.  
    6. public class SprayClick : MonoBehaviour
    7. {
    8.  
    9.    public GameObject phoneRef;
    10.    public GameObject phoneRef2;
    11.  
    12.    public bool isTouchies;
    13.    private int num2;
    14.  
    15.    private void Start()
    16.    {
    17.  
    18.        if (num2 == 5)
    19.        {
    20.            phoneRef.GetComponent<Gameplay>().isPhone2 = true;
    21.            phoneRef2.GetComponent<StartMenu>().isPhone = true;
    22.        }
    23.  
    24.    }
    25.  
    26.    public void Update()
    27.    {
    28.        if (num2 == 5)
    29.        {
    30.            phoneRef.GetComponent<Gameplay>().isPhone2 = true;
    31.            phoneRef2.GetComponent<StartMenu>().isPhone = true;
    32.        }
    33.    }
    34.  
    35.    public void IsPhoneFunc(int num)
    36.    {
    37.        isTouchies = true;
    38.        num2 = num;
    39.    }
    40.  
    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

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    951
    Marks4 likes this.
  3. bearhunter

    bearhunter

    Joined:
    Mar 17, 2013
    Posts:
    3
    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

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    951
    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 https://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html on how to integrate JS code via .jslib files.