- Create a
ScrollRectcomponent on a GameObject and add the above script to it. - Add a
Vertical Layout Groupcomponent to the content object. - User a
Content Size Fittercomponent to adjust size if needed. - Create a
TextGameObject to set as the prefab, and link it to the above script. - Call the
addMessagemethod to add text.
Last active
June 12, 2020 05:57
-
-
Save tsraveling/caa2bd5be6b81c17c1da305b3b426704 to your computer and use it in GitHub Desktop.
Text Log View Unity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections; | |
| using System.Collections.Generic; | |
| using UnityEngine; | |
| using UnityEngine.UI; | |
| public class TextManager : MonoBehaviour { | |
| public Transform contentView; | |
| public GameObject messagePrefab; | |
| public void addMessage(string text) { | |
| // Add the text object | |
| GameObject textObject = Instantiate(messagePrefab); | |
| Text textComponent = textObject.GetComponent<Text>(); | |
| textComponent.text = text; | |
| textObject.transform.SetParent(contentView); | |
| // Scroll to bottom | |
| this.gameObject.GetComponent<ScrollRect>().normalizedPosition = new Vector2(0, 0); | |
| } | |
| // Use this for initialization | |
| void Start () { | |
| } | |
| // Update is called once per frame | |
| void Update () { | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment