So for whatever reason, the counter variables consoleCount and logCount don’t seem to be incrementing, as logging them with Debug.Log shows they stay as zero.
When I run the game, it generates a GameObject and sets the parent and other settings correctly, but after that it generates further GameObjects, but an error is logged when attempting to set the parent, or other settings such as adding a specific component, as it still seems to be trying to operate on the original GameObject that was created.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class Console_ : MonoBehaviour {
struct log__ {
public string Message;
public string StackTrace;
public LogType Type;
}
private static readonly Dictionary<LogType, Color> logTypeColors = new Dictionary<LogType, Color> () {
{LogType.Assert, Color.blue},
{LogType.Error, Color.red},
{LogType.Exception, Color.magenta},
{LogType.Log, Color.white},
{LogType.Warning, Color.yellow}
};
private List<log__> logs = new List<log__>();
private List<GameObject> logsInConsole = new List<GameObject>();
public float Margin = 20;
public float ButtonHeight = 25;
public float ButtonWidth = 120;
public float InputHeight = 30;
public bool GenerateDebugs = false;
private GameObject debugConsole;
private Button close;
private Button clear;
private Button collapseView;
private RectTransform scrollView;
private GameObject logPanel;
private InputField inputField;
private bool show = false;
private bool collapse = false;
private int logCount;
private int consoleCount;
private int repeats;
void Start () {
float consoleWidth = Screen.width - (2 * Margin);
debugConsole = GameObject.Find ("DebugConsole");
debugConsole.GetComponent<RectTransform> ().sizeDelta = new Vector2 (consoleWidth, Screen.height - (2 * Margin));
close = GameObject.Find ("ConsoleCloseButton").GetComponent<Button> ();
close.GetComponent<RectTransform> ().anchoredPosition = Vector2.zero;
close.GetComponent <RectTransform> ().sizeDelta = new Vector2 (ButtonWidth, ButtonHeight);
close.GetComponentInChildren<Text> ().text = "Close Console";
clear = GameObject.Find ("ConsoleClearButton").GetComponent<Button> ();
clear.GetComponent<RectTransform> ().anchoredPosition = new Vector2 (ButtonWidth, 0);
clear.GetComponent <RectTransform> ().sizeDelta = new Vector2 (ButtonWidth, ButtonHeight);
clear.GetComponentInChildren<Text> ().text = "Clear Console";
collapseView = GameObject.Find ("ConsoleCollapseButton").GetComponent<Button> ();
collapseView.GetComponent<RectTransform> ().anchoredPosition = new Vector2 (ButtonWidth * 2, 0);
collapseView.GetComponent <RectTransform> ().sizeDelta = new Vector2 (ButtonWidth, ButtonHeight);
collapseView.GetComponentInChildren<Text> ().text = "Collapse View";
scrollView = GameObject.Find ("ConsoleScrollView").GetComponent<RectTransform> ();
logPanel = GameObject.Find ("LogPanel");
inputField = GameObject.Find ("ConsoleInputField").GetComponent<InputField> ();
inputField.GetComponent<RectTransform> ().anchoredPosition = Vector2.zero;
inputField.GetComponent<RectTransform> ().sizeDelta = new Vector2 (consoleWidth, InputHeight);
Application.logMessageReceived += handleLog;
debugConsole.SetActive (false);
logCount = 0;
consoleCount = 0;
repeats = 1;
if (GenerateDebugs) {
InvokeRepeating ("generateDebugs", 2, 2);
}
}
void Update () {
// toggles the console panel
if (Input.GetKeyDown (Common_.console)) {
ToggleConsole ();
}
for (;logCount < logs.Count; ++logCount) {
// combines identical messages if 'Collapse' is selected
if (collapse) {
var messageSameAsPrevious = logCount > 0 && logs[logCount].Message == logs[logCount - 1].Message;
if (messageSameAsPrevious){
logsInConsole [consoleCount - 1].GetComponentInChildren<Text> ().text = string.Format("({0}) {1}", ++repeats, logsInConsole [consoleCount - 1].GetComponentInChildren<Text> ().text);
continue;
}
}
repeats = 1;
logsInConsole.Add (new GameObject("Console Log"));
GameObject currentLog = logsInConsole[consoleCount];
currentLog.transform.SetParent(logPanel.transform);
currentLog.AddComponent<RectTransform>();
currentLog.AddComponent<CanvasRenderer>();
currentLog.AddComponent<Button>();
currentLog.AddComponent<LayoutElement>();
currentLog.AddComponent<Image>();
currentLog.GetComponent<Button>().interactable = false;
currentLog.GetComponent<LayoutElement>().minHeight = 20;
if (consoleCount%2 == 1){
currentLog.GetComponent<Image>().color = new Color(0, 0, 0, 85);
} else {
currentLog.GetComponent<Image>().color = new Color(0, 0, 0, 50);
}
Text textBox = currentLog.GetComponentInChildren<Text> ();
textBox.text = logs[logCount].Message;
textBox.color = logTypeColors[logs[logCount].Type];
++consoleCount;
}
}
// records a console log from the log callback
void handleLog (string message, string stackTrace, LogType type){
logs.Add (new log__ (){
Message = message,
StackTrace = stackTrace,
Type = type
});
}
public void ToggleConsole () {
show = !show;
debugConsole.SetActive(show);
if (show) {
Time.timeScale = 0;
} else {
Time.timeScale = 1;
}
return;
}
public void ClearConsole (){
logs = new List<log__>();
logsInConsole = new List<GameObject>();
logCount = 0;
consoleCount = 0;
repeats = 1;
return;
}
public void ToggleCollapse (){
collapse = !collapse;
logCount = 0;
consoleCount = 0;
repeats = 1;
return;
}
void generateDebugs () {
Debug.Log (Time.frameCount);
Debug.LogError (Time.time);
}
}