In my project I need the user to specify his own music file (mp3, or ogg).
(and unity WebGL needs to play it as AudioClip :))
It is possible at all ?
For now, I’v created *.jslib, and some java script function there, with I call from my gui, this is ok, function is called,
then to speed over the iteration (to not be forced to build package each time i change something in jslib, I’v created another javascript function in index.html and the only thing plugin does, is calling this function)
now in index.html I have something like this:
(just after )
then in script section
function BrowseForMusicFile()
{
//window.alert("HitMe !");
var chooser = document.querySelector('#fileDialog');
chooser.addEventListener("change", function(evt) {
console.log(this.value);
}, false);
chooser.click();
}
but the file browse dialog does not show up when I click into button in my unity WebGL …
(BrowseForMusicFile() is called, if I uncomment alert, it shows up)
Any idea why ?
Also anyone have any succes with using FileReader.readAsBinaryString to actually read the file from user disk
and pass it into WebGL player ? (that is my plan for the future, if the dialog eventually shows up :)))
After googling a little more: You can programmatically click the input element(with type=file) using jquery, javascript but only if you do it in an event handler belonging to an event THAT WAS STARTED BY THE USER! So, for example if you(the script) programmatically click the button in an ajax callback nothing will happen, but if you put the same line of code in an event handler that was raised by the user, it will work.
The problem here will be the same we have for doing fullscreen or mouselocking in Unity buttons: The browser restricts these features to be triggered by “user events” for security reasons, but clicks in Unity buttons don’t count as “user events” because Unity defers input processing to it’s main rendering loop, at which point the browser won’t see the connection to the user event.
The workaround typically used for this:
-Write your code in Unity UI to register the action on the mouseDown event (and not mouseUp as normal) on the button.
-Then, register a JavaScript event handler which intercepts the mouse Up event, and triggers the action (bring up the file dialog), when the mouse button is released.