From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(For some reason Unity JS doesn't like private in front of function)
(Compatibility with Unity 1.6.1+)
Line 25: Line 25:
     "function CookieCutter_init(name, callback){" +  
     "function CookieCutter_init(name, callback){" +  
     "    GetUnity().SendMessage( name, callback, " +  
     "    document.getElementById('Unity').SendMessage( name, callback, " +  
     "            ''+document.cookie);" +  
     "            ''+document.cookie);" +  
     "}" +  
     "}" +  

Revision as of 23:58, 21 January 2007

Author: KeliHlodversson



This is a utility script for accessing the browser cookies from the web player


Place this script on a GameObject. Make sure the GameObject has a unique name.

After the game has started, cookies can be read by calling CookieCutter.GetCookie(name) and set using CookieCutter.SetCookie(name, value, days), where name is the name of the cookie key, value is the string value to be stored and days is the number of days until the browser will delete the cookie.


  • Due to the asynchronous way the Unity Plug-In communicates with the browser, the cookies are not available straight away. Before calling Get or SetCookie, inspect CookieCutter.loaded, which will be set to true when the data is available.
  • This script only works in the web player plug-in.
  • Safari requires the embedding html to be served from an http: url. When testing on a local file url, no cookies will be saved or returned. Either enable personal web sharing and open the player through http://localhost/... or use a browser that supports cookies on file:// urls (eg. Firefox)
  • Watch the size of the cookies. All cookies defined using this module will be sent to the web server serving the web player on every web request. This increases the amount of data sent to the server, but might also compromise privacy if the cookies contain sensitive data. Especially if you are hosting the player on a domain name shared by others. You can limit the scope of the cookies created by this module by setting the path variable to limit the cookie to a subdirectory on the web server. (Ie. if the web player is located at, set path to /myProjects/coolGame to ensure the cookies are not available to urls outside that directory on the web server.)

JavaScript - CookieCutter.js

<javascript>private static var _cookies : Hashtable; private static var _instance : CookieCutter; static var loaded = false; var path = "/"; // Change this to limit the scope of the cookies created

function Awake () {

 _cookies = new Hashtable(); 
 _instance = this;
 // Define helper functions 
   "function CookieCutter_init(name, callback){" + 
   "    document.getElementById('Unity').SendMessage( name, callback, " + 
   "            +document.cookie);" + 
   "}" + 
   "function  CookieCutter_set(name, value, days){" + 
   "  var expires=;" + 
   "  if (days) {" + 
   "    var date = new Date();" + 
   "    date.setTime(date.getTime()+ " + 
   "      (days*24*60*60*1000));" + 
   "    expires = '; expires='+date.toGMTString();" + 
   "  }" + 
   "  document.cookie = name+'='+value+expires+'; path="+path+"';" + 


// Called by the browser javascript during initialisation function _ReadCookiesCallback(cookie_string) {

 _cookies = new Hashtable(); 
 for(var element in cookie_string.Split([";"[0]]) ) { 
     var key_value = element.Split(["="[0]],2); 
     if(key_value.Length == 2)
         _cookies[key_value[0].Trim()] = key_value[1]; 


function ReadCookiesFromBrowser() {

  if(Application.platform == RuntimePlatform.WindowsWebPlayer || Application.platform == RuntimePlatform.OSXWebPlayer)
      Application.ExternalCall("CookieCutter_init", name, "_ReadCookiesCallback"); 
      _ReadCookiesCallback("test1=1; testCookie=NotWebPlayer;");


static function ReloadCookies(){



static function SetCookie(name, value, days) {

  Application.ExternalCall("CookieCutter_set", name, value, days); 

} static function SetCookie(name, value) {

  Application.ExternalCall("CookieCutter_set", name, value); 


static function GetCookie(name) {

  return _cookies[name]; 



Example - CookieTest.js

<javascript> @script RequireComponent(GUIText)

var cookieValue : String;

// Wait until cookies are loaded while(!CookieCutter.loaded)

yield WaitForSeconds(0.1);

// Read testCookie cookieValue=CookieCutter.GetCookie("testCookie");

// SetDefault if(!cookieValue)


else // Else modify value


// Display the cookie value guiText.text=cookieValue;

// And save it in the browser CookieCutter.SetCookie("testCookie",cookieValue,100); </javascript>

Personal tools