浏览代码

Improve Logger:

- Several static members don't need to be static
- Log to console if no UI element is available
- Clear the log on scene reload
/4.1
Tim Mowrer 4 年前
当前提交
2916f583
共有 1 个文件被更改,包括 29 次插入24 次删除
  1. 53
      Assets/Scripts/Logger.cs

53
Assets/Scripts/Logger.cs


Text m_LogText;
public Text logText
{
get { return s_LogText; }
set
{
m_LogText = value;
s_LogText = value;
}
get => m_LogText;
set => m_LogText = value;
}
[SerializeField]

get { return s_VisibleMessageCount; }
set
{
m_VisibleMessageCount = value;
s_VisibleMessageCount = value;
}
get => m_VisibleMessageCount;
set => m_VisibleMessageCount = value;
static int s_VisibleMessageCount;
static Text s_LogText;
static StringBuilder s_StringBuilder = new StringBuilder();
static StringBuilder m_StringBuilder = new StringBuilder();
s_LogText = m_LogText;
s_VisibleMessageCount = m_VisibleMessageCount;
if (m_LogText == null)
{
m_LogText = GetComponent<Text>();
}
lock (s_Log)
{
s_Log?.Clear();
}
Log("Log console initialized.");
}

{
if (m_LastMessageCount != s_Log.Count)
{
s_StringBuilder.Clear();
var startIndex = Mathf.Max(s_Log.Count - s_VisibleMessageCount, 0);
m_StringBuilder.Clear();
var startIndex = Mathf.Max(s_Log.Count - m_VisibleMessageCount, 0);
s_StringBuilder.Append($"{i:000}> {s_Log[i]}\n");
m_StringBuilder.Append($"{i:000}> {s_Log[i]}\n");
s_LogText.text = s_StringBuilder.ToString();
var text = m_StringBuilder.ToString();
if (m_LogText)
{
m_LogText.text = text;
}
else
{
Debug.Log(text);
}
}
m_LastMessageCount = s_Log.Count;

}
}
}
}
}
正在加载...
取消
保存