Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //thanks to sophieH for this script :D
- private var debugmode = false;
- var isKongregate = false;
- static var userId = 0;
- static var username = "Guest";
- static var gameAuthToken = "";
- var primaryScript = false;
- var stat = "stat";
- var statValue = 0;
- static var loggedin = false;
- static var alreadyreg : boolean = false;
- static var regname : String = "";
- function OnKongregateAPILoaded(userInfoString){
- // We now know weâre on Kongregate
- isKongregate = true;
- Debug.Log("ON KONG");
- // Split the user info up into tokens
- var params = userInfoString.Split("|"[0]);
- userId = parseInt(params[0]);
- username = params[1];
- gameAuthToken = params[2];
- loggedin = true;
- Application.ExternalEval(
- "kongregate.services.addEventListener('login', function(){" +
- " var services = kongregate.services;" +
- " var params=[services.getUserId(), services.getUsername(), services.getGameAuthToken()].join('|');" +
- " kongregateUnitySupport.getUnityObject().SendMessage('KongregateAPI', 'OnKongregateUserSignedIn', params);" +
- "});");
- }
- // Called when the Kongregate user signs in, parse the tokenized user-info string that we
- // generate below using Javascript.
- function OnKongregateUserSignedIn(userInfoString){
- // We now know we are on Kongregate
- isKongregate = true;
- Debug.Log("ON KONG");
- // Split the user info up into tokens
- var params = userInfoString.Split("|"[0]);
- userId = parseInt(params[0]);
- username = params[1];
- gameAuthToken = params[2];
- loggedin = true;
- }
- //we don't need this part so cut it out
- function OnGUI(){
- if (debugmode){
- if (isKongregate){
- GUILayout.Label("Connected!");
- GUILayout.Label("UserID: " + userId);
- GUILayout.Label("UserName: " + username);
- GUILayout.Label("gameAuthToken: " + gameAuthToken);
- }else{
- GUILayout.Label("Not Connected");
- }
- GUILayout.BeginHorizontal();
- stat = GUILayout.TextField(stat, GUILayout.Width(100));
- if (GUILayout.Button("-")) statValue = Mathf.Max(0, statValue - 1);
- GUILayout.Label(statValue.ToString());
- if (GUILayout.Button("+")) statValue++;
- if (GUILayout.Button("sumbit stat!")){
- //Application.ExternalCall("kongregate.stats.submit","Stat",Random.Range(0,10));
- Application.ExternalCall("kongregate.stats.submit",stat,statValue);
- }
- GUILayout.EndHorizontal();
- }
- }
- function SubmitStat(stat : String, val : int) {
- if (isKongregate) {
- Application.ExternalCall("kongregate.stats.submit", stat, val);
- Debug.Log("Stat Sumbitted! " + stat + " " + val);
- }
- }
- // Register a sign in handler to let us know if the user signs in to Kongregate. Notice how we are using the
- // Javascript API along with Application.ExternalEval, and then calling back into our app using SendMessage.
- // We deliver the new user information as a simple pipe-delimited string, which we can easily parse using String.Split.
- function Awake(){
- // we only need one instance of this script
- var primaryExists = false;
- var otherAPIinstances : KongregateAPI[] = FindObjectsOfType(KongregateAPI) as KongregateAPI[];
- for (var i=0; i<otherAPIinstances.Length-1; i++){
- if (otherAPIinstances[i].primaryScript) primaryExists = true;
- }
- if (primaryExists){
- // there is already a working instance of this script, so lets stop this one.
- Destroy(gameObject);
- }else{
- // there is no working instance of this script, so lets make it this.
- primaryScript = true;
- }
- // This game object needs to survive multiple levels
- DontDestroyOnLoad (this);
- }
- function getReg(s : String) {
- //we already registered
- regname = s;
- alreadyreg = true;
- Debug.Log(s);
- }
- function Start(){
- // Begin the API loading process if it is available
- Application.ExternalEval(
- "if(typeof(kongregateUnitySupport) != 'undefined'){" +
- " kongregateUnitySupport.initAPI('KongregateAPI', 'OnKongregateAPILoaded');" +
- "}"
- );
- Application.ExternalEval(
- "var i,x,y,ARRcookies=document.cookie.split(';');" +
- "for (i=0;i<ARRcookies.length;i++) {" +
- "x=ARRcookies[i].substr(0,ARRcookies[i].indexOf('='));" +
- "y=ARRcookies[i].substr(ARRcookies[i].indexOf('=')+1);" +
- "x=x.replace(/^\\s+|\\s+$/g,'');" +
- "if (x=='sysLogin') {" +
- "unityObject.getObjectById('unityPlayer').SendMessage('KongregateAPI', 'getReg', unescape(y));" +
- "}" +
- "}"
- );
- }
Add Comment
Please, Sign In to add comment