Beispiel #1
0
        /// <summary>
        /// YoutubeライブのチャットIDを取得するコルーチン
        /// </summary>
        /// <returns>The chat identifier.</returns>
        private IEnumerator GetChatId()
        {
            // TODO: JsonStringFormatterクラスにchannelを作らせる
            string channel = youtubeAPIbase + channnelSearch + videoId + "&key=" + apikey;

            Debug.Log(channel);
            UnityWebRequest channelRequest = UnityWebRequest.Get(channel);

            yield return(channelRequest.SendWebRequest());

            if (channelRequest.isHttpError || channelRequest.isNetworkError)
            {
                Debug.LogError(channelRequest.error);
            }
            else
            {
                string[] jsonData = channelRequest.downloadHandler.text.Split(splitString, StringSplitOptions.None);

                Debug.Log("-----------------------");
                Debug.Log(channelRequest.downloadHandler.text);

                foreach (var json in jsonData)
                {
                    if (json.IndexOf("activeLiveChatId") <= 0)
                    {
                        Debug.LogWarning("This is not comment.");
                        continue;
                    }

                    chatId = StringFormatter.GetFormattedString(deleteString, json);
                    break;
                }

                StartCoroutine(this.GetComment());
            }
        }
Beispiel #2
0
        /// <summary>
        /// Youtubeライブのコメントを取得するコルーチン
        /// </summary>
        /// <returns>The comment.</returns>
        private IEnumerator GetComment()
        {
            //コルーチンが2つ以上にならないように止める
            StopCoroutine(this.InvokeWait());

            var commentURI = youtubeAPIbase + chatURIUp + chatId + pagetoken + nextPageTokenstr + chatURIbottom2 + apikey;

            Debug.Log("CommentURI : " + commentURI);

            UnityWebRequest connectCommentRequest = UnityWebRequest.Get(commentURI);

            yield return(connectCommentRequest.SendWebRequest());

            if (connectCommentRequest.isHttpError || connectCommentRequest.isNetworkError)
            {
                Debug.LogError(connectCommentRequest.error);
            }
            else
            {
                string[] jsonData = connectCommentRequest.downloadHandler.text.Split(splitString, StringSplitOptions.None);

                CommentParts comment = new CommentParts();
                foreach (var json in jsonData)
                {
                    StringFormatter.TextOutput(json);
                    if (json.IndexOf("nextPageToken") > 0)
                    {
                        string newToken = StringFormatter.GetFormattedString(deleteString, json);

                        Debug.Log("newToken : " + newToken);
                        Debug.Log("nextPageTokenstr : " + nextPageTokenstr);
                        if (nextPageTokenstr == newToken)
                        {
                            Debug.Log("Same Token.");
                        }
                        else
                        {
                            nextPageTokenstr = newToken;
                            Debug.Log("Change Token.");
                            continue;
                        }
                    }

                    if (json.IndexOf("displayMessage") > 0)
                    {
                        comment.displayMessage = StringFormatter.GetFormattedString(deleteString, json);
                        Debug.Log("displayMessage : " + comment.displayMessage);
                    }
                    if (json.IndexOf("displayName") > 0)
                    {
                        comment.displayName = StringFormatter.GetFormattedString(deleteString, json);
                        Debug.Log("displayName : " + comment.displayName);
                    }

                    if (comment.displayName != null && comment.displayMessage != null)
                    {
                        commentManager.GetComponent <CommentManager>().CommentUpdate(comment);
                        comment = new CommentParts();
                        yield return(null);
                    }
                }
                StartCoroutine(this.InvokeWait());
            }
        }