Search Unity

Browser -> Unity communication

Discussion in 'Scripting' started by TwistOfFat3, Oct 20, 2014.

  1. TwistOfFat3

    TwistOfFat3

    Joined:
    Oct 10, 2014
    Posts:
    7
    Unity -> Browser works fine
    Browser -> Unity isn't working.

    In my .html file:
    Code (csharp):
    1.  
    2. <script type="text/javascript">
    3.     function CookiePrefs_init(name, callback) {
    4.         var unity=u.getUnity();
    5.         if ( unity ) {
    6.             unity.SendMessage( name, callback, document.cookie);
    7.         }
    8.         else {
    9.             alert('Could not find Unity. Preferences will not be loaded');
    10.         }
    11.     }
    12. </script>
    13.  
    In CookiePrefs.cs
    Code (csharp):
    1.  
    2. #if UNITY_WEBPLAYER
    3.     /// <summary>
    4.     /// Loads the preferences from the cookie.
    5.     /// </summary>
    6.     public void LoadPreferences()
    7.     {
    8.         Application.ExternalCall("CookiePrefs_init", transform.name, "_ReadCookiesCallback");
    9.     }
    10.  
    11.     /// <summary>
    12.     /// Callback for javascript to send the cookie to
    13.     /// </summary>
    14.     /// <param name="cookie_string">The cookie</param>
    15.     public void _ReadCookiesCallback(string cookie_string)
    16.     {
    17.        if (cookieContents == null)
    18.             cookieContents = new Dictionary<string,string>();
    19.  
    20.         string[] split  = cookie_string.Split(';');
    21.         foreach(string element in split )
    22.         {
    23.             string[] key_value = element.Split('=');
    24.             if(key_value.Length == 2 && !key_value[0].StartsWith("_utm"))
    25.                 cookieContents.Add(key_value[0].Trim(), key_value[1]);
    26.         }
    27.         loaded=true;
    28.     }
    29. #endif // UNITY_WEBPLAYER
    30.  
    LoadPreferences is called from Start, the script is attached to an object named CookiePrefs which is at the root of my scene.
    CookiePrefs_init gets called, u.getUnity() returns the correct result but SendMessage doesn't do anything.

    I can't find anything useful, the only examples I find are for older versions of Unity so I'm starting to wonder if this isn't just borked in the current version.
     
  2. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    According to:
    http://docs.unity3d.com/Manual/UnityWebPlayerandbrowsercommunication.html

    you may be missing some code to grab the actual unity object to send the message to...

    Code (CSharp):
    1.  
    2. <script type="text/javascript" language="javascript">
    3. <!--
    4. //initializing the WebPlayer
    5. var u = new UnityObject2();
    6. u.initPlugin(jQuery("#unityPlayer")[0], "Example.unity3d");
    7.  
    8. function SaySomethingToUnity()
    9. {
    10.     u.getUnity().SendMessage("MyObject", "MyFunction", "Hello from a web page!");
    11. }
    12. -->
    13. </script>