- Home /
Is it possible to send in custom objects defined in the Browser using SendMessage(), or are we limited to strings?
I want to send the custom object roomDetails to the Unity player. Is it allowed? If yes, is this the right syntax? If not is there any way to send this info without having to use a string and multiple delimiters,
In the browser
function someFunction(){
var roomDetails = new Object();
roomDetails.length = roomLength;
roomDetails.width = roomWidth;
roomDetails.roomID = roomID;
roomDetails.XPos = roomX;
roomDetails.ZPos = roomZ;
roomDetails.doors = doors;
roomDetails.from = facing;
constructor.SendMessage("Avatar", "buildRoom", roomDetails);
}
In Unity attached to Avatar
function buildRoom (roomDetails : Object)
{
}
Answer by Bunny83 · May 16, 2011 at 01:58 PM
As far as i know you can only pass one simple datatype like: int, float, String, ...
. The problem is that the class you've created "on-the-fly" is not known in .Net/Mono.
One way would be to use a JSON parser in JScript to serialize the object as string, but you have to deserialize and interpret it yourself in Unity.
I would go for the easiest way on the Unity side. A simple character-seperated-string will do in most cases. If you use an array in JScript you can simply use .Join
to form a single string and in Unity String.Split
Hmm, is there no way to define a class so it is available in both Unity and the browser?
The browser's JScript and Unity's $$anonymous$$ono implementation are two seperated worlds so there is no direct way. You can create the class in both environments and give them both the ability to be serialized to or deserialized from a string. If you just want to transfer some simple data i would recommend to use some easy-to-use method like xml, CSV, .... I don't understand why you need this data in your website? It's better to handle such stuff completely in Unity. Also be careful, website JScript is executed on the client side and can be manipulated or observed very easily.
Answer by CHPedersen · May 16, 2011 at 10:47 AM
Edit: Misunderstood question and deleted answer, my apologies. >_<