[Features]/Question About RegisterJsMethod #349
Replies: 2 comments
-
I don't really understand what you are asking sorry.
using UnityEngine;
using VoltstroStudios.UnityWebBrowser;
public class JSTest : MonoBehaviour
{
public WebBrowserUIBasic webBrowser;
public void Start()
{
webBrowser.browserClient.RegisterJsMethod<string>("UwbSend", UwbSend);
}
private void UwbSend(string value)
{
Debug.Log($"Got value of {value} from JS... send another response.");
string responseValue = "Test Value Back";
webBrowser.browserClient.ExecuteJs($"uwbReturn('{responseValue}')");
}
} // Invoked by something (like a button on a page)
function uwbSend() {
uwb.ExecuteJsMethod('UwbSend', 'Test Value');
}
// Invoked by UWB's ExecuteJs
function uwbReturn(value) {
console.log(value);
} |
Beta Was this translation helpful? Give feedback.
-
Ok thank you , thats basically what my question was and the conclusion that i came function executeCommand(command, argument) {
const subscribers = [];
const cindex = this.window.dotnetComLayer.returnData.length;
// Execute the command'
if(!argument)
uwb.ExecuteJsMethod(command);
else
uwb.ExecuteJsMethod(command, argument);
const intervalId = setInterval(() => {
if (this.window.dotnetComLayer.returnData.length > cindex) {
// Notify all subscribers
const newData = this.window.dotnetComLayer.returnData[cindex];
subscribers.forEach(callback => callback(newData));
// Optionally clear the interval if you expect only one update
clearInterval(intervalId);
}else{
console.log('not found');
}
}, 50);
// Function to subscribe to data changes
return {
subscribe(callback) {
subscribers.push(callback);
// Return an unsubscribe function
return () => {
const index = subscribers.indexOf(callback);
if (index !== -1) {
subscribers.splice(index, 1);
}
};
}
};
} and in the html file where i need this info
|
Beta Was this translation helpful? Give feedback.
-
What platform are you experiencing this issue on?
Windows x64
What version of UWB are you using?
2.2.1
What Unity version are you running?
2022.3.48f1
Describe what the issue you are experiencing is.
Hello, I read over the wiki for javascript , found this method RegisterJsMethod
however i noticed that this method calls jsMethodManager.RegisterJsMethod
when supplying a function that returns anything but void
I am unable to pull any data from unity into the browser using methods like this.
I have in the meantime used a solution that calls ExecuteJs to add the data to a list that is polled periodically in js with a Observable to achieve the results i need.
client.ExecuteJs($@"window.dotnetComLayer.returnData.push(JSON.parse('{jsonData}'))");
My question is , will this method (RegisterJsMethod) in the future accept functions with return values ?
or is there something i have missed maybe another function somewhere?
Provide reproducible steps for this issue.
Any additional info you like to provide?
No response
Beta Was this translation helpful? Give feedback.
All reactions