Search Unity

Feedback console should writeout names of conflicting objects

Discussion in 'Editor & General Support' started by laurentlavigne, Jan 5, 2021.

  1. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,327
  2. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    You can filter the Project view to isolate all objects with a certain component:

    upload_2021-1-4_21-25-15.png

    I also put together this script that will print them to the console for you:

    upload_2021-1-4_21-35-26.png

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using System.Text;
    3. using System.Text.RegularExpressions;
    4. using UnityEditor;
    5. using UnityEngine;
    6.  
    7. public static class AudioListenerConflictDetector {
    8.     enum MonitoringLevel {
    9.         Always,
    10.         OnEnterPlaymode
    11.     }
    12.  
    13.     static readonly Regex messageRegex = new Regex( "There are \\d+ audio listeners in the scene\\. Please ensure there is always exactly one audio listener in the scene\\." );
    14.    
    15.     // Change your monitoring level here
    16.     // OnEnterPlaymode is more performant for normal cases but will only check when you first click play
    17.     static readonly MonitoringLevel monitoringLevel = MonitoringLevel.OnEnterPlaymode;
    18.  
    19.     [InitializeOnLoadMethod]
    20.     static void DetectAudioListeners () {
    21.         EditorApplication.playModeStateChanged += OnPlaymodeStateChange;
    22.     }
    23.  
    24.     private static void OnPlaymodeStateChange ( PlayModeStateChange state ) {
    25.         if( state == PlayModeStateChange.EnteredPlayMode ) {
    26.             if( monitoringLevel == MonitoringLevel.Always ) {
    27.                 Application.logMessageReceived += OnLogMessage;
    28.             } else {
    29.                 PrintAllAudioListeners();
    30.             }
    31.         } else if( state == PlayModeStateChange.ExitingPlayMode ) {
    32.             Application.logMessageReceived -= OnLogMessage;
    33.         }
    34.     }
    35.  
    36.     private static void OnLogMessage ( string condition, string stackTrace, LogType type ) {
    37.         if( type == LogType.Log ) {
    38.             if( messageRegex.IsMatch( condition ) ) {
    39.                 PrintAllAudioListeners();
    40.             }
    41.         }
    42.     }
    43.  
    44.     static void PrintAllAudioListeners () {
    45.         var listeners = Object.FindObjectsOfType<AudioListener>();
    46.         if( listeners.Length <= 1 ) {
    47.             return;
    48.         }
    49.  
    50.         // if you want to get really fancy, you can automatically set the filter when conflicts occur:
    51.         //SceneModeUtility.SearchForType( typeof( AudioListener ) );
    52.  
    53.         var inheritence = new List<string>();
    54.  
    55.         var sb = new StringBuilder();
    56.         sb.AppendLine( "The audio listeners are:" );
    57.         foreach( var obj in listeners ) {
    58.             inheritence.Clear();
    59.             var n = obj.transform;
    60.             while( n != null ) {
    61.                 inheritence.Add( n.name );
    62.                 n = n.parent;
    63.             }
    64.             inheritence.Reverse();
    65.        
    66.             sb.Append( "  " ).AppendLine( string.Join( "/", inheritence ) );
    67.         }
    68.  
    69.         Debug.LogError( sb.ToString() );
    70.     }
    71. }
     
    Last edited: Jan 5, 2021
  3. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,327
    So you detect that string with the regex and add the extra info? Mad respect!