Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by 1014893573 · Sep 18, 2016 at 01:59 PM · webglsendmessage

how to use sendmessage to call a unity function from html

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Alvaro_Spain · Oct 13, 2016 at 03:41 PM

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">&laquo; created with <a href="http://unity3d.com/" title="Go to unity3d.com">Unity</a> &raquo;</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 += "<br>";
     log.scrollTop = log.scrollHeight;
     */
 }        
         
     </script>
     
   </body>
 </html>
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Protecting WebGL Unity application from SendMessage exploitation 0 Answers

How to send multiple values from browser to unity webgl game? 1 Answer

How can I send russian symbols from JS to Unity WebGL 0 Answers

Connection slower with higher update rate using LLAPI. webgl 0 Answers

Unity WebGL UnityInstance.SendMessage Error: SendMessage: object Object1 does not have receiver for function moveObject 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges