The official doc says use SendMessage
SendMessage (‘MyGameObject’, ‘MyFunction’, ‘foobar’);
But after I built a webgl and add SendMessage to the released index.html, I got “SendMessage is undefined”. And I add this
var u=GetUnity();
u.SendMessage (‘MyGameObject’, ‘MyFunction’, ‘foobar’);
then I got GetUnity is undefined
How can I solve this problem
Just in case it helps you, I am using a template that works for me without problems, and I use SendMessage from index.html to Unity. I don’t need to use GetUnity(), just the bare SendMessage works for me on Chrome without problems. This code is used to get keys pressed from a MIDI keyboard and send them to the main program made in Unity in C#.
This is the complete index.html file that I am using at this moment, I hope that you can read it and use the information contained inside to solve your problem:
<!doctype html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Unity WebGL Player | %UNITY_WEB_NAME%</title>
<link rel="stylesheet" href="TemplateData/style.css">
<link rel="shortcut icon" href="TemplateData/favicon.ico" />
<script src="TemplateData/UnityProgress.js"></script>
</head>
<body class="template">
<p class="header"><span>Unity WebGL Player | </span>%UNITY_WEB_NAME%</p>
<div class="template-wrap clear">
<canvas class="emscripten" id="canvas" oncontextmenu="event.preventDefault()" height="%UNITY_HEIGHT%px" width="%UNITY_WIDTH%px"></canvas>
<div class="logo"></div>
<div class="fullscreen"><img src="TemplateData/fullscreen.png" width="38" height="38" alt="Fullscreen" title="Fullscreen" onclick="SetFullscreen(1);" /></div>
<div class="title">%UNITY_WEB_NAME%</div>
</div>
<p class="footer">« created with <a href="http://unity3d.com/" title="Go to unity3d.com">Unity</a> »</p>
%UNITY_WEBGL_LOADER_GLUE%
<script>
//Bare minimum JS code to read midi input
//Adapted from https://github.com/cwilso/WebMIDIAPIShim
var midi;
var log = document.getElementById("midi-log");
//BRO_init();
function BRO_init() {
logText("Inicializando MIDI...");
navigator.requestMIDIAccess().then( onSuccess, onFailure ); //get midi access
}
function onSuccess( access ) {
midi = access;
var inputs = midi.inputs;
logText("Encontrados " + inputs.size + " MIDI input(s)");
//connect to first device found
if(inputs.size > 0) {
var iterator = inputs.values(); // returns an iterator that loops over all inputs
var input = iterator.next().value; // get the first input
logText("Primer input MIDI encontrado: " + input.name);
input.onmidimessage = handleMIDIMessage;
}
}
function onFailure( err ) {
logText("MIDI Init Error. Error code: " + err.code);
}
function handleMIDIMessage(event){
//event.data & event.receivedTime are populated
//event.data has 3 components:
//0) The device id
//1) The controller id
//2) The controller value (typically in the range 0 - 127)
// mostramos sólo las pulsaciones y soltadas de tecla (códigos MIDI 144 y 128)
if (event.data.length === 3 && (event.data[0] == 144 || event.data[0] == 128)) {
var onOff = event.data[0];
var nota = event.data [1];
var volumen = event.data[2];
if (onOff == 128)
volumen = 0;
//logText('mandato: ' + onOff + ', nota: ' + nota + ', volumen: ' + volumen);
if (onOff == 144) {
SendMessage("InputTecladoWebGL", "TeclaPulsadaDesdeNavegador", nota);
}
else if (onOff == 128) {
SendMessage("InputTecladoWebGL", "TeclaLevantadaDesdeNavegador", nota);
}
}
}
function logText(str){
/*
log.innerHTML += str;
log.innerHTML += "
";
log.scrollTop = log.scrollHeight;
*/
}
</script>
</body>
</html>