Search Unity

web player communication with browser

Discussion in 'Scripting' started by draknir_, Sep 1, 2009.

  1. draknir_

    draknir_

    Joined:
    Sep 1, 2009
    Posts:
    30
    Hi community,

    I'm new to Unity and I need to get this working quickly, so unfortunately I haven't had the time to go through many tutorials or examples. I'd really appreciate your help on this problem!

    I'm trying to implement the functionality mentioned here: http://unity3d.com/support/documentation/Manual/Unity Web Player and browser communication.html
    to get a message sent from the browser to Unity3D. Unfortunately it doesn't work for me.

    I have done the following:
    - Added a new empty game object to the islands.unity project, named CommObject
    - Added a new C# script called commtest.cs
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class commtest: MonoBehaviour {
    6.  
    7.     // Use this for initialization
    8.     void Start()
    9.     {
    10.    
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update()
    15.     {
    16.    
    17.     }
    18.    
    19.     void MyFunction(string param)
    20.     {
    21.         Debug.Log(param);
    22.     }
    23. }
    24.  
    - Dragged the script onto the game object (this is the only way to 'attach' a script I could find).
    - Added the suggested piece of code to my browser page containing the unity web player script. It looks like this:
    Code (csharp):
    1.  
    2.         <script type="text/javascript" language="javascript">
    3.  
    4.         function SaySomethingToUnity()
    5.         {
    6.             GetUnity().SendMessage("CommObject", "MyFunction", "Hello from a web page!");
    7.         }
    8.  
    9.         </script>
    10.  
    Running the web player works fine, but I'm not getting the web page messages in the debug log :(

    Are there any ideas or suggestions as to what I'm doing wrong?

    thanks,
    draknir
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    You've defined the JS function in the web page, but you don't appear to call this function anywhere. Create a link in the HTML body somewhere with a call to the function in the URL:-
    Code (csharp):
    1.  
    2. [url='javascript:SaySomethingToUnity();']Talk to Unity[/url]
    3.  
    When you click this link, you should be talking to Unity. However, you won't see anything if you use Debug.Log, since this only works in the editor. Try playing a sound in the test function to see if Unity is responding.
     
  3. draknir_

    draknir_

    Joined:
    Sep 1, 2009
    Posts:
    30
    Hi andeeee,

    Thanks for your quick reply! I did actually try calling the JS function from somewhere (should have mentioned that) but it had no effect.

    The link you provided works though! I'm accessing the web player debug logs in Documents and Settings\user\Local Settings\Temp\UnityWebPlayer\log. The log shows me this error whenever the function is called from your link:
    Code (csharp):
    1. SendMessage: object CommObject not found!
    Any ideas?

    cheers,
    draknir
     
  4. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    I think maybe the script attached to the GameObject should also be called CommObject. Beyond that, the only thing I can think of is the object hierarchy - is your GameObject a child of another object, perhaps?
     
  5. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    No maybes about it, that's spot on. The browser-JS function calls SendMessage and provides three bits of information:

    - The name of the game object the message should go to

    - The name of the function that will be found in one of the components attached to that game object that you'll call

    - The "data" or "message" you want to send

    So the game object with your script needs to be called CommObject, and that's precisely what the cited error is indicating as the problem. :)
     
  6. draknir_

    draknir_

    Joined:
    Sep 1, 2009
    Posts:
    30
    @andeeee: I renamed the script to CommObject and tried again, but gives me the same error. I don't think it's a child of another object... Here's the hierarchy:



    @HiggyB: I am using an object called CommObject, that's why I'm confused about this error :(



    edit:

    I have tried doing the opposite, sending a message from the web player to the browser using Application.ExternalCall, and this works fine, but I still have no luck with CommObject. I tried calling the object from the browser using '/' to indicate the root path, but it didn't change anything.
     
  7. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    your problem might be that you called the component CommObject too, against what you tell above (commtest)
     
  8. draknir_

    draknir_

    Joined:
    Sep 1, 2009
    Posts:
    30
    It was first called commtest.cs, now I have changed it to CommObject.cs as suggested by andeeee.

    edit: OK, I solved it: I'm an idiot! I was using a custom scene called island2.unity. While using build/run it was grabbing island.unity - quickly rectified by a visit to build settings >_>. Sorry for taking up your time folks.