Skip to content

Extra Resources

Eric Lowry edited this page Aug 7, 2024 · 9 revisions

Advanced Logging

When using InputLayers, you are likely to notice the formatted logging that it uses. This is the result of custom versions of default Unity classes with built-in extended logging functionality:

Original Class Custom Class
MonoBehavior🔗 LowryBehavior
ScriptableObject🔗 LowryScriptable
Editor🔗 LowryEditor
EditorWindow🔗 LowryEditorWindow
PropertyDrawer🔗 LowryPropertyDrawer

These custom classes offer a number of logging tools designed to perform well, and offer better visual clarity when reading logs in the Unity Editor, as well as when exported. Here is a list of the built-in features, and how they can be used/configured:

Logging Methods

The core idea is to replace standard logging methods with custom variants that inject visual information to help identify the class they originate from, and display objects in a human-readable way using JSON.

Original Class Custom Class
Debug.Log()🔗 LogMsg()
Debug.LogWarning()🔗 LogWarning()
Debug.LogError()🔗 LogError()

Note: Some variants, like LowryEditor may not include the full suite of logging tools as InputLayers does not require them in that context.

Example

// Standard approach to logging
Debug.LogWarning("Beware!");
Debug.Log("Something happened", _contextObject);

// Custom logging approach
LogWarning("Beware!");
LogMsg(_objectToDisplayAsJson, "Something happened");

Optimization

For optimization purposes, it is recommended to avoid passing formatted strings, and instead pas elements as arguments:

// DO NOT DO THIS
LogMsg("A log about {_name}").

// Do this instead
LogMsg("A log about {0}", _name)

Automatic Styling

When using the above methods, a colorful title can be injected before the log. This is designed to help quickly and easily distinguish the source of the log.

In order to use this functionality, your class inhabiting from a custom "Lowry" class will need to override the d_logHeader value:

protected override string d_logHeader => "[The name of the class, or some other identifyer]";

For visual clarity, InputLayers always puts these titles betwee, brackets.

Color

By default, different colors are used for various class types:

Class Default Color
LowryBehavior #8c6119 headerOrange
LowryScriptable #a6415a headerPink
LowryEditor #2f8f61 headerGreen
LowryEditorWindow #2f8f61 headerGreen
LowryPropertyDrawer #2f8f61 headerGreen

However, in the case of LowryBehavior and LowryScriptable classes, the color can be overridden like this:

protected override Log.Colors d_logHeaderColor => Log.Colors.headerGrass;

Here is the list of available colors; which can be modified by editing the Lowry.Utils.Logs file:

  • #8c6119 headerOrange
  • #a6415a headerPink
  • #7d7329 headerGrass
  • #5c1bab headerPurple
  • #2f8f61 headerGreen
  • #871f51 headerWine

Pseudo Serializable Dictionary

Namespace: namespace Lowry.Utils.CodeExtensions

public sealed class PseudoSerializableDictionary<TKey, TValue>

Input Layers makes use of a serializable "dictionary", which consists instead of a List of keys and a List of values kept in sync.

This "dictionary" functions a lot like a typical C# Dictionary🔗:

Properties

Keys

public List<TKey> Keys { get {...}, set {...} }

Stores the TKey keys of the dictionary.

Values

public List<TValue> Values { get {...}, set {...} }

Stores the TValue values of the dictionary.

Count

public int Count { get {...} }

Returns: the number of elements currently in the dictionary.

Methods

Set(TKey, TValue)

public void Set (TKey key, TValue value) {...}

Assigns a value to a specific key in the dictionary.

Set(KeyValuePair<TKey, TValue>)

public void Set (KeyValuePair<TKey, TValue> pair) {...}

Adds a KeyValuePair direcly to the dictionary.

Remove(TKey)

public void Set (TKey key, TValue value) {...}

Removes the entry at a specific key from the dictionary.

Returns: true if the key was found and removed.

Remove(KeyValuePair<TKey, TValue>)

public void Set (KeyValuePair<TKey, TValue> pair) {...}

Removes the entry at a specific KeyValuePair's key from the dictionary.

Returns: true if the KeyValuePair's key was found and removed.

RemoveAt(int)

public void RemoveAt (int id) {...}

Removes the entry at a specific id in the dictionary.

Returns: true if the id was found and removed.

Contains(TKey)

public void Contains (TKey key) {...}

Returns: true if the key was found in the dictionary.

Contains(KeyValuePair<TKey, TValue>)

public void Contains (KeyValuePair<TKey, TValue> pair) {...}

Returns: true if the KeyValuePair's key was found in the dictionary.

Clear()

public void Clear () {...}

Clears the contents of the dictionary.

At(int)

public TValue At (int id) {...}

Returns: the TValue found at the position id in dictionary or default if the id is ut of bounds.

At(TKey)

public TValue At (TKey key) {...}

Returns: the TValue corresponding to the TKey in dictionary or throws an ArgumentOutOfRangeException if the TKey cannot be found in the dictionary.

Clone this wiki locally