Skip to main content

Unity

Toffee Unity SDK provides easy integration of offers into Unity games.

Installation​

  1. Open Unity and go to Window → Package Manager
  2. Click the + button and select Add package from git URL
  3. Enter the SDK URL:
    https://github.com/toffeecom/sdk-unity.git

Option 2: Add Package from Disk​

  1. Clone or download this repository
  2. Open Unity and go to Window → Package Manager
  3. Click the + button and select Add package from disk
  4. Navigate to the downloaded repository folder and select package.json

Option 3: Unity Package File​

  1. Download the latest Unity package from our releases page
  2. Import the package via Assets → Import Package → Custom Package

Setup​

Initialize the SDK​

public class GameManager : MonoBehaviour
{
void Start()
{
// Initialize the static SDK with your publishable key
ToffeeSDK.Initialize("your_publishable_key");
}
}

Fetching and Showing Offers​

Basic Usage​

using System.Collections.Generic;

public class OfferManager : MonoBehaviour
{

public async void FetchAndShowOffer()
{
var userMetadata = new Dictionary<string, string>
{
{"level", "15"},
{"achievements", "speedster,collector"},
{"playtime_hours", "120"}
};

try
{
// Use static SDK to fetch offer
var offer = await ToffeeSDK.FetchOffer("player_42", userMetadata);

if (offer != null)
{
// Show offer
ToffeeSDK.ShowOffer(offer);
}
else
{
Debug.Log("No offer available");
}
}
catch (System.Exception e)
{
Debug.LogError($"Failed to fetch offer: {e.Message}");
}
}
}

User Metadata​

Provide relevant user metadata to get better targeted offers:

var userMetadata = new Dictionary<string, string>
{
{"level", "15"},
{"experience_points", "45230"},
{"achievements", "speedster,collector,champion"},
};

API Reference​

ToffeeSDK​

Initialize(string publishableKey)

  • Initialize the SDK with your publishable key
  • Call once at game startup

FetchOffer(string userId, Dictionary<string, string> userMetadata)

  • Returns offer object with URL and image, null if no offer
  • Static method, call from anywhere

ShowOffer(Offer offer)

  • Opens the offer url in webview

Testing​

Use sandbox mode during development:

void Start()
{
// Enable debug mode for testing
ToffeeSDK.EnableDebugMode(true);
ToffeeSDK.Initialize("your_sandbox_publishable_key", 'https://services.sandbox.toffee.com'));
}