Search Unity

Unity 2020.1 sendMessage no longer works. Help

Discussion in 'Web' started by harrywenjie, Mar 7, 2020.

  1. harrywenjie

    harrywenjie

    Joined:
    Jun 25, 2018
    Posts:
    12
    The new version does not have instantiate in the .js file.
    There is no this line
    Code (JavaScript):
    1. var unityInstance = UnityLoader.instantiate("unityContainer", "Build/YourBuild.json", {onProgress: UnityProgress});
    If I try
    Code (JavaScript):
    1. unityInstance.SendMessage("ObjectName","methodname",value);
    The console in brower reports unityInstance not defined.

    How do I send message from browser to webgl now?
     
  2. ArnaudH

    ArnaudH

    Joined:
    Apr 5, 2019
    Posts:
    5
    It has changed to the following signature:

    Code (JavaScript):
    1. function createUnityInstance(canvas, config, onProgress) {
    2.   ...
    3. }
    The template has been changed to:

    Code (JavaScript):
    1. createUnityInstance(document.querySelector("#unity-canvas"), {
    2.         dataUrl: "...",
    3.         frameworkUrl: "...",
    4.         codeUrl: "...",
    5.         streamingAssetsUrl: "...",
    6.         companyName: "...",
    7.         productName: "...",
    8.         productVersion: "...",
    9.       });
    This means only the function "createUnityInstance" will now be visible as global, and you need to instantiate your own "unityInstance" variable with it. Also note it now requires the canvas object to be passed instead of the id.
     
  3. harrywenjie

    harrywenjie

    Joined:
    Jun 25, 2018
    Posts:
    12
    Thank you. But how exactly do I instantiate with createUnityInstance? I tried to put a "var unityInstance =" infront of it in the index.html. but then it reports sendMessage is not a function.
     
    Last edited: Mar 7, 2020
  4. ArnaudH

    ArnaudH

    Joined:
    Apr 5, 2019
    Posts:
    5
    It's usually a good idea to log the objects you're creating when you're exploring. Try putting in a "console.log(unityInstance)". I believe it's either under the Module object, or it should be capitalised as "SendMessage". Just log it in the console and explore the object, you should see it somewhere in there.
     
  5. harrywenjie

    harrywenjie

    Joined:
    Jun 25, 2018
    Posts:
    12
    Here is the log, and I did
    var unityInstance = createUnityInstance.....
    console.log(unityInstance)
    Please help, I don't understand, I see SendMessage right there, and I tried "SendMessage" with captial S, doesn't work either.

    Annotation 2020-03-08 110855.jpg
     
  6. ArnaudH

    ArnaudH

    Joined:
    Apr 5, 2019
    Posts:
    5
    createUnityInstance now returns a Promise, you need to add ".then((unityInstance) => {... do something with it ...})" after it and continue a chain from there. More info on promises can be found here: https://developers.google.com/web/fundamentals/primers/promises.

    The reason you're seeing the object function, but it says undefined when logging is because promises do not resolve immediately. If you log the variable immediately after the promise, it will be undefined as the Promise has not resolved yet. It will show up in the console, but with a small "i" next to it which will indicate the browser "filled it in when it eventually became available afterwards". It's a useful browser feature which can surely trip you up if you don't understand how promises work.

    See this link for all other info on how to migrate: https://forum.unity.com/threads/cha...-templates-introduced-in-unity-2020-1.817698/
     
    mekartikshah and De-Panther like this.
  7. harrywenjie

    harrywenjie

    Joined:
    Jun 25, 2018
    Posts:
    12
    Works fine now. Thank you so much for your help. I understand it's alpha, but the document for 2020.1 on unity website isn't updated for this yet.
     
  8. roka

    roka

    Joined:
    Sep 12, 2010
    Posts:
    598
    Can you post your solution. It will help a lot of people when they will use this version of Unity.
    Thank you
     
    Twyker_gp likes this.
  9. harrywenjie

    harrywenjie

    Joined:
    Jun 25, 2018
    Posts:
    12
    Code (JavaScript):
    1. var gameInstance = createUnityInstance(..........
    Code (JavaScript):
    1. gameInstance.then((unityInstance) => {
    2.             unityInstance.SendMessage("GameObject","method",value);        
    3.         });
     
  10. Twyker_gp

    Twyker_gp

    Joined:
    Dec 4, 2018
    Posts:
    29
    How do you now handle the new onProgress callback, @harrywenjie ? :x
    Before it was just { onProgress : UnityProgress }
     
  11. brunno159

    brunno159

    Joined:
    Oct 24, 2014
    Posts:
    23
    Code (CSharp):
    1. <!DOCTYPE html>
    2. <html lang="en-us">
    3.    <head>
    4.       <meta charset="utf-8">
    5.       <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    6.       <title>Unity WebGL Player | V Combined Meeting e Four Otology</title>
    7.       <link rel="shortcut icon" href="TemplateData/favicon.ico">
    8.       <link rel="stylesheet" href="TemplateData/style.css">
    9.    </head>
    10.    <body>
    11.       <div id="unity-container" class="unity-desktop">
    12.          <canvas id="unity-canvas"></canvas>
    13.          <div id="unity-loading-bar">
    14.             <div id="unity-logo"></div>
    15.             <div id="unity-progress-bar-empty">
    16.                <div id="unity-progress-bar-full"></div>
    17.             </div>
    18.          </div>
    19.          <div id="unity-footer">
    20.             <div id="unity-webgl-logo"></div>
    21.             <div id="unity-fullscreen-button"></div>
    22.             <div id="unity-build-title">V Combined Meeting e Four Otology</div>
    23.          </div>
    24.       </div>
    25.       <script>
    26.          var buildUrl = "Build";
    27.          var loaderUrl = buildUrl + "/build.loader.js";
    28.          var config = {
    29.            dataUrl: buildUrl + "/build.data.unityweb",
    30.            frameworkUrl: buildUrl + "/build.framework.js.unityweb",
    31.            codeUrl: buildUrl + "/build.wasm.unityweb",
    32.            streamingAssetsUrl: "StreamingAssets",
    33.            companyName: "Casa Mais",
    34.            productName: "V Combined Meeting e Four Otology",
    35.            productVersion: "1.0",
    36.          };
    37.        
    38.          var container = document.querySelector("#unity-container");
    39.          var canvas = document.querySelector("#unity-canvas");
    40.          var loadingBar = document.querySelector("#unity-loading-bar");
    41.          var progressBarFull = document.querySelector("#unity-progress-bar-full");
    42.          var fullscreenButton = document.querySelector("#unity-fullscreen-button");
    43.        
    44.          if (/iPhone|iPad|iPod|Android/i.test(navigator.userAgent)) {
    45.            container.className = "unity-mobile";
    46.            config.devicePixelRatio = 1;
    47.          } else {
    48.            canvas.style.width = "960px";
    49.            canvas.style.height = "600px";
    50.          }
    51.          loadingBar.style.display = "block";
    52.        
    53.          var script = document.createElement("script");
    54.          script.src = loaderUrl;
    55.          script.onload = () => {
    56.            window.gameInstance = createUnityInstance(canvas, config, (progress) => {
    57.              progressBarFull.style.width = 100 * progress + "%";
    58.            }).then((unityInstance) => {
    59.              loadingBar.style.display = "none";
    60.              fullscreenButton.onclick = () => {
    61.                unityInstance.SetFullscreen(1);
    62.              };
    63.            }).catch((message) => {
    64.              alert(message);
    65.            });
    66.          };
    67.          document.body.appendChild(script);
    68.       </script>
    69.       <script>
    70.          // Callback for the Unity OnClose event
    71.          window.onbeforeunload = function(e) {
    72.        
    73.          e.preventDefault();
    74.          // These are the messages you're seeing in the gif
    75.          console.log("Calling OnClose from Browser!");
    76.        
    77.        
    78.          gameInstance.then((unityInstance) => {
    79.          unityInstance.SendMessage("OnCloseObject","OnClose");       <<< this is line 79
    80.          });
    81.        
    82.          // This never shows up correctly for me, but it does prompt
    83.          // the player to close their window with a dialogue box
    84.          //
    85.          var dialogText = "You game has been saved!  Would you like to continue unloading the page?";
    86.          e.returnValue = dialogText;
    87.          return dialogText;
    88.          };  
    89.       </script>
    90.    </body>
    91. </html>
    Guys, heres my index.html of the webgl build. When i close the browser i want to have a function activated on unity side. Its giving me this error >>>


    Uncaught (in promise) TypeError: unityInstance is undefined
    onbeforeunload https://www.plataformareuni.com.br/v-combined-meeting-e-four-otology/:79

    Dunno whats wrong. Can anyone help?
     
  12. brunno159

    brunno159

    Joined:
    Oct 24, 2014
    Posts:
    23
  13. HexiledGames

    HexiledGames

    Joined:
    Aug 20, 2017
    Posts:
    2
    There is an easy solution.
    It's a matter of storing the unityInstance object in a window variable that you can latter use. The best moment to do that is right after the onload gets set, at the beginning of the "then". So:
    Code (CSharp):
    1. script.onload = () => {
    2.            window.gameInstance = createUnityInstance(canvas, config, (progress) => {
    3.              progressBarFull.style.width = 100 * progress + "%";
    4.            }).then((unityInstance) => {
    5.              window.gameInstance = unityInstance; //This is where you store the object
    6.              loadingBar.style.display = "none";
    7.              fullscreenButton.onclick = () => {
    8.                unityInstance.SetFullscreen(1);
    9.              };
    You can then send messages at a later time with
    gameInstance.SendMessage(GameObject_name, Method_name,value)

    You can even use this variable inside the .jslib plugins. So that events that respond to the changes in the browser may interact with objects inside the unityInstance object.
     
    abdulraheem_taha and tjerk_g like this.
  14. pws-devs

    pws-devs

    Joined:
    Feb 2, 2015
    Posts:
    63
    Hi

    I tried this solution for my application but the application never executed the ".then" part. Does it take longer to get it executed?