daily pastebin goal
3%
SHARE
TWEET

Untitled

a guest Dec 7th, 2017 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //thanks to sophieH for this script :D
  2.  
  3. private var debugmode = false;
  4.  
  5. var isKongregate = false;
  6.  
  7. static var userId = 0;
  8. static var username = "Guest";
  9. static var gameAuthToken = "";
  10.  
  11. var primaryScript = false;
  12.  
  13. var stat = "stat";
  14. var statValue = 0;
  15.  
  16. static var loggedin = false;
  17. static var alreadyreg : boolean = false;
  18. static var regname : String = "";
  19.  
  20. function OnKongregateAPILoaded(userInfoString){
  21.    
  22.    
  23.     // We now know we’re on Kongregate
  24.     isKongregate = true;
  25.     Debug.Log("ON KONG");
  26.     // Split the user info up into tokens
  27.     var params = userInfoString.Split("|"[0]);
  28.     userId = parseInt(params[0]);
  29.     username = params[1];
  30.     gameAuthToken = params[2];
  31.    
  32.     loggedin = true;
  33.    
  34.     Application.ExternalEval(
  35.     "kongregate.services.addEventListener('login', function(){" +
  36.     "   var services = kongregate.services;" +
  37.     "   var params=[services.getUserId(), services.getUsername(), services.getGameAuthToken()].join('|');" +
  38.     "   kongregateUnitySupport.getUnityObject().SendMessage('KongregateAPI', 'OnKongregateUserSignedIn', params);" +
  39.     "});");
  40.    
  41. }
  42.  
  43.  
  44. // Called when the Kongregate user signs in, parse the tokenized user-info string that we
  45. // generate below using Javascript.
  46. function OnKongregateUserSignedIn(userInfoString){
  47.     // We now know we are on Kongregate
  48.     isKongregate = true;
  49.     Debug.Log("ON KONG");
  50.     // Split the user info up into tokens
  51.     var params = userInfoString.Split("|"[0]);
  52.     userId = parseInt(params[0]);
  53.     username = params[1];
  54.     gameAuthToken = params[2];
  55.    
  56.     loggedin = true;
  57. }
  58.  
  59. //we don't need this part so cut it out
  60.  
  61. function OnGUI(){
  62.     if (debugmode){
  63.         if (isKongregate){
  64.             GUILayout.Label("Connected!");
  65.             GUILayout.Label("UserID: " + userId);
  66.             GUILayout.Label("UserName: " + username);
  67.             GUILayout.Label("gameAuthToken: " + gameAuthToken);
  68.            
  69.         }else{
  70.             GUILayout.Label("Not Connected");
  71.         }
  72.        
  73.         GUILayout.BeginHorizontal();
  74.         stat = GUILayout.TextField(stat, GUILayout.Width(100));
  75.         if (GUILayout.Button("-"))  statValue = Mathf.Max(0, statValue - 1);
  76.         GUILayout.Label(statValue.ToString());
  77.         if (GUILayout.Button("+")) statValue++;
  78.            
  79.         if (GUILayout.Button("sumbit stat!")){
  80.             //Application.ExternalCall("kongregate.stats.submit","Stat",Random.Range(0,10));
  81.             Application.ExternalCall("kongregate.stats.submit",stat,statValue);
  82.         }
  83.         GUILayout.EndHorizontal();
  84.     }
  85.    
  86. }
  87.  
  88. function SubmitStat(stat : String, val : int) {
  89.     if (isKongregate) {
  90.         Application.ExternalCall("kongregate.stats.submit", stat, val);
  91.         Debug.Log("Stat Sumbitted!  " + stat + "   " + val);
  92.     }
  93. }
  94.  
  95. // Register a sign in handler to let us know if the user signs in to Kongregate. Notice how we are using the
  96. // Javascript API along with Application.ExternalEval, and then calling back into our app using SendMessage.
  97. // We deliver the new user information as a simple pipe-delimited string, which we can easily parse using String.Split.
  98.  
  99. function Awake(){
  100.    
  101.     // we only need one instance of this script
  102.     var primaryExists = false;
  103.     var otherAPIinstances : KongregateAPI[] = FindObjectsOfType(KongregateAPI) as KongregateAPI[];
  104.     for (var i=0; i<otherAPIinstances.Length-1; i++){
  105.         if (otherAPIinstances[i].primaryScript) primaryExists = true;
  106.     }
  107.    
  108.     if (primaryExists){
  109.         // there is already a working instance of this script, so lets stop this one.
  110.         Destroy(gameObject);
  111.     }else{
  112.         // there is no working instance of this script, so lets make it this.
  113.         primaryScript = true;
  114.     }
  115.  
  116.     // This game object needs to survive multiple levels
  117.     DontDestroyOnLoad (this);
  118. }
  119.  
  120. function getReg(s : String) {
  121.     //we already registered
  122.     regname = s;
  123.     alreadyreg = true;
  124.     Debug.Log(s);
  125. }
  126.  
  127. function Start(){
  128.     // Begin the API loading process if it is available
  129.     Application.ExternalEval(
  130.     "if(typeof(kongregateUnitySupport) != 'undefined'){" +
  131.     " kongregateUnitySupport.initAPI('KongregateAPI', 'OnKongregateAPILoaded');" +
  132.     "}"
  133.     );
  134.    
  135.     Application.ExternalEval(
  136.     "var i,x,y,ARRcookies=document.cookie.split(';');" +
  137.     "for (i=0;i<ARRcookies.length;i++) {" +
  138.     "x=ARRcookies[i].substr(0,ARRcookies[i].indexOf('='));" +
  139.     "y=ARRcookies[i].substr(ARRcookies[i].indexOf('=')+1);" +
  140.     "x=x.replace(/^\\s+|\\s+$/g,'');" +
  141.     "if (x=='sysLogin') {" +
  142.         "unityObject.getObjectById('unityPlayer').SendMessage('KongregateAPI', 'getReg', unescape(y));" +
  143.     "}" +
  144.     "}"
  145.     );
  146.  
  147. }
RAW Paste Data
Top