Implement LayerMask.ToString

Please implement/override the LayerMask.ToString method and return meaningful information. In Unity 2018.4.4f1, calling the following code:

LayerMask layer = LayerMask.NameToLayer("UI") | LayerMask.NameToLayer("Ignore Raycast");
Debug.LogFormat("{0}", layer);

Outputs to the Console window:

It would be a lot more useful, if the output contains the layer names and the value. For example, the output could be like this instead:

I’m aware that there is the layer.value property that represents the integer bitMask, but for debugging happiness, what I described above would make a lot of sense.

2 Likes

I was about to open the same thread. :wink:

This would allow custom debug code to automatically log all properties of an object, for example:

public static void DumpLog(object target)
{
    if (target == null)
    {
        Debug.Log("Null");
        return;
    }

    Object unityObject = target as UnityEngine.Object;
    System.Type type = target.GetType();

    string message = unityObject != null ? unityObject.ToString() : type.Name;
    message += " (Dump)\n";

    FieldInfo[] fields = type.GetFields();
    foreach (var field in fields)
    {
        string line = field.Name + ": " + field.GetValue(target).ToString();
        message += line + "\n";
    }

    Debug.Log(message, unityObject);
}

There are a few more cases where code like this make development easier (showing properties in debug ui, etc), where we don’t actually know the type and therefore cannot provide a custom string format. Instead, many of these quick development solutions rely on a reasonable default implementation of ToString.

I hope Unity is able to implement ToString for all engine types in the future. It doesn’t have to be perfect, but at least some kind of information.