Value in my blocks cannot be changed (Unity)

107 views
Skip to first unread message

Garry Gorman

unread,
Aug 29, 2022, 10:43:49 AM8/29/22
to Blockly
Hi All,

I am developing a game where blockly controls a gameObject in unity webgl. When I test the index.html file on its own, I can change the values in the blocks no problem. But when I run a build and run, the values in the blocks cannot be changed. It looks like there is a conflict between unity and the text input element of my blocks. Has anyone solved this issue before?

Any help would be very much appreciated.

Thank you

Garry
Wont change.JPG
Changing.JPG

Maribeth Bottorff

unread,
Aug 29, 2022, 7:43:47 PM8/29/22
to Blockly
Hello, are there any errors in the console when the value can't be changed? Or can you provide any other context that might be able to help debug, like what is the difference between building and running vs opening the page in html? We will need more details in order to assist you since this seems like something particular to your build setup.

Maribeth

Garry Gorman

unread,
Aug 30, 2022, 4:36:12 AM8/30/22
to Blockly
Thank you again for your help Maribeth,

I have attached 2 images of what is happening in the console. The image below labeled context 1 (no unity, index.html on its own) shows the output to the console for a simple "move forward" block in a "repeat 3 times" block with all the code output to the console from my scrub_ function. The image labeled context 2 show the output to the console from a build and run in unity. The only difference is the "V4.framework.js.gz:2 0" getting called every frame (about 50 times per second) I think. When I run the code it works but I just cant change the values in the blocks.
I can select the text in the text area and delete it (as shown in images context 3 and 4) and when I press tab while the text area is empty, the text "unnamed" appears in the text area.
I currently have 4 blocks that require text input and they are all behaving the same way. 
I will keep investigating and if I can out what "V4.framework.js.gz:2 0" does I will post it here.

Thanks

Garry
context 3.JPG
context 2.JPG
context 1.JPG
context 4.JPG

Garry Gorman

unread,
Aug 30, 2022, 11:52:32 AM8/30/22
to Blockly
Hi again Maribeth,

Perhaps I should have added the structure of my project. I have created a webGL template for Unity containing the blockly library. To communicate with unity, the blocks generate code that push strings into an array called log, similar to glockly games maze. I have 2 custom blocks created that push their own string into the log array and the animator function sends the messages to the Unity game (see code below). This all works fine until I need to edit the value in a repeat block, text input block, function block or a number block. If you have any help on how I could address this issue I would greatly appreciate it.

Thank you

Garry

Blockly.JavaScript['move'] = function (block) {
    var dropdown_value = block.getFieldValue('VALUE') === "forward" ? 'log.push("FORWARD")' : 'log.push("BACKWARD")';
    // TODO: Assemble JavaScript into code variable.
    var code = dropdown_value + ';\n';
    return code;
};

let log = [];
// cycles through the array and gives instructions to unity
function animater() {
  var action = log.shift();
  if (action === "FORWARD")
  {
    unityGame.SendMessage("single boat", "StartCoroutine", "MoveForward");
  }
  if (action === "LEFT")
  {
    unityGame.SendMessage("single boat", "StartCoroutine", "TurnRight");
  }
  if (action === "RIGHT")
  {
    unityGame.SendMessage("single boat", "StartCoroutine", "TurnRight");
  }
  if (log.length) setTimeout(animater, 2200);
}

Garry Gorman

unread,
Sep 1, 2022, 6:13:46 AM9/1/22
to Blockly
I got this sorted.

Unity captures all keyboard inputs by default and does not share keyboard inputs wit the rest of the webpage. This must be disabled to allow blockly blocks with input fields to accept keyboard inputs. However, disabling the keyboard inputs limits the range of capabilities of the game as Unity will no longer be have access to keyboard inputs. Therefore, keyboard inputs must toggle between the Unity canvas and blockly as needed. To do this, add the following code to the start function in a c# script in your unity game.

#if !UNITY_EDITOR && UNITY_WEBGL 

     // disable WebGLInput.captureAllKeyboardInput so elements in web page can handle keyboard inputs 

      WebGLInput.captureAllKeyboardInput = false; 

#endif 


To allow focus to return to your unity game so that the player can control the player with the keyboard again, add the following script to your WebGL template index.html file after your Unity script.

<script>
    // function to allow unity to recapture focus while still sharing keyboard inputs with the rest of the webpage.
    var recaptureInputAndFocus = function () {
      var canvas = document.getElementById("unity-canvas");
      if (canvas) {
        canvas.setAttribute("tabindex", "1");
        canvas.focus();
      } else
        setTimeout(recaptureInputAndFocus, 100);
    }

    recaptureInputAndFocus();
  </script>

I hope this helps somebody as it fried my head for 3 days.

Sources

Thanks

Garry

Reply all
Reply to author
Forward
0 new messages