Skip to main content

Full Example

Full Example: Game Manager

A complete example that can be used in an actual game.

using UnityEngine;
using Tudada;

public class GameManager : MonoBehaviour
{
private TudadaRewardedVideoAd _rewardedAd;
private bool _isLoggedIn = false;

void Start()
{
// Register lifecycle events
TudadaSDK.OnShow += OnAppShow;
TudadaSDK.OnHide += OnAppHide;

// Start login
DoLogin();
}

// ===== Auth =====

void DoLogin()
{
TudadaSDK.Instance.Login(
onSuccess: (result) => {
Debug.Log("Login success! Code: " + result.code);
Debug.Log("User ID: " + result.userId);
_isLoggedIn = true;

// Load initial data after login
LoadCloudData();
PrepareAd();
},
onFail: (err) => Debug.LogError("Login failed: " + err)
);
}

// ===== Cloud Data =====

void LoadCloudData()
{
TudadaSDK.Instance.TudadaStoreGet("saveData",
onSuccess: (result) => {
if (!string.IsNullOrEmpty(result.value))
{
var saveData = JsonUtility.FromJson<SaveData>(result.value);
Debug.Log("Cloud data loaded - Level: " + saveData.level);
ApplySaveData(saveData);
}
else
{
Debug.Log("No saved data, starting new game");
}
},
onFail: (err) => Debug.LogError("Cloud load failed: " + err)
);
}

void SaveCloudData()
{
var saveData = new SaveData { level = 5, score = 10000, coins = 500 };
string json = JsonUtility.ToJson(saveData);

TudadaSDK.Instance.TudadaStoreSave("saveData", json,
onSuccess: (_) => Debug.Log("Cloud save complete"),
onFail: (err) => Debug.LogError("Cloud save failed: " + err)
);
}

// ===== Local Settings =====

void SaveSettings()
{
TudadaSDK.Instance.SetStorageSync("bgmVolume", "0.8");
TudadaSDK.Instance.SetStorageSync("sfxVolume", "1.0");
TudadaSDK.Instance.SetStorageSync("language", "ko");
}

void LoadSettings()
{
string bgmVolume = TudadaSDK.Instance.GetStorageSync("bgmVolume");
string sfxVolume = TudadaSDK.Instance.GetStorageSync("sfxVolume");
Debug.Log("BGM: " + bgmVolume + ", SFX: " + sfxVolume);
}

// ===== Ads =====

void PrepareAd()
{
_rewardedAd = TudadaSDK.Instance.CreateRewardedVideoAd("your-ad-unit-id");

_rewardedAd.OnLoad += () => Debug.Log("Ad ready");

_rewardedAd.OnError += (err) =>
Debug.LogError("Ad error: " + err.errMsg);

_rewardedAd.OnClose += (result) => {
if (result.isEnded)
{
GiveReward();
}
// Preload next ad
_rewardedAd.Load();
};

// Load first ad
_rewardedAd.Load();
}

// Called from watch ad button
public void OnWatchAdButtonClick()
{
if (_rewardedAd != null && _rewardedAd.IsLoaded)
{
_rewardedAd.Show();
}
else
{
Debug.Log("Ad is not loaded yet");
}
}

void GiveReward()
{
Debug.Log("Reward granted: Coins +100");
// Reward granting logic
}

// ===== Lifecycle =====

void OnAppShow(OnShowResult result)
{
Debug.Log("App returned to foreground");
Time.timeScale = 1;
// Resume BGM, etc.
}

void OnAppHide(OnHideResult result)
{
Debug.Log("App went to background");
Time.timeScale = 0;
SaveCloudData(); // Auto-save when going to background
}

// ===== Vibration =====

public void OnButtonClick()
{
TudadaSDK.Instance.VibrateShort(type: VibrateType.light);
}

public void OnImpact()
{
TudadaSDK.Instance.VibrateShort(type: VibrateType.heavy);
}

// ===== Cleanup =====

void OnDestroy()
{
TudadaSDK.OnShow -= OnAppShow;
TudadaSDK.OnHide -= OnAppHide;
_rewardedAd?.Destroy();
}

// ===== Helpers =====

void ApplySaveData(SaveData data)
{
// Game state restoration logic
}
}

[System.Serializable]
public class SaveData
{
public int level;
public int score;
public int coins;
}