Search Unity

Feedback Implement LayerMask.ToString

Discussion in 'Editor & General Support' started by Peter77, Aug 19, 2019.

  1. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,618
    Please implement/override the
    LayerMask.ToString
    method and return meaningful information. In Unity 2018.4.4f1, calling the following code:
    Code (CSharp):
    1. LayerMask layer = LayerMask.NameToLayer("UI") | LayerMask.NameToLayer("Ignore Raycast");
    2. 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.
     
    Xarbrough and karl_jones like this.
  2. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    1,188
    I was about to open the same thread. ;)

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

    Code (CSharp):
    1. public static void DumpLog(object target)
    2. {
    3.     if (target == null)
    4.     {
    5.         Debug.Log("Null");
    6.         return;
    7.     }
    8.  
    9.     Object unityObject = target as UnityEngine.Object;
    10.     System.Type type = target.GetType();
    11.  
    12.     string message = unityObject != null ? unityObject.ToString() : type.Name;
    13.     message += " (Dump)\n";
    14.  
    15.     FieldInfo[] fields = type.GetFields();
    16.     foreach (var field in fields)
    17.     {
    18.         string line = field.Name + ": " + field.GetValue(target).ToString();
    19.         message += line + "\n";
    20.     }
    21.  
    22.     Debug.Log(message, unityObject);
    23. }
    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.