Unity
Toffee Unity SDK provides easy integration of offers into Unity games.
Installation​
Option 1: Git URL (Recommended)​
- Open Unity and go to Window → Package Manager
- Click the + button and select Add package from git URL
- Enter the SDK URL:
https://github.com/toffeecom/sdk-unity.git
Option 2: Add Package from Disk​
- Clone or download this repository
- Open Unity and go to Window → Package Manager
- Click the + button and select Add package from disk
- Navigate to the downloaded repository folder and select
package.json
Option 3: Unity Package File​
- Download the latest Unity package from our releases page
- 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'));
}