I am trying to mock up a communication channel between the plugin and the webpage it lives in. I know that this is a feature not yet available but I could manage with and build upon a basic framework. So, here’s the idea:
The webpage where the plugin lives communicates via an AJAX interface to the server through a PHP script which in turn accesses the shared data in a MySQL database.
Within the unity script code, I use WWW to communicate with the server through a PHP script to access the shared data.
All works well except for one problem: one naturally wants to allow more than one session active at a time. That can be achieved by tagging the shared data in the database with a randomly generated number, which in turn would be stored in a $_SESSION variable so that it’s available to both sides of the coin. However, the Unity plugin appears to reset the web session internally. So, all associations with the embedding webpage and the plugin appear to be lost. Is this a bug? any suggestion how to work around it?
Another idea would be to pass a random key to the plugin via a static assignment. But how do I access parameters passed to the plugin from within the Unity scripts? One may have expected that this data would be in the Application class, but I can’t locate it in the docs.
I know that a full communication protocol between the plugin and embedding page is due in 2.0, but a very basic static communication channel is all I need for now. The aim of doing all this is to allow lengthy text with links/images appear on the webpage alongside the plugin based on the content within the plugin. I know I can bring and display all the data into the plugin via WWW, but that would be a horrificly inefficient approach.
You could use the .Net classes to achieve what you want. However, how about you generate a key in Unity. Then pass that key as one of the arguments using the WWW class? That way the key can still be unique and not reset.
Thanks for the suggestion; but I don’t get it. If I pass an argument via WWW to the webserver, how would the webpage embedding the plugin know about it? From playing around with WWW, it appears to me that the web session initiated within the Unity plugin via WWW and the one initiated by the webpage embedding the plugin are totally independent…this is unusual; it seems to me it should be rather easy to fix the problem with a minor revision of the plugin allowing parameter attributes in the code. We know this already is there for width/height attributes; one just needs an additional “user” field for communication. Anyone at Unity with news when could this be coming?
You would not use the web session as your identifier. You would use a “key” created in Unity. the key could be created game “session”, per level, or at whatever granularity you wanted.
For example:
In one object create your key:
// My key object type = MyKeyObject
var myKey : String; // Or float, etc.
function Start() {
DontDestroyOnLoad(this);
// Some code to create your key.
// can use random + some other stuff, the sky is the limit.
}
When communicating, have a reference to the above object. However, grab the reference when creating your communication object not by drag and drop. If by drag and drop you will end up with multiple instances.
var theKeyObject : MyKeyObject;
var yourBaseUrl = "http://www.yourUrl.com/ourpage?"
fuction OnStart() {
theKeyObject = GameObject.Find("MyKeyObject");
}
function SendMessage() {
var url : string = yourBaseUrl + "key=" + theKeyObject.myKey + "&date=" + whateverYouWantToPass;
results = WWW(url);
yield results;
// Look at and process the results
}
Or somethink like that. I do not have Unity in front of my right now so I cannot check the exact syntax.
Thanks; I understand now your suggestion. I may not have explained the issue well in my previous post. So, let me try to sketch things:
In the PHP page:
<html>
<head><title>Unity Web Player</title></head><body>
[Lengthy AJAX Code to talk to server via XMLHttpRequest using a RANDOM KEY generated by the PHP code; webpage content depends on this random key which is also stored in the server's database.]
<noscript><object classid= etceteta...
<param name="src" value="portal.unityweb" />
<embed src="portal.unityweb" width= etcetera....
</object></noscript>
</body>
Now, in the Unity code, I use the WWW class as you have in your code snippet to talk to the server. But I need the random key in the embedding page above to make sure the web page’s content reflects what’s going on in the corresponding Unity game… My problem is cross-communicating between the PHP file and the plugin’s runtime environment.
A would do the job if it was around. If such a single static channel of communication existed, a great deal of complex realtime stuff may be done by using AJAX and that single key. Furthermore, my tests show that the $_SESSION environment is currently reset by the plugin on load and hence cannot be used for piping data.
Just thinking out loud here, but could you generate the key in the PHP page then retrieve it from Unity using another key, say IP address? Once the key is retrieved then used it for the rest of the communication.
hmmm, guess I better learn AJAX. I have been thinking about it so now would be as good a time as any
thanks a lot, that would do it! the IP address together with a date/time stamp should do the job. It would be a dirty trick, but then no one promised programming would be better than politics… I’ll use the IP address trick until we get the much needed implementation in the plugin ( to the unity development team).