Created
July 22, 2012 08:19
-
-
Save Chman/3158887 to your computer and use it in GitHub Desktop.
Kongregate API & Unity3D
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Kongregate API integration in Unity3D (UnityScript version) | |
// | |
// 1. Create a "KongAPI" game object in your very first scene. | |
// 2. Create a new script "KongAPI.js". | |
// 3. Fill it with the following code : | |
#pragma strict | |
static var isConnected:boolean = false; | |
static var userId:int = 0; | |
static var username:String = "Guest"; | |
static var gameAuthToken:String = ""; | |
function OnKongregateAPILoaded(userInfoString:String) | |
{ | |
isConnected = true; | |
Debug.Log("Connected to Kongregate."); | |
var params:String[] = userInfoString.Split("|"[0]); | |
userId = parseInt(params[0]); | |
username = params[1]; | |
gameAuthToken = params[2]; | |
} | |
function Awake() | |
{ | |
if (isConnected) | |
return; | |
DontDestroyOnLoad(this); | |
Application.ExternalEval( | |
"if(typeof(kongregateUnitySupport) != 'undefined'){" + | |
"kongregateUnitySupport.initAPI('KongAPI', 'OnKongregateAPILoaded');" + | |
"}" | |
); | |
} | |
static function send(stat:String, value:int) | |
{ | |
Debug.Log("Submit to Kongregate : " + stat + " - " + value.ToString()); | |
if (isConnected) | |
Application.ExternalCall("kongregate.stats.submit", stat, value); | |
} | |
// 4. Add the "KongAPI.js" script to your "KongAPI" game object. | |
// 5. Send stats to Kongregate using : | |
KongAPI.send("StatNameInKongAdministration", statValueAsInteger); | |
// And that's all you need. The "KongAPI" class is static, so you can call "KongAPI.send()" from anywhere and | |
// from any scene as long as it's been initialized. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment