Unity Integration
Add content moderation to your Unity games with the Vettly Unity SDK.
Installation
- Open Unity Package Manager (
Window > Package Manager) - Click the
+icon and select "Add package from git URL" - 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:
| Setting | Description |
|---|---|
| API Key | Your Vettly API key |
| Environment | Production or Sandbox |
| Fail Open | Allow content when API fails (recommended: true) |
| Timeout Seconds | Request timeout (default: 30) |
| Policy Preset | Policy 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
| Property | Type | Description |
|---|---|---|
Decision | Decision | Allow, Block, or Review |
Reason | string | Moderation reason |
Confidence | float | 0-1 confidence score |
Explanation | string | Human-readable explanation |
RequestId | string | Request identifier for support |
FromFallback | bool | True if result is from fallback |
IsAllowed | bool | Convenience: Decision == Allow |
IsBlocked | bool | Convenience: Decision == Block |
NeedsReview | bool | Convenience: Decision == Review |
Decision Enum
csharp
public enum Decision
{
Allow, // Content is safe
Block, // Content is blocked
Review // Content needs manual review
}VettlySettings Properties
| Property | Type | Default | Description |
|---|---|---|---|
ApiKey | string | - | Your Vettly API key |
Environment | Environment | Production | Production or Sandbox |
BaseUrlOverride | string | - | Optional custom API endpoint |
FailOpen | bool | true | Allow content when API fails |
TimeoutSeconds | int | 30 | Request timeout in seconds |
PolicyPreset | string | "default" | Policy preset name |
Editor Tools
Test Window
Access via Tools > Vettly > Test Window to test moderation directly in the Unity Editor.
- Select your VettlySettings asset
- Enter test content and player ID
- 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:
- Use a backend proxy: Route moderation requests through your game server
- Use temporary tokens: Fetch short-lived tokens from your backend
- 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
TimeoutSecondsin 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
- Moderation Policies - Configure what to block
- Best Practices - Production recommendations
- Error Handling - Handle edge cases
