iOS SDK v2.0.3 through v2.0.4 has a bug that results Adjust Installs and Sessions being underreported. This is resolved in SDK v2.0.5
Unity 2017.1.1 requires Kongregate SDK 2.0.4 or higher. SDK 2.0.4 resolved an issue related to a change in the way Unity handles cookies on Android, which resulted in users being logged out of Kongregate.

Unity Editor Setup

  1. In Unity, select Assets -> Import Package -> Custom Package… Find and select kongregate-sdk-x.x.x.x/Unity/Kongregate.unitypackage and select all files.
    • You may receive some warnings about images/textures/PNG files during the import. You can ignore these.
  2. Create an Empty Game Object named Kongregate. Attach a new script to the game object to handle initializing and managing the Kongregate SDK. You may use the KongregateGameObject-Example.cs file as a starting point for this as it contains a lot of the hooks already in the script that you will need to implement. Feel free to modify this to suit your needs and remove or comment out the unneeded example code. Verify that you have set your Game ID and API Key in this file.
    • NOTE: You may notice the new class Assets/Plugins/Kongregate/KongregateManager.cs and set of demo scripts under Assets/Plugins/Kongregate/demo. The KongregaetGameObject-Example.cs script is the traditional starting point that many devs are familiar with. This example script is still fully supported. The KongregateManager.cs class is intended to be a cleaner starting point that may be directly used by your game. Simply attach it to a game object and initilize the fields. The individual demo scripts are broken into logical API’s our SDK provides. Feel free to use either approach.

Unity Android Setup

Below are extra steps required for Android builds.

Shared Secret Provider

Most Kongregate games support Shared Sign-on meaning when you log into one Kongregate Game you automatically log into other Kongregate Games without needing to re-enter a username and password. To support this on Android your game must:

  1. Be signed using Kongregate’s certificate (see Mobile Build System)
  2. Define a Shared Secret Provider and the custom ReadSharedData2 permission as described below.

Use the Shared Secret Provider Generator tool to generate a Shared Secret Provider class and retrieve the entries that must be added to /Assets/Plugins/Android/AndroidManifest.xml. If this file doesn’t exist, you can copy the base one from Unity /Applications/Unity/PlaybackEngines/AndroidPlayer/Apk/AndroidManifest.xml.

Select Tools -> Kongregate Tools -> Shared Secret Provider Generator, Click Generate Provider and follow the instructions in the Text Box:

Provider Generator

Copy the <provider>, <permission>, and <uses-permissions> tags out of the text box and past them into your AndroidManifest.xml. Be sure the resulting android:name and android:authority follow the format com.kongregate.android.api.[YourGame]SharedSecretProvider. This name must be unique across all Kongregate games.

If you have problems with the tool, you may use the providerbuilder ant script instead.

Google Play Game Services Library

The Kongregate and Swrve SDKs require the Google Cloud Messaging and Ads components from Google Play Game Services library. Google’s appcompat-v7 and support-v4 are also required. You have a few options for adding the required dependencies.

Option 1 (preferred): using Google Play Games Plugin (adding GCM and Ads to dependencies)

If you use Google Play Games Plugin for Unity for Play Services integration you can take advantage of the PlayServicesSupport class to manage the extra modules required by our SDK. Simply drop this class (KongregateGPSDependencies.cs) in your /Assets/Editor/ directory and the PlayServicesSupport class will download the modules.

Option 2: using other third party GPGS plugins

If you use another GPS Plugin, such as Prime31’s, you may or may not be all set. The plugin must include the Cloud Messaging and Ads modules. If they are missing, you will need to add them modules or the complete GPS library project as described below. You may need to resolve JAR files that are included by multiple Plugins.

Option 3: include the full Google Play Game Services and support library projects

If you do not use a Play Services plugin, you need to include the google play services jar and required resource into your project.

  1. Use the Android SDK Manager to download the Extras/Google Play Services and Extras/Android Support Libary and Support Library Projects. For more details, see the full setup instructions

  2. Copy the google play services and appcompat library projects into your your project.

     cp -r <android_sdk>/extras/google_play_services/libproject/google-play-services_lib <unity_project>/Assets/Plugins/Android
     cp -r <android_sdk>/extras/android/support/v7/appcompat/libs <unity_project>/Assets/Plugins/Android
    

    You should be good to go at this point. However, the big drawback with this approach is that it includes all the GPGS modules and you are likely to run against a too many method references error.

Option 4: copy over only the required GPGS and Support AARs

This approach only works in Unity 5, and involves copying over all the requires GPGS and Support Android Archives (AARs) from a maven repository. This will provide you with the minimal required modules and should help prevent hitting the too many method references error, though it’s not quite as convenient at 1 since you must manuall manage the dependencies.

  1. Use the Android SDK Manager to download Extras/Android Suport Repository and Extras/Google Repository

  2. Copy over the Ads, GCM and support AARs along with the AARs they depend on into <unity_project>/Assets/Plugins/Android/

     <android_sdk>/extras/android/m2repository/com/android/support/appcompat-v7/23.1.1/appcompat-v7-23.1.1.aar
     <android_sdk>/extras/android/m2repository/com/android/support/support-v4/23.1.1/support-v4-23.1.1.aar
     <android_sdk>/extras/google/m2repository/com/google/android/gms/play-services-ads/8.4.0/play-services-ads-8.4.0.aar
     <android_sdk>/extras/google/m2repository/com/google/android/gms/play-services-base/8.4.0/play-services-base-8.4.0.aar
     <android_sdk>/extras/google/m2repository/com/google/android/gms/play-services-basement/8.4.0/play-services-basement-8.4.0.aar
     <android_sdk>/extras/google/m2repository/com/google/android/gms/play-services-gcm/8.4.0/play-services-gcm-8.4.0.aar
    

Troubleshooting GPGS and Support files

A common issue adding GPGS and Support files to your project is that another Unity Plugin may already include some of the modules leadin to duplicate class conflicts at compile time. To resolve this, identify the JARs or AARs that are included twice and delete whichever one is older. This most often happens with the support-v4.jar which is used by many plugins.

DEX Method Limit

Once you’ve added all the various plugins required, it’s not unusual to hit the 64K DEX Method limit. Unity is improving it’s support for Gradle Builds with every release, which will hopefully eventually remove this restriction. For now, however, the best option is to trim your plugins to only include what you need.

If you still are bumping against the 64K limit, you may be able to apply the following technique to some of your plugins to bring down the number methods they bring in.

  1. Look for plugins that do not contain any resources. That is they do not contain a /Assets/Plugins/<plugin>/res directory or the /res directory is empty. For example the beta and answers plugins included with Fabric do not contain any resources and are good candidates for this technique.
  2. Open the AndroidManifest.xml of the plugin and change the package name in the manifest tag to match the package name of your app.

This should save just over 1600 methods per plugin. A few more tips may be found in our FAQ.

Unity iOS Setup

The unitypacakge includes /Assets/Editor/KongregatePostProcessor.dll that will perform all the required modifications to the XCode project at build time. If this post-processor interferes another post-processor your projects uses, you may need to disable it by removeing the KongregatePostProcessor.dll file and performing the following steps when you build the XCode project.

  1. Make sure the KongregateSDK and dependencies are added to the Xcode project. Drag the following files from kongregate-sdk-x.x.x.x/ios/ into the generated XCode project.

     KongregateUnityWrapper.framework
     KongregateSDK.framework 
     KongregateSDK.bundle
     Swrve.framework
     Swvrve.bundle
    

    The KongregateSDK.framework, KongregateUnityWrapper.framework, and Swrve.frameworks should now be in the Linked Frameworks and Libraries section of your projects General Build Settings. The KongregateSDK.bundle and Swrve.bundle should now be in the Copy Bundle Resources section of your project’s Build Phases

  2. Add any missing dependent iOS frameworks to your project under General -> Linked Framworks and Libraries.

     Accounts.framework
     AdSupport.framework
     AddressBook.framework
     AssetLibrary.framework
     AVFoundation.framework
     CFNetwork.framework
     CoreData.framework
     CoreLocation.framework
     CoreTelephony.framework
     QuartzCore.framework
     GameKit.framework
     iAd.framework
     MessageUI.framework
     SafariServices.framework
     Security.framework
     StoreKit.framework
     SystemConfiguration.framework
     UIKit.framework
    
  3. Add the following linker flags under Build Settings -> Linking -> Other Linker Flags

     -lz
     -ObjC
    

Unity Web Setup

Please see the web integration documentation for information on Unity web player builds.

Initialization

Initialize the SDK in the script attached to the Kongregate Game Object you created. You may see the kongregate-sdk-x.x.x.x/Unity/KongregateGameObject-Example.cs for a complete sample. This file is intended as an example. It may be used as a starting point, but be sure to review and adapt it for your games needs, removing any unecessary parts.

Initialize the SDK as soon as possible after you app starts. The Game Objects onEnable() method is a good place.


  // Modify the settings in the static `KongregateAPI.Settings` object prior to initializing our SDK.
  // Your producer will provide you with the correct values .
  // Be sure to use the correct values for Android and iOS builds. 
#define UNITY_IPHONE
  KongregateAPI.Settings.SwrveAppId = 1234;
  KongregateAPI.Settings.SwrveApiKey = "YourSwrveAPIKey"
  KongregateAPI.Settings.AdjustAppToken = "YourAdjustAppToken"
  KongregateAPI.Settings.AdjustEventTokenMap = new Dictionary<string,object>()  {
    { KongregateAPI.ADJUST_SALE, "YourSaleToken" },
    { KongregateAPI.ADJUST_SESSION, "YourSessionToken" },
    { KongregateAPI.ADJUST_INSTALL, "YourInstallToken" }
  };
  KongregateAPI.Settings.AdjustEnvironment = "sandbox"; // be sure to set to "production" for release builds
#elif UNITY_ANDROD
  // Android settings here
  ...
#endif
  // Settings common for iOS and Android
  KongregateAPI.Settings.AdjustEnvironment = "sandbox"; // be sure to set to "production" for release builds

  KongregateAPI kongregate = KongregateAPI.Initialize(GetGameId(), GetAPIKey()); 

  // Set the callback that will be invoked to collect game specific properties to include in all analytic events.
  kongregate.Analytics.SetCommonPropsCallback(() => {
    Dictionary<string,object> commonProps = new Dictionary<string, object>()
    {
      { "hard_currency_balance", 100 },
      { "soft_currency_balance", 200 },
      // ...
    };
    return commonProps;
  });

IMPORTANT: Adjust and Swrve require different tokens/keys/ids for Android and iOS. Use #define’s or some othe mechanism to help make sure they are set properly for your build. Swrve also has different IDs/Keys for Sandbox and Production mode, and Adjust has an environment setting that must be set for Sandbox Production.

Lifecycle Hooks

The Kongregate SDK must be notified when the app is paused or resumes. This is important for accuratly tracking sessions and pausing/resuming background tasks.

void OnApplicationPause(bool pausing) {
  KongregateAPI kongregate = KongregateAPI.GetAPI();
  if (pausing) {
    kongregate.OnPause();
  } else {
    kongregate.OnResume();
  }
}

Event Callbacks

You should set a callback listener for processing Kongregate events.

KongregateAPI.GetAPI().SetEventBundleListener(gameObject, (string eventName, string eventJSON) => {
  // handle the kongregate events
}

User info & Services

You may access various information about the user.

KongregateAPI api = KongregateAPI.GetAPI();
string username = api.Services.GetUsername();
string authtoken = api.Services.GetGameAuthToken();
bool isguest = api.Services.IsGuest();

Statistics, Scores & Achievements

You may submit statistics through the client, if you game supports Kongregate Achievements. This may also be done server-to-server using our REST API.

KongregateAPI.GetAPI().Stats.Submit("HighScore", 1);

Mobile control & button

You may either let the button draw natively, or manually open the kongregate panel

//configure up our button
kongregate.Mobile.ButtonSetNativeRendering(true); // true by default, false means render in Unity rather than native
kongregate.Mobile.ButtonSetX(10);
kongregate.Mobile.ButtonSetY(10);
kongregate.Mobile.ButtonSetSize(48);
kongregate.Mobile.ButtonShow();

// if our button is not hidden, we need to draw it
if (!kongregate.Mobile.ButtonIsHidden()) {
  if (kongregate.Mobile.ButtonIsNativeRendering()) {
    //nothing to do, rendering is handled in native SDK
  } else {
    // draw our button
    if (GUI.Button (kongregate.Mobile.ButtonGetRect(), kongregate.Mobile.ButtonGetTexture(), "label")) {
      Debug.Log ("You clicked the Kong button!");
      kongregate.Mobile.OpenKongregateWindow();
    }
  }

Unity API Docs & Usage

Unity API Documentation