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

Javascipt plugin code stopped working on upgrade from 5.4 to 5.6

Discussion in 'WebGL' started by nsmith1024, May 2, 2017.

  1. nsmith1024

    nsmith1024

    Joined:
    Mar 18, 2014
    Posts:
    869
    Hello,

    I had everything working fine in Unity 5.4, i upgraded to 5.6.0.f3 then ALL my javascript plug in code stopped working.

    When an old style GUI button is pressed in the OnGUI function it calls the plugin function below
    ImageUploaderCaptureClick, which should pop up a file dialog box allowing the user to select a photo. That was working before in 5.4.

    But in 5.6 when the button is clicked there is an error like this:

    Invoking error handler due to
    Uncaught TypeError: Cannot read property 'addEventListener' of null

    Seems the code below

    document.getElementById('canvas').addEventListener('click', OpenFileDialog, false);

    the 'canvas' element is missing, resulting in getElementById= null, then of course there is no addEventListener function from null.

    anybody knows what happened to the 'canvas' that was there in 5.4 that is now mssing in 5.6?

    All my javascript plugin code is using the canvas which is now missing in 5.6.


    Here is sample code:

    FILE=MyPlugin,jslib

    Code (JavaScript):
    1.  
    2. var MyPlugin = {
    3.  
    4.   ImageUploaderCaptureClick: function() {
    5.     if (!document.getElementById('ImageUploaderInput')) {
    6.       var fileInput = document.createElement('input');
    7.       fileInput.setAttribute('type', 'file');
    8.       fileInput.setAttribute('id', 'ImageUploaderInput');
    9.       fileInput.style.visibility = 'hidden';
    10.       fileInput.onclick = function (event) {
    11.         this.value = null;
    12.       };
    13.       fileInput.onchange = function (event) {
    14.         SendMessage('LocalLoginUpload', 'FileSelected', URL.createObjectURL(event.target.files[0]));
    15.       }
    16.       document.body.appendChild(fileInput);
    17.     }
    18.     var OpenFileDialog = function() {
    19.       document.getElementById('ImageUploaderInput').click();
    20.       document.getElementById('canvas').removeEventListener('click', OpenFileDialog);
    21.     };
    22.     document.getElementById('canvas').addEventListener('click', OpenFileDialog, false);
    23.   }
    24. };
    25.  
    26. mergeInto(LibraryManager.library, MyPlugin);
    27.  
    28.  
    29.  
    30.  
    I tried adding a canvas element in javascript near the top of the function i added this

    Code (JavaScript):
    1.  
    2.     var c=document.createElement('canvas');
    3.     c.id='canvas';
    4.    document.body.appendChild(c);
    5.  
    generates no errors, but it doesnt work, the onclick never fires which is what i use to trigger the open file dialog box to be displayed so that the user can select a picture from his computer to be used for his profile.

    I have a lot of similar code which no longer works either, all of it worked in 5.4 now it doesnt work in 5.6. I spent so many hours to get it to work, now just by upgrading it doesnt work.

    So now i cant do anything! Im stuck!

    Anybody has ideas?

    Thanks!
     
    Last edited: May 2, 2017
  2. Laur3nt1u

    Laur3nt1u

    Joined:
    Apr 30, 2014
    Posts:
    23