From Unify Community Wiki
Jump to: navigation, search

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

import UnityEngine.Application; // Imports ExternalEval(), ExternalCall() and platform into the current namespace
import UnityEngine.RuntimePlatform; // Imports WindowsWebPlayer and OSXWebPlayer
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){" + 
    "    var unity=GetUnity();" + // Old Unity versions (before 2.0) should have this line changed to: "var unity=document.Unity;"
    "    if ( unity ) { " +
    "      unity.SendMessage( name, callback, " + 
    "            ''+document.cookie);" + 
    "    } " +
    "    else { " +
    "      alert('Could not find Unity plugin. Cookies will not be loaded'); " +
    "    } " +
    "}" + 
    "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.toUTCString();" + 
    "  }" + 
    "  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(platform == WindowsWebPlayer || platform == OSXWebPlayer)
       ExternalCall("CookieCutter_init", name, "_ReadCookiesCallback"); 
       _ReadCookiesCallback("test1=1; testCookie=NotWebPlayer;");
static function ReloadCookies(){
static function SetCookie(name, value, days) { 
   ExternalCall("CookieCutter_set", name, value, days); 
static function SetCookie(name, value) { 
   ExternalCall("CookieCutter_set", name, value); 
static function GetCookie(name) { 
   return _cookies[name]; 

Example - CookieTest.js

@script RequireComponent(GUIText)
var cookieValue : String;
// Wait until cookies are loaded
 yield WaitForSeconds(0.1);
// Read testCookie
// SetDefault
else // Else modify value
// Display the cookie value
// And save it in the browser
Personal tools