- Home /
How to send multiple values from browser to unity webgl game?
I’ve seen in the docs that I can communicate from my website with the unity app by sending messages:
SendMessage('MyGameObject', 'MyFunction', 'MyValue');
but how can I send multiple values down to the function of that object using only one SendMessage? Maybe that function needs several values… Or can I pass some object and parse it with C#?
Thank you
Answer by chitzui · Feb 12, 2018 at 05:46 PM
So as it does not seem to be possible to send anything else than numbers and strings. The solution I came up with for now is sending a string with JS and then parsing it inside C#:
In Javascript Browser Code:
gameInstance.SendMessage(
'Cylinder', 'HandleRotationString', '10|0|30'
);
In C# Unity Code:
void HandleRotationString(string coordinates) {
string[] coord = coordinates.Split('|');
float x = float.Parse(coord[0]);
float y = float.Parse(coord[1]);
float z = float.Parse(coord[2]);
// ... use variables
}
I hope it helps someone.
You could use JSON for this purpose. That would do the parsing for you.
This is the best answer when you are dealing with more complex data like arrays and classes, I don't even know why I didn't think of this. Just parsing it to and from JSON with JsonUtility.
hello
but how to send if string is variable. I use
.....
.....
var buffer = _malloc(bufferSize);
stringToUTF8(returnStr, buffer, bufferSize);
unityInstance.Send$$anonymous$$essage
('XYZTPanelOfProjects','UploadingObjectJavascript',buffer);
return buffer;
For 'Send$$anonymous$$essage' i get error For 'return buffer' i get valid data
What is wrong, some help I appreciate for me
Thanks
JCD
Your answer
Follow this Question
Related Questions
WebGL build callback on tab close or browser quit. 1 Answer
Is there a way to send an XMLHttpRequest and get the response in a WWW object? 0 Answers
How to send a large string from Javascript to WebGL ? 0 Answers
Interact across browser javascipt and unity webgl 2 Answers
Webgl browser error " DirectoryNotFoundException: Could not find a part of the path" 0 Answers