정리
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using UnityEngine.Events;
|
||||
using TMPro;
|
||||
public class ConsoleVisualiser : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Transform textParent = null;
|
||||
[SerializeField] private int textSize = 20;
|
||||
[SerializeField] private int maxMessages = 10;
|
||||
|
||||
public UnityEvent onMessageRecieved;
|
||||
|
||||
//private TMP_FontAsset messageFont;
|
||||
private int currentNumMessages;
|
||||
|
||||
static readonly Dictionary<LogType, Color> logTypeColors = new Dictionary<LogType, Color>() {
|
||||
{ LogType.Assert, Color.white },
|
||||
{ LogType.Error, Color.red },
|
||||
{ LogType.Exception, Color.red },
|
||||
{ LogType.Log, Color.white },
|
||||
{ LogType.Warning, Color.yellow },
|
||||
};
|
||||
|
||||
void Awake() {
|
||||
//messageFont = Resources.Load<TMP_FontAsset>("DefaultTMPFont");
|
||||
}
|
||||
|
||||
void OnEnable () {
|
||||
Application.logMessageReceived += HandleLog;
|
||||
}
|
||||
|
||||
void OnDisable () {
|
||||
Application.logMessageReceived -= HandleLog;
|
||||
}
|
||||
|
||||
void HandleLog (string message, string stackTrace, LogType type) {
|
||||
StartCoroutine(SendMessageToText(message, type));
|
||||
}
|
||||
|
||||
private IEnumerator SendMessageToText(string message, LogType type) {
|
||||
yield return new WaitForEndOfFrame();
|
||||
|
||||
GameObject go = new GameObject(message);
|
||||
go.transform.SetParent(textParent);
|
||||
go.transform.SetSiblingIndex(0);
|
||||
go.transform.localScale = Vector3.one;
|
||||
|
||||
TextMeshProUGUI txt = go.AddComponent<TextMeshProUGUI>();
|
||||
txt.color = logTypeColors[type];
|
||||
txt.text = message;
|
||||
//txt.font = messageFont;
|
||||
txt.fontSize = textSize;
|
||||
|
||||
RectTransform rt = (RectTransform)go.transform;
|
||||
Vector2 size = rt.sizeDelta;
|
||||
size.y = textSize;
|
||||
rt.sizeDelta = size;
|
||||
|
||||
currentNumMessages++;
|
||||
if (currentNumMessages > maxMessages) {
|
||||
Destroy(textParent.GetChild(textParent.childCount-1).gameObject);
|
||||
currentNumMessages--;
|
||||
}
|
||||
|
||||
onMessageRecieved.Invoke ();
|
||||
|
||||
yield return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 50205579a744e624b89d236cb0b569e8
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
Reference in New Issue
Block a user