Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Plugin communication

Discussion in 'Editor & General Support' started by vatche, Jul 15, 2006.

  1. vatche

    vatche

    Joined:
    Jun 8, 2006
    Posts:
    5
    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:

    1) 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.

    2) 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 <param> 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.

    Thanks,
    vatche
     
  2. hsparra

    hsparra

    Joined:
    Jul 12, 2005
    Posts:
    750
    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.
     
  3. vatche

    vatche

    Joined:
    Jun 8, 2006
    Posts:
    5
    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 <embed> 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?

    Thanks again,
    vatche
     
  4. hsparra

    hsparra

    Joined:
    Jul 12, 2005
    Posts:
    750
    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:
    Code (csharp):
    1.  
    2. // My key object type = MyKeyObject
    3. var myKey : String; // Or float, etc.
    4.  
    5. function Start() {
    6.    DontDestroyOnLoad(this);
    7.    // Some code to create your key.
    8.    // can use random + some other stuff, the sky is the limit.
    9. }
    10.  
    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.
    Code (csharp):
    1.  
    2.  
    3. var theKeyObject : MyKeyObject;
    4. var yourBaseUrl = "http://www.yourUrl.com/ourpage?"
    5. fuction OnStart() {
    6.   theKeyObject = GameObject.Find("MyKeyObject");
    7. }
    8.  
    9. function SendMessage() {
    10.   var url : string = yourBaseUrl + "key=" + theKeyObject.myKey + "&date=" + whateverYouWantToPass;
    11.  
    12.   results = WWW(url);
    13.   yield results;
    14.  
    15.   // Look at and process the results
    16. }
    17.  
    Or somethink like that. I do not have Unity in front of my right now so I cannot check the exact syntax.
     
  5. vatche

    vatche

    Joined:
    Jun 8, 2006
    Posts:
    5
    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:

    Code (csharp):
    1.  
    2. <html>
    3. <head><title>Unity Web Player</title></head><body>
    4.  
    5. [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.]
    6.  
    7. <noscript><object classid= etceteta...
    8. <param name="src" value="portal.unityweb" />
    9. <embed src="portal.unityweb" width= etcetera....
    10. </object></noscript>
    11. </body>
    12.  
    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 <param user="somekey"> 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.

    Thanks,
    vatche
     
  6. hsparra

    hsparra

    Joined:
    Jul 12, 2005
    Posts:
    750
    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 :)
     
  7. vatche

    vatche

    Joined:
    Jun 8, 2006
    Posts:
    5
    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 <param> implementation in the plugin ( :wink: to the unity development team).

    Thanks,
    vatche

     
  8. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Container page - player communication will be part of Unity 1.6.