This article is fromInspoy miscellaneous collection | food chicken inspoy learning record

preface

I was going to continue writing about the UI, but the last article mentioned the SFUtils class, so I will introduce the logging aspect of the encapsulation first

purpose

The game is currently targeted at PC and Mac platforms, but there are concerns about network synchronization on mobile platforms, which will be discussed later. This is not the main problem, of course. The main problem is that Unity logging is extremely difficult to use. Using debug.log () is the most common way to do this, but it simply outputs the Log to the UnityEditor Console, which is a bit ugly. But as a member of the Look Society, find a way to make this as elegant as possible. The idea is to unify info, warning and error into one log and record them all in the file. In this way, it will be convenient for us to browse the previous logs and not lose them. When combined with the real-time log viewer (there was a pit in it, but I don’t think I had time to fill in the ORZ recently), it’s perfect

design

All parts of the game that need to be logged use the methods provided by SFUtils.

methods instructions
log(string[, int]) This parameter is optional. The smaller the number, the more important it is. The default value is 0
logWarning(string) Logs a warning, which has the same effect as debug.logWarning ()
logError(string) Logs an error with the same effect as debug.logerror ()
assert(bool[, string]) Provides the same functionality as debug.assert (), and prompts are also written to files

LogWarning () corresponds to a normal log of level -1, while logError() corresponds to a normal log of level -2. The log method is then output to Console (as the default) based on the log level and formatted to a file

logStr = " [INFO-" + level.ToString() + "] -" + logStr;Copy the code

Next, the crucial step is to append the above logStr to the log file

static void logToFile(string msg)
{
    DateTime dt = DateTime.Now;
    string output = dt.ToString("HH:mm:ss.ff ") + msg;
    if(sm_logFileInfo ! =null && sm_logFileInfo.Exists)
    {
        varsw = sm_logFileInfo.AppendText(); sw.WriteLine(output); sw.Close(); }}Copy the code

This way, every time we log out, we will write to the same file, so we will create a new log file every time the game starts, to separate each run, named after the current time

static public void clearLogFile()
{
    DateTime dt = DateTime.Now;
    string path = Application.persistentDataPath + "/GameLog" + dt.ToString("yyyyMMddHHmmss") + ".txt";
    sm_logFileInfo = new FileInfo(path);
    var sw = sm_logFileInfo.CreateText();
    sw.WriteLine("[BounceArena] - " + dt.ToString("G"));
    sw.Close();
}Copy the code

miscellaneous

Well, these are not related to logging, but since they are in the SFUtils class, I will introduce them in passing

Get the Unix timestamp

Unity/C# doesn’t even have an interface to get current Unix timestamps by default

static public long getTimeStampNow()
{
    TimeSpan ts = DateTime.UtcNow - new DateTime(1970.1.1.0.0.0.0);
    return Convert.ToInt64(ts.TotalSeconds);
}Copy the code

Find a child object under a GameObject

Find the child object based on the given name of the parent node and child object. Note that this method takes quite a bit of time and is best avoided in an UPDATE. If there are more than one child object with the same name, only the first result found is returned.

static public GameObject findChildWithParent(GameObject parent, string childName)
{
    Transform parentTrans = parent.transform;
    foreach (Transform trans in parentTrans.GetComponentInChildren<Transform>())
    {
        if (trans.name == childName)
        {
            return trans.gameObject;
        }
        else
        {
            var child = SFUtils.findChildWithParent(trans.gameObject, childName);
            if(child ! =null)
            {
                returnchild; }}}return null;
}Copy the code

The complete code

The code snippet posted above only contains key parts due to space limitations, the full code can be found on my Github