PlayerPrefsx

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Aded missing <javascript> tags around the code)
m (Changed while(loaded) to while(!loaded) in example)
Line 12: Line 12:
 
For best reliability, put the following code before you use PlayerPrefsx:
 
For best reliability, put the following code before you use PlayerPrefsx:
  
while (PlayerPrefsx.loaded) yield;
+
<javascript>while (!PlayerPrefsx.loaded) yield;</javascript>
  
 
It shouldn't take any significant amount of time to load the cookies - a frame at most, probably. (Non-Web player builds will be loaded instantaneously) In any case this will make sure PPX function are not called when they shouldn't be.
 
It shouldn't take any significant amount of time to load the cookies - a frame at most, probably. (Non-Web player builds will be loaded instantaneously) In any case this will make sure PPX function are not called when they shouldn't be.

Revision as of 13:58, 11 October 2007

Author: StarManta; built on CookieCutter by KeliHlodversson

As is, it works with Unity 2.0 Web Players. It has not been tested with 1.x. If it doesn't work, line 24 is probably the line that would need to be changed.

Contents

Description

CookieCutter is a utility script for accessing the browser cookies from the web player. PlayerPrefsx takes that base, and adds functions so that, to make your game web-game-save compatible, you can simply find and replace PlayerPrefs with PlayerPrefsx. PlayerPrefsx will automatically determine whether to use CookieCutter code or PlayerPrefs code so your game can always save.

Usage

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 PlayerPrefs.SetInt("Player Count", 5), etc - just like PlayerPrefs. PPX also has SetBool and GetBool methods that work online and off.

For best reliability, put the following code before you use PlayerPrefsx:

<javascript>while (!PlayerPrefsx.loaded) yield;</javascript>

It shouldn't take any significant amount of time to load the cookies - a frame at most, probably. (Non-Web player builds will be loaded instantaneously) In any case this will make sure PPX function are not called when they shouldn't be.

Notes

  • 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 http://myhost.com/myProjects/coolGame/webPlayer.html, set path to /myProjects/coolGame to ensure the cookies are not available to urls outside that directory on the web server.)

JavaScript - PlayerPrefsx.js

<javascript>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 : PlayerPrefsx;

static var loaded = false; static var path = "/"; // Change this to limit the scope of the cookies created private static var isWebPlayer : boolean;

function Start () { isWebPlayer = Application.platform == RuntimePlatform.OSXWebPlayer || Application.platform == RuntimePlatform.WindowsWebPlayer; if (!isWebPlayer) { loaded=true; return; }

_instance = this;

 _cookies = new Hashtable(); 
 // Define helper functions 
 ExternalEval( 
   "function PlayerPrefsx_init(name, callback){" + 
   "    var unity= document.getElementById('UnityObject'); " +
   "    if ( unity ) { " +
   "      unity.SendMessage( name, callback, " + 
   "            +document.cookie);" + 
   "    } " +
   "    else { " +
   "      alert('Could not find Unity plugin. Cookies will not be loaded'); " +
   "    } " +
   "}" + 
   "function  PlayerPrefsx_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+"';" + 
   "}"); 
 ReadCookiesFromBrowser(); 

}


//DEBUG CONSOLE

/*function OnGUI() { if (isWebPlayer) { for (var element in _cookies) { GUILayout.Label(element); } } }*/

// Called by the browser javascript during initialisation function _ReadCookiesCallback(cookie_string : String) { loaded=true; _cookies = new Hashtable(); var split : Array = cookie_string.Split(";"[0]); for(var element in split ) { var key_value = element.Split(["="[0]],2); if(key_value.Length == 2) _cookies[key_value[0].Trim()] = key_value[1]; } }

function ReadCookiesFromBrowser() {

  loaded=false;
  if(isWebPlayer) {
      ExternalCall("PlayerPrefsx_init", gameObject.name, "_ReadCookiesCallback"); 

}

  else
      _ReadCookiesCallback("test1=1; testCookie=NotWebPlayer;");

}

static function ReloadCookies(){

 _instance.ReadCookiesFromBrowser();

}

static function SetCookie(name : String, value : String, days : int) {

  _cookies[name]=value; 
  ExternalCall("PlayerPrefsx_set", name, value, days); 

} static function SetCookie(name : String, value) { var valueS : String = value.ToString();

  _cookies[name]=valueS; 
  ExternalCall("PlayerPrefsx_set", name, value); 

}

static function GetCookie(name : String) : String { if (_cookies[name] != null) { return _cookies[name]; } else { return ""; } }

//PLAYERPREFS COMPATIBILITY FUNCTIONS

static function SetBool(name : String, value : boolean) { SetInt(name, value ? 1 : 0); } static function SetInt(name : String, value : int) { if (isWebPlayer) SetCookie(name, value); else PlayerPrefs.SetInt(name, value); } static function SetFloat(name : String, value : float) { if (isWebPlayer) SetCookie(name, value); else PlayerPrefs.SetFloat(name, value); } static function SetString(name : String, value : String) { if (isWebPlayer) SetCookie(name, value); else PlayerPrefs.SetString(name, value); }

static function GetBool(name : String, defaultValue : boolean) : boolean { return (GetInt(name, defaultValue ? 1 : 0) == 1); } static function GetInt(name: String, defaultValue : int) : int { if (name=="") return defaultValue; if (isWebPlayer) { var temp : String = GetCookie(name); if (temp != "") { return int.Parse(temp); } else { SetCookie(name, ""+defaultValue); return defaultValue; } } else return PlayerPrefs.GetInt(name, defaultValue); }

static function GetFloat(name: String, defaultValue : float) : float { if (name=="") return defaultValue; if (isWebPlayer) { var temp : String = GetCookie(name); if (temp != "") return float.Parse(temp); else { SetCookie(name, defaultValue); return defaultValue; } } else return PlayerPrefs.GetFloat(name, defaultValue); }

static function GetString(name: String, defaultValue : String) : String { if (name=="") return defaultValue; if (isWebPlayer) { var temp : String = GetCookie(name); if (temp != "") return temp; else { SetCookie(name, defaultValue); return defaultValue; } } else return PlayerPrefs.GetString (name, defaultValue); }</javascript>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox