Skip to content

Instantly share code, notes, and snippets.

@k-harris
Last active February 9, 2019 19:31
Show Gist options
  • Save k-harris/ae9f2c33585608f527fb919a9b47f5bf to your computer and use it in GitHub Desktop.
Save k-harris/ae9f2c33585608f527fb919a9b47f5bf to your computer and use it in GitHub Desktop.
Text system that updates much like a MMO chat. Can be called and updated by any game event.
////////////////////////////////////////////////////////////////////////////////
// Authors: Kaila Harris
// Copyright © 2016 DigiPen (USA) Corp. and its owners. All Rights Reserved.
////////////////////////////////////////////////////////////////////////////////
// goes on UI text that it will update
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
[RequireComponent(typeof(Text))]
public class TextFeedManager : MonoBehaviour
{
public Text textField;
public bool ClearOnMouseExit = false;
List<string> infoText;
string infoDisplayed;
int lineLimit = 5;
string preline, postline;
AudioSource textAudio;
void Awake()
{
if (textField == null)
textField = GetComponent<Text>();
infoText = new List<string>();
infoDisplayed = "";
}
// Use this for initialization
void Start ()
{
textAudio = gameObject.GetComponent<AudioSource>();
}
public void SetTextSystem(int _lineLimit)
{
lineLimit = _lineLimit;
}
public void SetTextSystem(int _lineLimit, string _preline)
{
lineLimit = _lineLimit;
preline = _preline;
}
public void SetTextSystem(int _lineLimit, string _preline, string _postilne)
{
lineLimit = _lineLimit;
preline = _preline;
postline = _postilne;
}
public void AddDataToFeed(string _info)
{
infoText.Add(_info);
UpdateInfoFeed();
}
public void AddDataToFeed(string _info, int _index)
{
infoText[_index] = _info;
UpdateInfoFeed();
}
public void ClearFeed()
{
infoText.Clear();
UpdateInfoFeed();
}
void UpdateInfoFeed()
{
CreateNewFeed();
textField.text = infoDisplayed;
if (textAudio != null)
textAudio.Play();
}
void CreateNewFeed()
{
while (infoText.Count > lineLimit)
infoText.RemoveAt(0);
infoDisplayed = "";
foreach (string str in infoText)
infoDisplayed += string.Concat(preline, str, postline, "\n");
}
private void OnMouseExit()
{
if(ClearOnMouseExit)
{
foreach (string str in infoText)
infoDisplayed += string.Concat("", "\n");
}
}
void OnDestroy()
{
textField = null;
infoText.Clear();
}
void Test()
{
infoText.Add("hello world!");
infoText.Add("this");
infoText.Add("is");
infoText.Add("a");
infoText.Add("test");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment