【Unity】LINEっぽいチャットでScroll View チュートリアル③ChatController前編
Unity 2019.2.0f1 Personal(2019年8月)
前回の続きです〜
今回はチャットが動くようスクリプトを作成していきます。
Scroll ViewやAuto Layoutについては前回まででほぼ終わってしまいましたが…。
せっかくなのでちゃんと機能するところまで進めていきます〜
ChatController作成
CharSystem.csというスクリプトを作成し、
ChatSystemオブジェクトにつけましょう。
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class ChatSystem : MonoBehaviour { private int id = 0; [SerializeField] InputField chatInputField; [SerializeField] GameObject chatNodePrefab; [SerializeField] GameObject content; void Start() { } public void OnClickMineButton() { CreateChatNode(ChatRoll.MINE); } public void OnClickOthersButton() { CreateChatNode(ChatRoll.OTHERS); } private void CreateChatNode(ChatRoll roll) { id++; string str = chatInputField.text; chatInputField.text = ""; ChatData data = new ChatData(id, roll, str); Debug.Log("id:" + data.id + " roll:" + roll.ToString() + " body:" + str); } } public enum ChatRoll { MINE, OTHERS, } public class ChatData { public int id; public ChatRoll roll; public string body; public ChatData(int id, ChatRoll roll, string body) { this.id = id; this.roll = roll; this.body = body; } }
ChatSystem.cs
InputFieldから文字入力をしButtonを押すと、
ChatDataクラスを生成してid, roll, bodyを保存するスクリプトです。
各種設定
まずは各メンバ変数をInspectorから設定します。
ChatNodePrefabには前回作成したPrefabを設定します。
続いてMineButtonのButton>OnClickにOnClickMineButtonを、
OthersButtonのButton>OnClickにOnClickOthersButtonを設定します。
最後にInput FieldのLine TypeをMulti Line Newlineに設定し、
日本語入力ができるようにします。
実行
「こんにちは」と入力してMINEボタンを、
「さようなら」と入力してOTHERSボタンを押しました。
次回はChatNodeを生成してChatDataのデータを流し込んでいきます。
というわけで今回はここまでです、ありがとうございました〜