Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Passing a variable from JS into Unity Player on page load?

Discussion in 'Scripting' started by gateway69, May 18, 2010.

  1. gateway69

    gateway69

    Joined:
    Feb 18, 2010
    Posts:
    94
    So I have been trying to figure out how to send a variable into the Unity Player as the page loads.

    I have looked at this http://unity3d.com/support/documentation/Manual/Unity Web Player and browser communication.html and while I tried to copy the examples im unable to send data into the unity player as far as I can tell.

    I put this piece of JS script on the web page

    Code (csharp):
    1. <script type="text/javascript" language="javascript">
    2. <!--
    3. function assetpath()
    4. {
    5.     document.getElementById("UnityContent").SendMessage("GameObject", "WebAsset", "<?php print $node->field_assunity[0]['filepath'];?>");
    6. }
    7. -->
    8. </script>
    it should send a path something like /blah/blah/sometest into unity..

    I have a string var set up in Unity

    here is a bit of the unity code..

    Code (csharp):
    1.  
    2. var test : String;
    3.  
    4. function WebAsset(param : String)
    5. {
    6.  
    7. test = param;
    8.  
    9.  
    10. }
    11.  
    12. function OnGUI() {
    13.  
    14. GUI.Label( Rect(10,10,100,100),test);
    15. Debug.Log("test");
    16.  
    17. }
    18.  
    19.  
    I have a empty game object in the scene called GameObject but each time i load the page with the player and the above code compiled, im not seeing my path data being displayed in the GUI.Label

    thoughts?

    btw first time trying to send data from the web into Unity and do something with it :p
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    is the js function on the website ever called?
     
  3. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    And if it is called, when is it called? If you call it immediately upon page load then that might be your problem. Please remember that the page will load, and JavaScript is able to execute, while your web player data file is still downloading (the user seeing the progress bar) and as such your call might "fall on deaf ears". So you must be sure the content is fully loaded and ready before you can send anything into it via JavaScript. As such, you should either (a) have your content ping the page to let it know it's loaded and ready, or (b) have your content load and ping for the needed data itself. In either of those cases know that ExternalCall can be used within Unity to communicate outbound.


    Also, you're manually getting the content by its ID, but often there are slight tweaks in the ID used by the object tag (used in Windows IE) and the embed tag (used elsewhere). You might try using the default GetUnity() function we provide in our HTML template, or at least ensure that the ID you're using in that call matches the ID used by the content in question, otherwise you can call all day and nobody will answer. :p


    Let us know if you have any other questions on that!
     
  4. wmaass

    wmaass

    Joined:
    May 24, 2010
    Posts:
    10
    HiggyB,

    When looking at the documentation and your post, what does "UnityContent" or "content" refer to?
     
  5. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    Your piece of Unity web player content (your game, the thing you built from Unity). As I noted above, you should use our built-in GetUnity() function to get a reference to it as you can, and should have a slightly different name in use for the object tag item (Windows IE) and the embed tag item (all other browsers).

    If you continue to have trouble then please share your entire HTML page (attach it to a post for example) so we can properly assist here, thanks!