From 766cdff5ffa72b65d7f106658d1603f47739b2ba Mon Sep 17 00:00:00 2001 From: chai <215380520@qq.com> Date: Fri, 27 Oct 2023 11:05:14 +0800 Subject: + init --- GameCode/TwitchUIHandler.cs | 154 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 GameCode/TwitchUIHandler.cs (limited to 'GameCode/TwitchUIHandler.cs') diff --git a/GameCode/TwitchUIHandler.cs b/GameCode/TwitchUIHandler.cs new file mode 100644 index 0000000..c69fc7a --- /dev/null +++ b/GameCode/TwitchUIHandler.cs @@ -0,0 +1,154 @@ +using System; +using Irc; +using Steamworks; +using TMPro; +using UnityEngine; +using UnityEngine.UI; + +public class TwitchUIHandler : MonoBehaviour +{ + private const string TWITCH_OAUTH_PLAYERPREF_KEY = "TwitchOauth"; + + private const string TWITCH_NAME_PLAYERPREF_KEY = "TwitchName"; + + [SerializeField] + private TMP_InputField m_UserNameText; + + [SerializeField] + private TMP_InputField m_OauthText; + + [SerializeField] + private Button m_GetOAuthButton; + + [SerializeField] + private ListMenuPage m_TwitchBar; + + private Action m_OnMsgAction; + + private bool m_Connected; + + public static string OAUTH_KEY + { + get + { + return PlayerPrefs.GetString("TwitchOauth" + SteamUser.GetSteamID().ToString(), string.Empty); + } + private set + { + PlayerPrefs.SetString("TwitchOauth" + SteamUser.GetSteamID().ToString(), value); + } + } + + public static string TWITCH_NAME_KEY + { + get + { + return PlayerPrefs.GetString("TwitchName" + SteamUser.GetSteamID().ToString(), string.Empty); + } + private set + { + PlayerPrefs.SetString("TwitchName" + SteamUser.GetSteamID().ToString(), value); + } + } + + public static TwitchUIHandler Instance { get; private set; } + + private void Awake() + { + Instance = this; + InitListeners(); + } + + private void Start() + { + TwitchIrc instance = TwitchIrc.Instance; + instance.OnChannelMessage = (ChannelMessage)Delegate.Combine(instance.OnChannelMessage, new ChannelMessage(OnChannelMessage)); + TwitchIrc instance2 = TwitchIrc.Instance; + instance2.OnUserLeft = (UserLeft)Delegate.Combine(instance2.OnUserLeft, new UserLeft(OnUserLeft)); + TwitchIrc instance3 = TwitchIrc.Instance; + instance3.OnUserJoined = (UserJoined)Delegate.Combine(instance3.OnUserJoined, new UserJoined(OnUserJoined)); + TwitchIrc instance4 = TwitchIrc.Instance; + instance4.OnServerMessage = (ServerMessage)Delegate.Combine(instance4.OnServerMessage, new ServerMessage(OnServerMessage)); + TwitchIrc instance5 = TwitchIrc.Instance; + instance5.OnExceptionThrown = (ExceptionThrown)Delegate.Combine(instance5.OnExceptionThrown, new ExceptionThrown(OnExceptionThrown)); + if (!string.IsNullOrEmpty(OAUTH_KEY)) + { + m_OauthText.text = OAUTH_KEY; + } + if (!string.IsNullOrEmpty(TWITCH_NAME_KEY)) + { + m_UserNameText.text = TWITCH_NAME_KEY; + } + } + + private void InitListeners() + { + m_GetOAuthButton.onClick.AddListener(GetOauth); + } + + public void AddMsgAction(Action a) + { + m_OnMsgAction = (Action)Delegate.Combine(m_OnMsgAction, a); + } + + public void OnContinueClick() + { + OAUTH_KEY = m_OauthText.text; + TWITCH_NAME_KEY = m_UserNameText.text.ToLower(); + TwitchIrc.Instance.Username = TWITCH_NAME_KEY; + TwitchIrc.Instance.OauthToken = OAUTH_KEY; + TwitchIrc.Instance.Channel = TWITCH_NAME_KEY; + TwitchIrc.Instance.Connect(); + } + + private void GetOauth() + { + Application.OpenURL("http://twitchapps.com/tmi/"); + } + + public void MessageSend() + { + } + + public void GoUrl(string url) + { + Application.OpenURL(url); + } + + private void OnServerMessage(string message) + { + Debug.Log(message); + } + + private void OnChannelMessage(ChannelMessageEventArgs channelMessageArgs) + { + m_OnMsgAction?.Invoke(channelMessageArgs.Message, channelMessageArgs.From); + } + + private void OnUserJoined(UserJoinedEventArgs userJoinedArgs) + { + if (userJoinedArgs.User.ToUpper() == TwitchIrc.Instance.Username.ToUpper()) + { + Debug.Log("LOCAL USER JOINED!"); + ConnectedToTwitch(); + } + } + + private void ConnectedToTwitch() + { + if (!m_Connected) + { + m_Connected = true; + m_TwitchBar.Open(); + } + } + + private void OnUserLeft(UserLeftEventArgs userLeftArgs) + { + } + + private void OnExceptionThrown(Exception exeption) + { + Debug.Log(exeption); + } +} -- cgit v1.1-26-g67d0