Back to Documentation

Unity SDK Examples

Real-world examples showing how to use the OddSockets Unity SDK in production games and applications

View Unity Examples
Basic Chat System
Available
Simple example showing how to create a basic chat system in Unity using coroutines and the OddSockets Unity SDK.
using UnityEngine;
using OddSockets.Unity;

public class ChatManager : MonoBehaviour
{
    private OddSocketsUnityClient client;
    private OddSocketsUnityChannel chatChannel;

    void Start()
    {
        var config = new OddSocketsUnityConfig
        {
            ApiKey = "ak_live_1234567890abcdef",
            UserId = "player123",
            AutoConnect = true
        };

        client = new OddSocketsUnityClient(config);
        client.OnConnected += OnConnected;
        
        StartCoroutine(client.ConnectAsync());
    }

    private void OnConnected()
    {
        chatChannel = client.Channel("game-chat");
        StartCoroutine(chatChannel.SubscribeAsync(OnMessage));
    }

    private void OnMessage(OddSocketsMessage message)
    {
        Debug.Log($"Chat: {message.Data}");
    }
}
View Docs
Multiplayer Game State
Available
Real-time multiplayer game state synchronization with player positions, actions, and game events.
using UnityEngine;
using OddSockets.Unity;

public class MultiplayerManager : MonoBehaviour
{
    [SerializeField] private OddSocketsUnityConfig config;
    private OddSocketsUnityClient client;
    private OddSocketsUnityChannel gameChannel;

    void Start()
    {
        client = new OddSocketsUnityClient(config);
        client.OnConnected += OnConnected;
        StartCoroutine(client.ConnectAsync());
    }

    private void OnConnected()
    {
        gameChannel = client.Channel("game-room-1");
        StartCoroutine(gameChannel.SubscribeAsync(OnGameEvent));
    }

    private void OnGameEvent(OddSocketsMessage message)
    {
        var gameData = message.GetData<GameEvent>();
        HandleGameEvent(gameData);
    }

    public void SendPlayerMove(Vector3 position)
    {
        var moveData = new { 
            type = "player_move", 
            position = position,
            playerId = client.UserId,
            timestamp = System.DateTime.UtcNow
        };
        
        StartCoroutine(gameChannel.PublishAsync(moveData));
    }
}
Configuration
Player Presence System
Available
Track online players, show who's in the game, and handle player join/leave events with presence tracking.
using UnityEngine;
using UnityEngine.UI;
using OddSockets.Unity;
using System.Collections.Generic;

public class PresenceManager : MonoBehaviour
{
    [SerializeField] private Text playerCountText;
    [SerializeField] private Transform playerListParent;
    
    private OddSocketsUnityClient client;
    private OddSocketsUnityChannel presenceChannel;
    private Dictionary<string, GameObject> playerUIElements;

    void Start()
    {
        playerUIElements = new Dictionary<string, GameObject>();
        
        var config = new OddSocketsUnityConfig
        {
            ApiKey = "ak_live_1234567890abcdef",
            UserId = $"player_{Random.Range(1000, 9999)}",
            AutoConnect = true
        };

        client = new OddSocketsUnityClient(config);
        client.OnConnected += OnConnected;
        StartCoroutine(client.ConnectAsync());
    }

    private void OnConnected()
    {
        presenceChannel = client.Channel("lobby");
        
        // Subscribe with presence enabled
        var options = new SubscriptionOptions
        {
            EnablePresence = true
        };
        
        StartCoroutine(presenceChannel.SubscribeAsync(OnMessage, options));
        
        // Handle presence changes
        presenceChannel.OnPresenceChange += OnPresenceChange;
    }

    private void OnPresenceChange(PresenceInfo presence)
    {
        playerCountText.text = $"Players Online: {presence.Occupancy}";
        UpdatePlayerList(presence.Occupants);
    }
}
Inspector Configuration
Available
Configure OddSockets directly in the Unity Inspector with serialized fields and tooltips for easy setup.
using UnityEngine;
using OddSockets.Unity;

public class OddSocketsManager : MonoBehaviour
{
    [Header("OddSockets Configuration")]
    [SerializeField] private OddSocketsUnityConfig config;
    
    [Header("Game Settings")]
    [SerializeField] private string roomName = "game-room";
    [SerializeField] private bool autoJoinRoom = true;
    
    [Header("Debug")]
    [SerializeField] private bool enableDebugLogs = true;
    
    private OddSocketsUnityClient client;
    private OddSocketsUnityChannel gameRoom;

    void Start()
    {
        // Use the config set up in the Inspector
        client = new OddSocketsUnityClient(config);
        
        if (enableDebugLogs)
        {
            client.OnConnected += () => Debug.Log("✅ Connected to OddSockets");
            client.OnDisconnected += (reason) => Debug.Log($"❌ Disconnected: {reason}");
            client.OnError += (error) => Debug.LogError($"🚨 Error: {error.Message}");
        }

        StartCoroutine(client.ConnectAsync());
        
        if (autoJoinRoom)
        {
            client.OnConnected += JoinGameRoom;
        }
    }

    private void JoinGameRoom()
    {
        gameRoom = client.Channel(roomName);
        StartCoroutine(gameRoom.SubscribeAsync(OnGameMessage));
        
        if (enableDebugLogs)
        {
            Debug.Log($"🎮 Joined room: {roomName}");
        }
    }

    private void OnGameMessage(OddSocketsMessage message)
    {
        if (enableDebugLogs)
        {
            Debug.Log($"📨 Received: {message.Data}");
        }
    }
}