|
// shamelessly copied from http://wiki.unity3d.com/index.php?title=CookieCutter |
|
// this class provides wrapper to intereact with browser cookies. |
|
|
|
using UnityEngine; |
|
using System.Collections; |
|
using System.Collections.Generic; |
|
|
|
public class CookieCutter : MonoBehaviour { |
|
|
|
public static bool loaded { get; set; } |
|
public static string rawCookieString { get; set; } |
|
private static Dictionary<string, string> _cookies; |
|
private static string _path = "/"; // Change this to limit the scope of the cookies created |
|
private static CookieCutter _instance; |
|
public static CookieCutter instance { |
|
get { |
|
if(!_instance) { |
|
_instance = FindObjectOfType(typeof(CookieCutter)) as CookieCutter; |
|
} |
|
if(!_instance) { |
|
_instance = new GameObject().AddComponent<CookieCutter>(); |
|
_instance.gameObject.name = "CookieCutter"; |
|
} |
|
return _instance; |
|
} |
|
set{} |
|
} |
|
|
|
void Awake() { |
|
_cookies = new Dictionary<string, string>(); |
|
Application.ExternalEval(System.String.Format( |
|
@" |
|
function CookieCutter_getUnity() { |
|
if (typeof UnityObject === 'function' || typeof UnityObject2 === 'function') { |
|
for(var key in window) { |
|
var obj = window[key]; |
|
if (jQuery.isFunction(obj.getUnity)) { |
|
return obj.getUnity(); |
|
} |
|
} |
|
} |
|
return null; |
|
} |
|
|
|
function CookieCutter_init(name, callback) { |
|
var unity = CookieCutter_getUnity(); |
|
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={0}'; |
|
} |
|
", _path)); |
|
ReadCookiesFromBrowser(); |
|
} |
|
|
|
// Called by the browser javascript during initialisation |
|
public void _ReadCookiesCallback(string cookie_string) { |
|
// typical cookie string looks like this: PHPSESSID=42ckges0gi9le28gbedd56k1r5; wp-settings-1=editor%3Dhtml; wp-settings-time-1=1363981258 |
|
string[] strings = cookie_string.Split(';'); |
|
foreach (string str in strings) { |
|
string str_stripped = str.Trim(); |
|
string[] parts = str_stripped.Split('='); |
|
if (parts.Length == 2) { |
|
string name = parts[0]; |
|
string val = parts[1]; |
|
setCookieDict(name, val); |
|
} |
|
} |
|
|
|
loaded = true; |
|
} |
|
|
|
protected void setCookieDict(string name, string val) { |
|
if (_cookies.ContainsKey(name)) { |
|
_cookies[name] = val; |
|
} else { |
|
_cookies.Add(name, val); |
|
} |
|
} |
|
|
|
public static void ReadCookiesFromBrowser() { |
|
loaded = false; |
|
if(Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.OSXWebPlayer) { |
|
Application.ExternalCall("CookieCutter_init", "CookieCutter", "_ReadCookiesCallback"); |
|
} else { |
|
instance._ReadCookiesCallback("test1=1; testCookie=NotWebPlayer;"); |
|
} |
|
} |
|
|
|
public static void ReloadCookies() { |
|
ReadCookiesFromBrowser(); |
|
} |
|
|
|
public static void SetCookie(string name, string val, int days) { |
|
instance.setCookieDict(name, val); |
|
Application.ExternalCall("CookieCutter_set", "CookieCutter", val, days); |
|
} |
|
|
|
public static string GetCookie(string name) { |
|
if (_cookies.ContainsKey(name)) { |
|
return _cookies[name]; |
|
} |
|
return ""; |
|
} |
|
|
|
} |