ãUnityãLINEã£ã½ããã£ããã§Scroll View ãã¥ã¼ããªã¢ã«â£ChatNode
Unity 2019.2.0f1 Personal(2019å¹´8æ)
LINEã£ã½ããã£ããã§Scroll Viewãã¥ã¼ããªã¢ã«ã
ã©ã¹ãã¯ChatNodeã¹ã¯ãªãããä½æãã¦ããã¾ãã
ChatNodeã¹ã¯ãªããä½æ
ã¾ãã¯ChatNode.csãä½æãã
以åPrefabåããChatNodeãªãã¸ã§ã¯ãã«ã¤ãã¦ããã¾ãã
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class ChatNode : MonoBehaviour { private ChatData chatData; [SerializeField] LayoutGroup layoutGroup; [SerializeField] Image chatBoard; [SerializeField] Text chatText; [SerializeField] Image chatIcon; [SerializeField] Sprite mineSprite; [SerializeField] Sprite othersSprite; public void Init(ChatData data) { chatData = data; chatText.text = chatData.body; if (chatData.roll == ChatRoll.MINE) { chatIcon.sprite = mineSprite; layoutGroup.childAlignment = TextAnchor.MiddleRight; chatBoard.color = new Color(0f, 1f, 0.1f); chatIcon.transform.SetSiblingIndex(1); } else if (chatData.roll == ChatRoll.OTHERS) { chatIcon.sprite = othersSprite; layoutGroup.childAlignment = TextAnchor.MiddleLeft; chatBoard.color = new Color(1f, 1f, 1f); chatIcon.transform.SetSiblingIndex(0); } }
ChatNode.cs
ã¡ã½ããã¯Initã®ã¿ã§ãã
ChatDataãåãåããrollã«ãã£ã¦ChatNodeã®å
容ãå¤æ´ãã¾ãã
spriteå¤æ´ãchildAlignmentã§å·¦å³ã®ãã¸ã·ã§ã³æ±ºããã«ã©ã¼å¤æ´ã
SetSiblingIndexã¯Hierarchyä¸ã§ã®ãªãã¸ã§ã¯ãã®ä¸¦ã³é ãã¹ã¯ãªããããå¤æ´ãã¾ãã
ChatSystemã¹ã¯ãªããä¿®æ£
ååä½æãã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); // ***** éå§ ***** var chatNode = Instantiate<GameObject>(chatNodePrefab, content.transform, false); chatNode.GetComponent<ChatNode>().Init(data); // ***** çµäº ***** } } 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
Instantiateã§ChatNodePrefabãä½æãã¾ãã
Instantiate第ï¼å¼æ°ã¯è¦ªãªãã¸ã§ã¯ãã§ããContentãæå®ã
Instantiate第ï¼å¼æ°()ãfalseã«ããã¨è¦ªãªãã¸ã§ã¯ãã®ä½ç½®ã«çæããã¾ãã
ãã®å¾ãChatNodeã®Initã¡ã½ãããå¼ã³åºãã¾ãã
å®éã«åçãã¦è©¦ãã¦ã¿ã¾ãããã
æåå
¥åãMINEãã¿ã³ãæ¼ãã¨ç«ãåãã
OTHERSãã¿ã³ãæ¼ãã¨ç¬ãåãã
ChatBoardã®MAX Widthãå¶éãã
æå¾ã«ChatBoardã®MAX Widthãå¶éããæ¹æ³ãæ¸ãã¦ããã¾ãã
ç»é¢å¹
ã®50%ãè¶
ããã50%ã«å¶éãã¾ãã
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class ChatNode : MonoBehaviour { private ChatData chatData; [SerializeField] LayoutGroup layoutGroup; [SerializeField] Image chatBoard; [SerializeField] Text chatText; [SerializeField] Image chatIcon; [SerializeField] Sprite mineSprite; [SerializeField] Sprite othersSprite; public void Init(ChatData data) { chatData = data; chatText.text = chatData.body; if (chatData.roll == ChatRoll.MINE) { chatIcon.sprite = mineSprite; layoutGroup.childAlignment = TextAnchor.MiddleRight; chatBoard.color = new Color(0f, 1f, 0.1f); chatIcon.transform.SetSiblingIndex(1); } else if (chatData.roll == ChatRoll.OTHERS) { chatIcon.sprite = othersSprite; layoutGroup.childAlignment = TextAnchor.MiddleLeft; chatBoard.color = new Color(1f, 1f, 1f); chatIcon.transform.SetSiblingIndex(0); } // ***** éå§ ***** StartCoroutine(CheckTextSize()); // ***** çµäº ***** } // ***** éå§ ***** private IEnumerator CheckTextSize() { yield return new WaitForEndOfFrame(); if (chatBoard.rectTransform.sizeDelta.x > this.GetComponent<RectTransform>().sizeDelta.x * 0.5f) { chatBoard.GetComponent<LayoutElement>().preferredWidth = this.GetComponent<RectTransform>().sizeDelta.x * 0.5f; } } // ***** çµäº ***** }
ChatSystem.cs
Unityã®Auto Layoutã·ã¹ãã ã«ãã UIãã¼ãã®ãµã¤ãºå¤æ´ã¯
ãã¬ã¼ã ã®æå¾ã«å®è¡ããã¾ãã
ãªã®ã§WaitForEndOfFrameã®å¾ã§ChatBoardã®preferredWidthãå¤æ´ãã¾ãã
以ä¸ã§LINEã£ã½ããã£ããã®ãã¥ã¼ããªã¢ã«çµäºã§ãã
ãããã¨ããããã¾ããã