Thanks for using UniTwitch.
This project relies on using WebSockets, a WebSocketSharp library and a Thread dispatcher library.
UnityMainThreadDispatcher: Author: Pim de Witte (pimdewitte.com) and contributors https://github.com/PimDeWitte/UnityMainThreadDispatcher
WebSocketSharp: sta/websocket-sharp.
- Import the project.
- Create an empty object to be your Twitch Handler (from now on "TwitchHandler").
- Add the TwitchChatHandler component to your TwitchHandler object
- This will add a UnityMainThreadDispatcher component, used to handle async commands.
- The TwitchChatHandler will have
- Listen to whispers: Allow direct messages to your bot to be parsed.
- Listen To Public Chat: Allows public chat messages to be parsed.
- Enable Help: Allows the usage of "!help" (configurable) and will LOG this (future state is to send it to such chat.)
- Configuration: The twitch connection configuration
- Twitch Chat Callback Handler: A MonoBehaviour Component that handles IRC methods
- Reconnect
- OnMessage 3: OnError
- OnConnect
- OnClose
- Commands (Array): List of configured commands. Size is 0 by default.
- Help Command: The key message that users must send to get the help command to get executed if enabled (see III). Default is !help.
- Create a new asset through Assets>Create>LopapaGames>Twitch>TwitchConfiguration
- OAuth: the Chat OAuth key, generated through https://twitchapps.com/tmi/ this is NOT the application OAuth key (not even sure if that one works too, e. g. oauth:01a1abc1abc0a0abc0abc01ab0abc0 )
- Bot Name: the username that the script will behave as (matching above's account, e. g. porfiriopartida_bot)
- Channel Name: The channel to scan from. e. g. porfiriopartida
- Create a new empty GameObject and add the DemoTwitchCallback component to it (name it CallbacksObject).
- Hover the TwitchHandler GameObject on your scene
- Attach the new configuration in the Configuration field (created on step 4)
- Attach the CallbacksObject to the handler's Twitch Chat Callback Handler (for custom callback, see below)
- If you want to customize your IRC callbacks, create a new Script that inherits from TwitchChatCallbackHandler (which inherits from MonoBehaviour), for an example, see: Assets/LopapaGames/Demo/Scripts/DemoTwitchCallback.cs
- Replace the component added in step #5 using your own script instead.
Congratulations, you are all setup. You should be able to run the scene without errors, it will connect and listen for messages. You can send messages and see the Console receive those messages.
There are some extra steps to fully customize your scene handling.
- Create a new Scriptable Object Script that inherits from ACommand, see Assets/LopapaGames/Demo/ScriptableObjects/Scripts/SampleCommand.cs
- Make sure to have it as with the CreateAssetMenu handler so you can create it from the menu, example:
[CreateAssetMenu(fileName = "SampleCommand", menuName = "LopapaGames/Twitch/Sample/Command")]
- Your Execute implementation will receive the user name that executed the command as well as the full message sent (including the command)
- Make sure to have it as with the CreateAssetMenu handler so you can create it from the menu, example:
- Create a ScriptableObject through the Assets > and the path defined in your menuName, example:
- Assets > Create > LopapaGames > Twitch > Sample > Command
- It will create a new object given the name you used as fileName, e. g. SampleCommand
- Hover your new scriptable object in your assets
- You will have at least the Command Key, which is the command to be triggered: e. g. !hide
- If you added more public serializable fields, they will also show there, e.g. the Demo Sample Command will also have a Set Active boolean to be selected.
- Hover the TwitchHandler game object in the scene.
- In the Commands Array, put a new Size, from 0 to 1 (you can add as many as you have created above.)
- A new Element 0 will be displayed if you added Size 1. This accepts an ACommand script (created in step 2)
- Drag the scriptable object on top of the first element of the Commands list (Element 0).
- In the Commands Array, put a new Size, from 0 to 1 (you can add as many as you have created above.)
Hit the play button and go to the scanned chat. Send !hide (assuming this was your key command) and your Scriptable Object's Execute will be invoked (enqueued to be invoked actually)
In our SampleCommand example, it will execute a Singleton's function using the Set Active attribute.
DemoUIManager.Instance.textElement.SetActive(setActive);
You can pass the message to be parsed and do something more interesting.