Repository with examples on how to implement TwitchLib within Unity. This project is a WIP and is still being updated.
A repository which contains a Unity project with a few example implementations and Unity Unit Tests. The actual TwitchLib project can be found here
Below are basic examples of how to utilize TwitchLib.Unity libraries including Api, Chat Client and PubSub
// If type or namespace TwitchLib could not be found. Make sure you add the latest TwitchLib.Unity.dll to your project folder// Download it here: https://github.com/TwitchLib/TwitchLib.Unity/releases// Or download the repository at https://github.com/TwitchLib/TwitchLib.Unity, build it, and copy the TwitchLib.Unity.dll from the output directoryusingTwitchLib.Client.Models;usingTwitchLib.Unity;usingUnityEngine;publicclassTwitchClientExample:MonoBehaviour{[SerializeField]//[SerializeField] Allows the private field to show up in Unity's inspector. Way better than just making it publicprivatestring_channelToConnectTo=Secrets.USERNAME_FROM_OAUTH_TOKEN;privateClient_client;privatevoidStart(){// To keep the Unity application active in the background, you can enable "Run In Background" in the player settings:// Unity Editor --> Edit --> Project Settings --> Player --> Resolution and Presentation --> Resolution --> Run In Background// This option seems to be enabled by default in more recent versions of Unity. An aditional, less recommended option is to set it in code:// Application.runInBackground = true;//Create Credentials instanceConnectionCredentialscredentials=newConnectionCredentials(Secrets.USERNAME_FROM_OAUTH_TOKEN,Secrets.OAUTH_TOKEN);// Create new instance of Chat Client_client=newClient();// Initialize the client with the credentials instance, and setting a default channel to connect to._client.Initialize(credentials,_channelToConnectTo);// Bind callbacks to events_client.OnConnected+=OnConnected;_client.OnJoinedChannel+=OnJoinedChannel;_client.OnMessageReceived+=OnMessageReceived;_client.OnChatCommandReceived+=OnChatCommandReceived;// Connect_client.Connect();}privatevoidOnConnected(objectsender,TwitchLib.Client.Events.OnConnectedArgse){Debug.Log($"The bot {e.BotUsername} succesfully connected to Twitch.");if(!string.IsNullOrWhiteSpace(e.AutoJoinChannel))Debug.Log($"The bot will now attempt to automatically join the channel provided when the Initialize method was called: {e.AutoJoinChannel}");}privatevoidOnJoinedChannel(objectsender,TwitchLib.Client.Events.OnJoinedChannelArgse){Debug.Log($"The bot {e.BotUsername} just joined the channel: {e.Channel}");_client.SendMessage(e.Channel,"I just joined the channel! PogChamp");}privatevoidOnMessageReceived(objectsender,TwitchLib.Client.Events.OnMessageReceivedArgse){Debug.Log($"Message received from {e.ChatMessage.Username}: {e.ChatMessage.Message}");}privatevoidOnChatCommandReceived(objectsender,TwitchLib.Client.Events.OnChatCommandReceivedArgse){switch(e.Command.CommandText){case"hello":_client.SendMessage(e.Command.ChatMessage.Channel,$"Hello {e.Command.ChatMessage.DisplayName}!");break;case"about":_client.SendMessage(e.Command.ChatMessage.Channel,"I am a Twitch bot running on TwitchLib!");break;default:_client.SendMessage(e.Command.ChatMessage.Channel,$"Unknown chat command: {e.Command.CommandIdentifier}{e.Command.CommandText}");break;}}privatevoidUpdate(){// Don't call the client send message on every Update,// this is sample on how to call the client,// not an example on how to code.if(Input.GetKeyDown(KeyCode.Space)){_client.SendMessage(_channelToConnectTo,"I pressed the space key within Unity.");}}}usingSystem.Collections;usingSystem.Collections.Generic;// If type or namespace TwitchLib could not be found. Make sure you add the latest TwitchLib.Unity.dll to your project folder// Download it here: https://github.com/TwitchLib/TwitchLib.Unity/releases// Or download the repository at https://github.com/TwitchLib/TwitchLib.Unity, build it, and copy the TwitchLib.Unity.dll from the output directoryusingTwitchLib.Unity;usingUnityEngine;publicclassTwitchApiExample:MonoBehaviour{[SerializeField]//[SerializeField] Allows the private field to show up in Unity's inspector. Way better than just making it publicprivatestring_usernameToGetChannelVideosFrom="LuckyNoS7evin";privateApi_api;privatevoidStart(){// To keep the Unity application active in the background, you can enable "Run In Background" in the player settings:// Unity Editor --> Edit --> Project Settings --> Player --> Resolution and Presentation --> Resolution --> Run In Background// This option seems to be enabled by default in more recent versions of Unity. An aditional, less recommended option is to set it in code:// Application.runInBackground = true;// Create new instance of Api_api=newApi();// The api needs a ClientID or an OAuth token to start making calls to the api.// Set the client id_api.Settings.ClientId=Secrets.CLIENT_ID;// Set the oauth token.// Most requests don't require an OAuth token, in which case setting a client id would be sufficient.// Some requests require an OAuth token with certain scopes. Make sure your OAuth token has these scopes or the request will fail._api.Settings.AccessToken=Secrets.OAUTH_TOKEN;}privatevoidUpdate(){// Don't call the Api on every Update, this is sample on how to call the Api,// This is not an example on how to code.if(Input.GetKeyDown(KeyCode.Alpha1))// Alpha1 = The number 1 key on your keyboard.{// Do what you want here, however if you want to call the twitch API this can be done as follows.// The following example is the GetChannelVideos if you want to call any TwitchLib.Api// endpoint replace the the following with your method call "_api.Channels.v5.GetChannelVideosAsync("{{CHANNEL_ID}}");"_api.Invoke(_api.Channels.v5.GetChannelVideosAsync("14900522"),GetChannelVideosCallback);}if(Input.GetKeyDown(KeyCode.Alpha2))// Alpha2 = The number 2 key on your keyboard.{if(string.IsNullOrWhiteSpace(_usernameToGetChannelVideosFrom))thrownewSystem.Exception($"The provided channel doesn't exist: {_usernameToGetChannelVideosFrom}");StartCoroutine(GetChannelVideosByUsername(_usernameToGetChannelVideosFrom));}}privateIEnumeratorGetChannelVideosByUsername(stringusernameToGetChannelVideosFrom){// Lets get Lucky's id firstTwitchLib.Api.Models.Helix.Users.GetUsers.GetUsersResponsegetUsersResponse=null;yieldreturn_api.InvokeAsync(_api.Users.helix.GetUsersAsync(logins:newList<string>{usernameToGetChannelVideosFrom}),(response)=>getUsersResponse=response);// We won't reach this point until the api request is completed, and the getUsersResponse is set.// We'll assume the request went well and that we made no typo's, meaning we should have 1 user at index 0, which is LuckyNoS7evinstringluckyId=getUsersResponse.Users[0].Id;// Now that we have lucky's id, lets get his videos!TwitchLib.Api.Models.v5.Channels.ChannelVideoschannelVideos=null;yieldreturn_api.InvokeAsync(_api.Channels.v5.GetChannelVideosAsync(luckyId),(response)=>channelVideos=response);// Again, we won't reach this point until the request is completed!// Handle user's ChannelVideos// Using this way of calling the api, we still have access to usernameToGetChannelVideosFrom!varlistOfVideoTitles=GetListOfVideoTitles(channelVideos);varprintableListOfVideoTitles=string.Join(" | ",listOfVideoTitles);Debug.Log($"Videos from user {usernameToGetChannelVideosFrom}: {printableListOfVideoTitles}");}privatevoidGetChannelVideosCallback(TwitchLib.Api.Models.v5.Channels.ChannelVideose){varlistOfVideoTitles=GetListOfVideoTitles(e);varprintableListOfVideoTitles=string.Join(" | ",listOfVideoTitles);Debug.Log($"Videos from 14900522: {printableListOfVideoTitles}");}privateList<string>GetListOfVideoTitles(TwitchLib.Api.Models.v5.Channels.ChannelVideoschannelVideos){List<string>videoTitles=newList<string>();foreach(varvideoinchannelVideos.Videos)videoTitles.Add(video.Title);returnvideoTitles;}}usingTwitchLib.Unity;usingUnityEngine;publicclassTwitchPubSubExample:MonoBehaviour{privatePubSub_pubSub;privatevoidStart(){// To keep the Unity application active in the background, you can enable "Run In Background" in the player settings:// Unity Editor --> Edit --> Project Settings --> Player --> Resolution and Presentation --> Resolution --> Run In Background// This option seems to be enabled by default in more recent versions of Unity. An aditional, less recommended option is to set it in code:// Application.runInBackground = true;// Create new instance of PubSub Client_pubSub=newPubSub();// Subscribe to Events_pubSub.OnWhisper+=OnWhisper;_pubSub.OnPubSubServiceConnected+=OnPubSubServiceConnected;// Connect_pubSub.Connect();}privatevoidOnPubSubServiceConnected(objectsender,System.EventArgse){Debug.Log("PubSubServiceConnected!");// On connect listen to Bits evadsent// Please note that listening to the whisper events requires the chat_login scope in the OAuth token._pubSub.ListenToWhispers(Secrets.CHANNEL_ID_FROM_OAUTH_TOKEN);// SendTopics accepts an oauth optionally, which is necessary for some topics, such as bit events._pubSub.SendTopics(Secrets.OAUTH_TOKEN);}privatevoidOnWhisper(objectsender,TwitchLib.PubSub.Events.OnWhisperArgse){Debug.Log($"{e.Whisper.Data}");// Do your bits logic here.}}publicstaticclassSecrets{publicconststringCLIENT_ID="CLIENT_ID";//Your application's client ID, register one at https://dev.twitch.tv/dashboardpublicconststringOAUTH_TOKEN="OAUTH_TOKEN";//A Twitch OAuth token which can be used to connect to the chatpublicconststringUSERNAME_FROM_OAUTH_TOKEN="USERNAME_FROM_OAUTH_TOKEN";//The username which was used to generate the OAuth tokenpublicconststringCHANNEL_ID_FROM_OAUTH_TOKEN="CHANNEL_ID_FROM_OAUTH_TOKEN";//The channel Id from the account which was used to generate the OAuth token}