Skip to content

Unity Integration

Add content moderation to your Unity games with the Vettly Unity SDK.

Installation

  1. Open Unity Package Manager (Window > Package Manager)
  2. Click the + icon and select "Add package from git URL"
  3. Enter: https://github.com/vettly/com.vettly.unity.git

Requirements

  • Unity 2021.3 LTS or higher
  • .NET Standard 2.1 compatibility
  • Internet connection for API calls

Quick Start

1. Create Settings Asset

In Unity, go to Assets > Create > Vettly > Settings to create a VettlySettings asset.

Configure the following:

SettingDescription
API KeyYour Vettly API key
EnvironmentProduction or Sandbox
Fail OpenAllow content when API fails (recommended: true)
Timeout SecondsRequest timeout (default: 30)
Policy PresetPolicy preset name to use

2. Initialize the SDK

csharp
using Vettly;

public class GameManager : MonoBehaviour
{
    async void Start()
    {
        // Option 1: Initialize with settings asset reference
        var settings = Resources.Load<VettlySettings>("VettlySettings");
        await Vettly.Initialize(settings);

        // Option 2: Initialize from Resources folder
        // (expects VettlySettings.asset in Resources folder)
        await Vettly.InitializeFromResources();
    }
}

3. Moderate Text Content

csharp
using Vettly;

public class ChatManager : MonoBehaviour
{
    public async void SendMessage(string playerId, string message)
    {
        var result = await Vettly.ModerateTextAsync(playerId, message);

        if (result.IsAllowed)
        {
            // Content is safe - send the message
            BroadcastMessage(message);
        }
        else if (result.IsBlocked)
        {
            // Content is blocked
            ShowWarning($"Message blocked: {result.Reason}");
        }
        else if (result.NeedsReview)
        {
            // Content needs manual review - queue it
            QueueForReview(playerId, message);
        }
    }
}

API Reference

Initialization

csharp
// Initialize with settings asset
Task Vettly.Initialize(VettlySettings settings)

// Initialize from Resources/VettlySettings.asset
Task Vettly.InitializeFromResources()

Text Moderation

csharp
Task<VettlyResult> Vettly.ModerateTextAsync(
    string userId,           // Player/user identifier
    string content,          // Text content to moderate
    string locale = "en",    // Content locale
    string context = "",     // Additional context
    string policy = null     // Override policy preset
)

VettlyResult

PropertyTypeDescription
DecisionDecisionAllow, Block, or Review
ReasonstringModeration reason
Confidencefloat0-1 confidence score
ExplanationstringHuman-readable explanation
RequestIdstringRequest identifier for support
FromFallbackboolTrue if result is from fallback
IsAllowedboolConvenience: Decision == Allow
IsBlockedboolConvenience: Decision == Block
NeedsReviewboolConvenience: Decision == Review

Decision Enum

csharp
public enum Decision
{
    Allow,   // Content is safe
    Block,   // Content is blocked
    Review   // Content needs manual review
}

VettlySettings Properties

PropertyTypeDefaultDescription
ApiKeystring-Your Vettly API key
EnvironmentEnvironmentProductionProduction or Sandbox
BaseUrlOverridestring-Optional custom API endpoint
FailOpenbooltrueAllow content when API fails
TimeoutSecondsint30Request timeout in seconds
PolicyPresetstring"default"Policy preset name

Editor Tools

Test Window

Access via Tools > Vettly > Test Window to test moderation directly in the Unity Editor.

  1. Select your VettlySettings asset
  2. Enter test content and player ID
  3. Click "Test Moderation" to see results

This is useful for:

  • Testing your API key configuration
  • Validating policy behavior
  • Debugging moderation decisions

Examples

Chat System

csharp
using UnityEngine;
using Vettly;

public class ChatSystem : MonoBehaviour
{
    [SerializeField] private VettlySettings vettlySettings;

    private async void Start()
    {
        await Vettly.Initialize(vettlySettings);
    }

    public async void OnPlayerSubmitChat(string playerId, string message)
    {
        // Show "checking..." indicator
        ShowModerationPending();

        var result = await Vettly.ModerateTextAsync(
            playerId,
            message,
            locale: "en",
            context: "in-game chat"
        );

        if (result.IsAllowed)
        {
            SendChatMessage(playerId, message);
        }
        else
        {
            // Log for analytics
            Debug.Log($"Blocked message from {playerId}: {result.Reason}");

            // Show user-friendly feedback
            ShowModerationFeedback("Your message couldn't be sent.");
        }
    }
}

Player Profile Names

csharp
using UnityEngine;
using Vettly;

public class ProfileManager : MonoBehaviour
{
    public async Task<bool> ValidateDisplayName(string playerId, string displayName)
    {
        var result = await Vettly.ModerateTextAsync(
            playerId,
            displayName,
            context: "display name"
        );

        if (!result.IsAllowed)
        {
            Debug.LogWarning($"Display name rejected: {result.Reason}");
            return false;
        }

        return true;
    }
}

With Error Handling

csharp
using UnityEngine;
using Vettly;

public class RobustModeration : MonoBehaviour
{
    public async Task<bool> ModerateWithFallback(string playerId, string content)
    {
        var result = await Vettly.ModerateTextAsync(playerId, content);

        // Check if result came from fallback (API failed)
        if (result.FromFallback)
        {
            Debug.LogWarning("Moderation API unavailable, using fallback");
            // Optionally queue for later review
            QueueForLaterReview(playerId, content);
        }

        return result.IsAllowed;
    }
}

Security Best Practices

API Key Security

Do not ship long-lived API keys in production builds. Players can extract keys from game binaries.

For production games:

  1. Use a backend proxy: Route moderation requests through your game server
  2. Use temporary tokens: Fetch short-lived tokens from your backend
  3. Implement rate limiting: Prevent abuse from modified clients

Example backend proxy approach:

csharp
// Client-side: Call your backend instead of Vettly directly
public async Task<ModerationResult> ModerateViaBackend(string content)
{
    var request = new UnityWebRequest(
        "https://your-game-server.com/api/moderate",
        "POST"
    );
    // ... your backend then calls Vettly with the real API key
}

Troubleshooting

Common Issues

"API key not set"

  • Ensure VettlySettings asset has a valid API key
  • Check that settings are properly loaded

"Request timed out"

  • Increase TimeoutSeconds in settings
  • Check player's internet connection

"Invalid response"

  • Verify API key is correct
  • Check if using correct environment (Production vs Sandbox)

Debug Logging

The SDK logs errors to Unity's Debug console:

csharp
// Errors logged as:
Debug.LogError($"Vettly API error: {errorMessage}");
Debug.LogError($"Vettly client error: {exception.Message}");

Next Steps