quantum-space-buddies/QSB/DebugLog.cs

65 lines
2.0 KiB
C#
Raw Normal View History

2020-02-15 19:48:02 +00:00
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
2020-02-15 19:48:02 +00:00
namespace QSB
{
public class DebugLog : MonoBehaviour
{
private const int ScreenLinesMax = 6;
2020-02-15 19:48:02 +00:00
private static Text _screenText;
private static List<string> _lines;
private void Awake()
{
var assetBundle = QSB.Helper.Assets.LoadBundle("assets/debug");
2020-02-15 19:48:02 +00:00
var logCanvas = Instantiate(assetBundle.LoadAsset<GameObject>("assets/logcanvas.prefab"));
DontDestroyOnLoad(logCanvas);
DontDestroyOnLoad(this);
2020-02-15 19:48:02 +00:00
logCanvas.GetComponent<Canvas>().sortingOrder = 9999;
_screenText = logCanvas.GetComponentInChildren<Text>();
2020-02-15 19:48:02 +00:00
_lines = new List<string>(ScreenLinesMax);
for (var i = 0; i < ScreenLinesMax; i++)
{
_lines.Add(".");
}
}
2020-02-15 19:48:02 +00:00
private static string JoinAll(params object[] logObjects)
{
return string.Join(" ", logObjects.Select(o => o.ToString()).ToArray());
}
2020-02-15 19:48:02 +00:00
public static void Console(params object[] logObjects)
{
QSB.Helper.Console.WriteLine(logObjects);
}
2020-02-15 19:48:02 +00:00
public static void Screen(params object[] logObjects)
{
for (var i = 1; i < ScreenLinesMax; i++)
{
_lines[i - 1] = _lines[i];
}
2020-02-15 19:48:02 +00:00
_lines.Insert(ScreenLinesMax - 1, JoinAll(logObjects));
_screenText.text = string.Join("\n", _lines.ToArray());
}
2020-02-15 19:48:02 +00:00
public static void HUD(params object[] logObjects)
{
if (Locator.GetPlayerBody() == null)
{
Console("Warning: tried to log to HUD but player is not ready.");
Console(logObjects);
return;
}
2020-02-15 19:48:02 +00:00
var data = new NotificationData(NotificationTarget.Player, JoinAll(logObjects), 5f, true);
NotificationManager.SharedInstance.PostNotification(data, false);
}
2020-02-15 19:48:02 +00:00
}
}