honey_cake
honey_cake

Reputation: 103

Calling a Unity WebGl Function from JavaScript (Vue)

I have a VueJs Project right now, where I'm building a web page. In order to show 3D Content and let the User work with it, I wanted to embed a Unity Project as WebGL. I´m not a professional programmer, my knowledge is more selfe-taught from what I can find at the Internet.

First of all I created a plugin, as it is explained here. My .jslib file looks like that:

var UnityJavascipt = {

 $JustAWebGLObject:
  {
  },
  SendToJavscript: function ()
  {
              console.log("worked!")
  }     
}; autoAddDeps(UnityJavascipt , '$JustAWebGLObject'); mergeInto(LibraryManager.library, UnityJavascipt );

I embed my Unity WebGl like that in Vue:

<template>
  <unity src="static/Build/game.json" width="1000" height="600" unityLoader="static/Build/UnityLoader.js" ref="myInstance"></unity>  
</template>

I can also call "SendToJavaScript" from my C# Script. But now it starts to get unclear for me, since I want to be able to call my "SendToJavaScript" Function (or any other one that will be added later) from a outside JavaScript. In the manual they refer to the emscripten pagefor more detailed information. But for me that all is just very confusing. I already tried to call my function like that in VueJs:

_SendToJavaScript()

But I always get a ReferenceError: _SendToJavaScript is not defined I also read in the forum here, that it would be possible to call the function like that:

UnityInstance.Module.asmLibraryArg._SendToJavaScript()

UnityInstance is the Variable from my index.html file, where UnityLoader get's instantiated. But again I'm getting a Reference Error.

It would be very helpful if someone could explain what I'm doing wrong... Communication between WebGL and Vue are essential for my project but I´m stuck here for days. So thanks in advance!!

Upvotes: 0

Views: 2491

Answers (1)

DoctorPangloss
DoctorPangloss

Reputation: 3073

Use unityInstance.SendMessage() documented here.

You can pass a callback or call a .jslib defined function at the end of your C# method.

Upvotes: 0

Related Questions