projectviola / Unity-Twitch-Eventsub

Provides a simple integration of Twitch's EventSub API for Unity projects with minimal dependency. It's not using TWITCH LIB.
https://www.twitch.tv/project_viola
0 stars 0 forks source link
eventsub twitch twitch-api unity unity3d unity3d-plugin websocket

Twitch EventSub Unity Integration

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.

Dependencies

Features

Setup

  1. 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.
  2. Add the TwitchManager script to a GameObject in your Unity scene.
  3. Set your Twitch application's Client ID in the TwitchManager component in the Inspector.

Usage

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);
    }
}

Mock Server Testing

To test your integration without connecting to the live Twitch API, you can use the Twitch CLI mock server:

  1. Set up and run the Twitch CLI mock server.
  2. 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:

  1. You need to first install Twitch CLI.
  2. Configure your Twitch CLI by setting your Client ID and Client Secret.
    • Initiate the setup process by using "twitch configure" command.
  3. Start your server with WebSocket functionality.
twitch event websocket start-server
  1. You can use event triggers to test your code.

Example:

twitch event trigger channel.subscribe --transport=websocket

Customization

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.

Limitations

License

This project is open-source and free to use in your own projects without attribution.

Disclaimer

This is not an official Twitch product. Use at your own risk and ensure compliance with Twitch's Developer Agreement and Terms of Service.