-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathTwitchChatExample.cs
72 lines (68 loc) · 2.78 KB
/
TwitchChatExample.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[RequireComponent(typeof(TwitchIRC))]
public class TwitchChatExample : MonoBehaviour
{
public int maxMessages = 100; //we start deleting UI elements when the count is larger than this var.
private LinkedList<GameObject> messages =
new LinkedList<GameObject>();
public UnityEngine.UI.InputField inputField;
public UnityEngine.UI.Button submitButton;
public UnityEngine.RectTransform chatBox;
public UnityEngine.UI.ScrollRect scrollRect;
private TwitchIRC IRC;
//when message is recieved from IRC-server or our own message.
void OnChatMsgRecieved(string msg)
{
//parse from buffer.
int msgIndex = msg.IndexOf("PRIVMSG #");
string msgString = msg.Substring(msgIndex + IRC.channelName.Length + 11);
string user = msg.Substring(1, msg.IndexOf('!') - 1);
//remove old messages for performance reasons.
if (messages.Count > maxMessages)
{
Destroy(messages.First.Value);
messages.RemoveFirst();
}
//add new message.
CreateUIMessage(user, msgString);
}
void CreateUIMessage(string userName, string msgString)
{
Color32 c = ColorFromUsername(userName);
string nameColor = "#" + c.r.ToString("X2") + c.g.ToString("X2") + c.b.ToString("X2");
GameObject go = new GameObject("twitchMsg");
var text = go.AddComponent<UnityEngine.UI.Text>();
var layout = go.AddComponent<UnityEngine.UI.LayoutElement>();
go.transform.SetParent(chatBox);
messages.AddLast(go);
layout.minHeight = 20f;
text.text = "<color=" + nameColor + "><b>" + userName + "</b></color>" + ": " + msgString;
text.color = Color.black;
text.font = Resources.GetBuiltinResource(typeof(Font), "Arial.ttf") as Font;
scrollRect.velocity = new Vector2(0, 1000f);
}
//when Submit button is clicked or ENTER is pressed.
public void OnSubmit()
{
if (inputField.text.Length > 0)
{
IRC.SendMsg(inputField.text); //send message.
CreateUIMessage(IRC.nickName, inputField.text); //create ui element.
inputField.text = "";
}
}
Color ColorFromUsername(string username)
{
Random.seed = username.Length + (int)username[0] + (int)username[username.Length - 1];
return new Color(Random.Range(0.25f, 0.55f), Random.Range(0.20f, 0.55f), Random.Range(0.25f, 0.55f));
}
// Use this for initialization
void Start()
{
IRC = this.GetComponent<TwitchIRC>();
//IRC.SendCommand("CAP REQ :twitch.tv/tags"); //register for additional data such as emote-ids, name color etc.
IRC.messageRecievedEvent.AddListener(OnChatMsgRecieved);
}
}