This project provides a simple integration of Twitch's EventSub API for Unity projects. It allows developers to easily incorporate Twitch events into their Unity games or applications.
- System.Net is required. (No WebGL support)
- NativeWebSocket
- Needed for WebSocket functionality.
- A native Unity Package.
- Use Unity's package manager to acquire the package.
- https://github.com/endel/NativeWebSocket.git
- Newtonsoft Json
- Added for advanced json serialization and deserialization functionality.
- A native Unity package.
- Use Unity's package manager to acquire the package.
- Easy setup with minimal configuration
- Supports Twitch events: Raids, Chat Messages, Subscriptions, Gifted Subs, and Follows
- Handles OAuth authentication flow
- Persistent credential storage
- Support for connecting to Twitch CLI mock server for testing
- Register to Twitch Developers and register your app.
- If you want to use Twitch CLI for testing. Make sure to choose "Client Type" as "Confidential" and generate your "Client Secret" for local mock server setup.
- Note that "Client Secret" is no longer needed for websocket API connection but needed for Twitch CLI to function.
- Add the
TwitchManager
script to a GameObject in your Unity scene. - Set your Twitch application's Client ID in the
TwitchManager
component in the Inspector.
The EventSubHandler
class handles incoming Twitch events. Modify this class to implement your desired functionality for each event type.
Example:
public class EventSubHandler : MonoBehaviour, IEventHandler
{
public void OnChatMessage(Event chatEvent)
{
// Implement other functionality here
Debug.Log($"Chat message from {chatEvent.chatter_user_name}: {chatEvent.message.text}");
}
}
Helix Message Functionality Example:
public class YourClass : MonoBehaviour
{
// Assign TwitchManager first
public TwitchManager TwitchManager;
public void SendTwitchChat(string message)
{
TwitchManager.SendChatMessage(message);
TwitchManager.SendAnnouncement(message);
}
}
To test your integration without connecting to the live Twitch API, you can use the Twitch CLI mock server:
- Set up and run the Twitch CLI mock server.
- In the Unity Inspector, find the
TwitchManager
component and enable the "Use Mock Server" toggle.
This will direct all API calls to the local mock server instead of the live Twitch API, allowing for easier testing and development.
To setup the server:
- You need to first install Twitch CLI.
- Configure your Twitch CLI by setting your Client ID and Client Secret.
- Initiate the setup process by using "twitch configure" command.
- Start your server with WebSocket functionality.
twitch event websocket start-server
- You can use event triggers to test your code.
Example:
twitch event trigger channel.subscribe --transport=websocket
Feel free to modify and extend the functionality to suit your project's needs. You can add support for additional Twitch events by updating the EventSubClient
and EventSubHandler
classes.
- Currently supports a limited set of Twitch events (Raids, Chat Messages, Subscriptions, Gifted Subs, Reward Redeems, Sending Messages and Follows)
- Requires a Twitch Developer application for authentication when not using the mock server
- System.net is required. There is no way to implement this package into a WebGL project.
This project is open-source and free to use in your own projects without attribution.
This is not an official Twitch product. Use at your own risk and ensure compliance with Twitch's Developer Agreement and Terms of Service.