- Home /
Easily getting variable values from WebGL in Browser
I'm developing some 3D interactions in Unity, and they will be embedded in a webpage which will have other interactive components. I'm going to need to communicate between these other components, and I'm wondering if there's an easy way to return a value from Unity into JS variables in the browser.
I know I can use SendMessage() to call functions in Unity from external JS, but can I return values from these calls?
I don't know how to efficiently do this, so the solution I'm thinking of right now looks like this:
Browser:
var health;
var exp;
function GetHealth(){
SendMessage("Player", "GetHealth");
}
function ReturnValue(varName, val){
if(varName == "health"){
health = val;
}
if(varName == "exp"){
exp = val;
}
//etc.
}
Unity:
public void GetHealth(){
Application.ExternalCall("ReturnValue", "health", health);
}
This is a really ugly solution that would be ripe with problems. Ideally, I'd like to do something like
Browser:
function GetHealth(){
return SendMessage("Player", "GetHealth");
}
Unity:
public int GetHealth(){
return health;
}
Any ideas?
Answer by elenzil · Nov 01, 2017 at 04:53 PM
this thread in the forums seems pretty relevant.
edit actually it provides a demo of the requested functionality, plus an approach w/ significantly lower runtime overhead than SendMessage, altho it's only significant if you're calling into C# several thousands of times per frame, imo.
Answer by Bunny83 · Oct 31, 2017 at 11:50 PM
In the past we had methods like Application.ExternalEval or Application.ExternalCall. However it seems they got deprecated. Instead you should use a "jslib" plugin and use external method definitions in C#. According to the "AddNumbers" example you should be able to return values.
Hi Bunny - thanks for your answer here, i always read your answers carefully since they're pretty knowledgable.
yes, the jslib plugin approach works well for calling JavaScript from C# and passing values back to C#. I've also confirmed that you can call javascript functions hosted in the main page from within the plugin code, as well as access elements in the DO$$anonymous$$ from within the plugin code.
this question is actually about the reverse tho: calling C# from javascript and getting a return value. Send$$anonymous$$essage() works well for sending a string or number (or nothing) to Unity, but it has no provision for synchronously returning a value.
i'm hoping to look into this a bit more. there might be some way to use marshaling to pass a C# callback function to JS and then have JS call the callback. i have no evidence this is actually possible, but it seems like the only open avenue of approach, given the APIs at hand.
any thoughts greatly appreciated.
@elenzil I know this is old but I also need to retrieve a variable from Unity using javascript in the browser, were you able to achieve this?
check out the thread linked to in the other answer here. "this thread in the forums seems pretty relevant."
Your answer
Follow this Question
Related Questions
EntryPointNotFoundException when trying webgl browser communication jslib example 2 Answers
How to send multiple values from browser to unity webgl game? 1 Answer
Interact across browser javascipt and unity webgl 2 Answers
Check if Javascript function exists on webpage before calling ExternalCall 1 Answer