Unity → Browser works fine
Browser → Unity isn’t working.
In my .html file:
<script type="text/javascript">
function CookiePrefs_init(name, callback) {
var unity=u.getUnity();
if ( unity ) {
unity.SendMessage( name, callback, document.cookie);
}
else {
alert('Could not find Unity. Preferences will not be loaded');
}
}
</script>
In CookiePrefs.cs
#if UNITY_WEBPLAYER
/// <summary>
/// Loads the preferences from the cookie.
/// </summary>
public void LoadPreferences()
{
Application.ExternalCall("CookiePrefs_init", transform.name, "_ReadCookiesCallback");
}
/// <summary>
/// Callback for javascript to send the cookie to
/// </summary>
/// <param name="cookie_string">The cookie</param>
public void _ReadCookiesCallback(string cookie_string)
{
if (cookieContents == null)
cookieContents = new Dictionary<string,string>();
string[] split = cookie_string.Split(';');
foreach(string element in split )
{
string[] key_value = element.Split('=');
if(key_value.Length == 2 && !key_value[0].StartsWith("_utm"))
cookieContents.Add(key_value[0].Trim(), key_value[1]);
}
loaded=true;
}
#endif // UNITY_WEBPLAYER
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.