A very simple plugin to connect your Unity games to the Twitch chat. It's possible to Login with just one line of code and start reading the messages. Custom settings for different environments and more complex login methods are also supported.
- In Unity, navigate to
Window
->PackageManager
- In the Package Manager Window, click on the
+
sign in the top left corner and click onAdd Package from GIT url
- Type the repository URL in the text box:
https://github.com/rothiotome/very-simple-twitch-chat.git
Import VerySimpleTwitchChat namespace in each class/script you want to use it:
using VerySimpleTwitchChat
This is the easiest way to use the plugin. You can use TwitchChat.Login("Channel_name")
to connect to the channel without needing a token or any settings customization.
using UnityEngine;
using VerySimpleTwitchChat;
public class TestingChat : MonoBehaviour
{
string channelName = "RothioTome";
void Start()
{
TwitchChat.Login(channelName);
}
}
To customize the default settings got to Window
-> Twitch Chat
-> Setup
. This will create a defaultTwitchChatSettings
file in your Resources
folder that will be used unless a custom one is specified.
It's possible to create new custom Twitch settings directly in your project to have multiple profiles. To create a new profile, right click in the Project window -> Create
-> TwitchChatSettings
. You can then use the custom Twitch Settings reference as an argument to the Login method.
using System.Collections.Generic;
using UnityEngine;
using VerySimpleTwitchChat;
public class TestingChat : MonoBehaviour
{
[SerializeField] TwitchChatSettings customSettings;
string channelName = "RothioTome";
void Start()
{
TwitchChat.Login(channelName, customSettings);
}
}
To send messages the Twitch chat, you need writting permissions. Anonymous connection is not allowed, so you need to create a new TwitchLoginInfo
with a token that has writting permissions.
using System.Collections.Generic;
using UnityEngine;
using VerySimpleTwitchChat;
public class TestingChat : MonoBehaviour
{
string channelName = "RothioTome";
void Start()
{
TwitchChat.Login(channelName, new TwitchLoginInfo("Username", "OauthToken"));
}
}
Once you are connection, you can send messages using the TwitchChat.SendChatMessage("message")
static method.
To receive the Twitch chat messages, subscribe to the onTwitchMessageReceived
static delegates.
using System.Collections.Generic;
using UnityEngine;
using VerySimpleTwitchChat;
public class TestingChat : MonoBehaviour
{
void Start()
{
TwitchChat.onTwitchMessageReceived += OnMessageReceived;
}
private void OnDestroy()
{
TwitchChat.onTwitchMessageReceived -= OnMessageReceived;
}
private void OnMessageReceived(Chatter chatter)
{
Debug.Log($"Message received from <color={chatter.tags.colorHex}>{chatter.tags.displayName}</color> : {chatter.message}");
}
}
To send chat messages you can use the TwitchChat.SendChatMessage("message")
static method. Sending chat messages is only available when you use OAuth connection method with a Token that has writting permissions.
Very Simple Twitch Chat will automatically join the channel you use in the login method. However, this plugin supports multi channel join, so you will be able to Join a second channel (and virtually infinite of them) by using the TwitchChat.JoinChannel("channelName")
method. To know if a message has been received in your main channel or another one, you can use the method Chatter.IsFromChannel("channelName")
. To leave a channel you've previously joined, you can use the method TwitchChat.LeaveChannel("channelName)
.
Note: Sometimes the Channel Joined event occurs a few seconds before the messages start arriving. Please be aware of the delay
Error adding package: https://github.com/rothiotome/very-simple-twitch-chat.git. Unable to add package
A GIT client installed on your computer is required to add a Unity package using the Git address. You can download the Git client from git-scm.com.
If you don't want to install the Git client you can clone/download the repository to a specific folder and then add that folder to the package manager.
This project is released under the MIT License by RothioTome (2023)