Skip to content

Unity Integration

This guide will walk you through integrating the Funtico SDK into your Unity WebGL game. We’ll cover everything from initial setup to signing in users and saving scores, all with practical code examples.

Before you can call any SDK functions, you need to add the FunticoManager to your scene. This is a crucial step, as this component handles all the communication with the Funtico backend.

  1. Create a Manager Object: In your first scene (like a loading or main menu scene), create a new empty GameObject. A good name for it is FunticoManager.
  2. Add the Script: Attach the FunticoManager.cs script to the GameObject you just created.
  3. Make it Persistent: The FunticoManager is a singleton that needs to persist across scene loads. The script handles this for you with DontDestroyOnLoad(gameObject), so you’re all set!

That’s it for the scene setup. Now you can access the SDK from anywhere in your code using FunticoManager.Instance.

Section titled “Option A: Use Our Pre-configured Template (Recommended)”

The easiest way is to use the template that comes with the Funtico SDK package, which is already set up for you.

  1. In the Unity Editor, go to Edit > Project Settings > Player.
  2. Select the WebGL tab.
  3. Open the Resolution and Presentation section.
  4. From the WebGL Template dropdown menu, select the Funtico template.

Now, when you build your project, Unity will use this template automatically.

If you are using your own WebGL template, you’ll need to make a few manual edits to your index.html file. These changes are necessary to ensure the Funtico JavaScript SDK (which runs in the browser) can find and communicate with your Unity game instance.

Here are the three required steps:

  1. Add the Funtico SDK Script

    In the <head> section of your index.html, add the following line to load the Funtico JavaScript library:

    <script src="https://funtico-frontend-js-sdk.pages.dev/funtico-sdk.min.js"></script>
  2. Create a Global Instance Variable

    The Funtico SDK needs a global variable to find your game. In your index.html, locate this line:

    var script = document.createElement("script");

    Just before it, add the following line to declare the variable:

    var myGameInstance = null;
  3. Assign the Unity Instance

    Finally, you need to assign the created Unity game instance to the variable from the previous step. Find the createUnityInstance function call in your file. Inside its .then() block, add myGameInstance = unityInstance;.

    It will look like this:

    createUnityInstance(canvas, config, (progress) => {
    //... progress bar logic
    }).then((unityInstance) => {
    myGameInstance = unityInstance; // <-- Add this line
    //... other logic
    });
  4. Add perserving drawing buffer Finally, we need to add one more line, after Unity config creatio. Find definition of config- it will be multiline and will look like this: var config = { xxx, } Add the following line under it to enable preserving the drawing buffer:

    config.webglContextAttributes = {
    "preserveDrawingBuffer": true,
    "powerPreference": "{{{ WEBGL_POWER_PREFERENCE }}}"
    };

Here’s how to use the main features.

First things first, you need to initialize the SDK. This should be done as early as possible when your game starts.

using FunticoSDK.Runtime.Scripts;
using UnityEngine;
using Cysharp.Threading.Tasks;
public class GameInitializer : MonoBehaviour
{
private string authClientId = "YOUR_CLIENT_ID";
private string env = "production"; // or "sandbox"
void Start()
{
// Initialize the SDK
FunticoManager.Instance.Init(authClientId, env);
// You can then proceed with other logic, like trying to sign the user in
CheckUserLoginStatus().Forget();
}
private async UniTaskVoid CheckUserLoginStatus()
{
//... see next steps
}
}

To prompt the user to sign in, you’ll call SignInAsync. Since this is an asynchronous operation, you’ll need to await it.

public async UniTask SignInUser()
{
try
{
Debug.Log("Attempting to sign in...");
await FunticoManager.Instance.SignInAsync();
Debug.Log("Sign-in successful!");
// Now you can load the main game scene or get user info
}
catch (System.Exception ex)
{
Debug.LogError($"Sign-in failed: {ex.Message}");
}
}

After a user has signed in, you can retrieve their profile information. The GetUserInfoAsync method returns a FunticoUser object, or null if no user is logged in.

using FunticoSDK.Runtime.Scripts;
using UnityEngine;
using Cysharp.Threading.Tasks;
using UnityEngine.UI; // For Text elements
public class UserProfile : MonoBehaviour
{
private Text userNameText;
private Text userIDText;
async void Start()
{
try
{
FunticoManager.FunticoUser user = await FunticoManager.Instance.GetUserInfoAsync();
if (user!= null)
{
Debug.Log($"Welcome back, {user.UserName}!");
userNameText.text = user.UserName;
userIDText.text = user.UserId;
}
else
{
Debug.Log("No user is logged in.");
// Maybe show a "Sign In" button here
}
}
catch (System.Exception ex)
{
Debug.Log($"Could not get user info: {ex.Message}");
}
}
}

Saving a player’s score is another simple async call.

public async UniTask SavePlayerScore(int score)
{
try
{
Debug.Log($"Saving score: {score}");
string response = await FunticoManager.Instance.SaveScoreAsync(score);
Debug.Log($"Server response: {response}");
FunticoManager.ShowAlert("Score saved successfully!");
}
catch (System.Exception ex)
{
Debug.LogError($"Failed to save score: {ex.Message}");
FunticoManager.ShowAlert("Error saving score.");
}
}

To sign the user out, simply call DoSignOut. This is a synchronous call, so no await is needed.

public void LogOut()
{
Debug.Log("Signing out...");
FunticoManager.Instance.DoSignOut();
// Return to the main menu or login screen
}

To create a WebGL build of your game:

  1. Go to File → Build Settings
  2. Select WebGL and click “Switch Platform” if it’s not already active
  3. Click “Build”. Unity will ask you to choose a folder to save the build files

Here are a few simple ways to start a local server. Open your terminal or command prompt, navigate into your build folder, and run one of the following commands:

Node.js

Terminal window
npx serve -p 3000

Python 3

Terminal window
python -m http.server 3000

PHP

Terminal window
php -S localhost:3000

Then, open your browser to http://localhost:3000.

IssueSolution
”FunticoManager instance not found”Ensure FunticoManager GameObject exists in your scene and the script is attached
SignInAsync never completesCheck your authClientId is correct and you’re connected to the internet
Mock data in WebGL buildVerify the Funtico SDK script is included in your HTML template
”Cannot find myGameInstance”Follow the Custom HTML Template steps to expose your Unity instance to JavaScript