Skip to content

Commit 6cd2b40

Browse files
committed
Scene lighting settings are now searched for references (can be turned off)
1 parent 3c42921 commit 6cd2b40

3 files changed

Lines changed: 69 additions & 6 deletions

File tree

Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class Parameters
3131
public SceneSearchMode searchInScenes = SceneSearchMode.AllScenes;
3232
public Object[] searchInScenesSubset = null;
3333
public Object[] excludedScenesFromSearch = null;
34+
public bool searchInSceneLightingSettings = true;
3435
public bool searchInAssetsFolder = true;
3536
public Object[] searchInAssetsSubset = null;
3637
public Object[] excludedAssetsFromSearch = null;
@@ -162,6 +163,7 @@ public SearchResult Run( Parameters searchParameters )
162163

163164
// Get the scenes that are open right now
164165
SceneSetup[] initialSceneSetup = !isInPlayMode ? EditorSceneManager.GetSceneManagerSetup() : null;
166+
Scene activeScene = EditorSceneManager.GetActiveScene();
165167

166168
// Make sure that the AssetDatabase is up-to-date
167169
AssetDatabase.SaveAssets();
@@ -524,7 +526,7 @@ public SearchResult Run( Parameters searchParameters )
524526
if( excludedScenesPathsSet.Contains( scenePath ) )
525527
continue;
526528

527-
SearchScene( scenePath, searchResult, searchParameters.lazySceneSearch, initialSceneSetup );
529+
SearchScene( scenePath, searchResult, searchParameters, initialSceneSetup );
528530
}
529531
}
530532

@@ -607,6 +609,10 @@ public SearchResult Run( Parameters searchParameters )
607609

608610
EditorUtility.ClearProgressBar();
609611

612+
// If the active scene was changed during search, reset it
613+
if( EditorSceneManager.GetActiveScene() != activeScene )
614+
EditorSceneManager.SetActiveScene( activeScene );
615+
610616
#if UNITY_2018_3_OR_NEWER
611617
// If a prefab stage was open when the search was triggered, try reopening the prefab stage after the search is completed
612618
if( !string.IsNullOrEmpty( openPrefabStageAssetPath ) )
@@ -895,7 +901,7 @@ private void AddSearchedObjectToFilteredSets( Object obj, bool expandGameObjects
895901
}
896902

897903
// Search a scene for references
898-
private void SearchScene( string scenePath, List<SearchResultGroup> searchResult, bool lazySearch, SceneSetup[] initialSceneSetup )
904+
private void SearchScene( string scenePath, List<SearchResultGroup> searchResult, Parameters searchParameters, SceneSetup[] initialSceneSetup )
899905
{
900906
Scene scene = EditorSceneManager.GetSceneByPath( scenePath );
901907
if( isInPlayMode && !scene.isLoaded )
@@ -911,7 +917,7 @@ private void SearchScene( string scenePath, List<SearchResultGroup> searchResult
911917

912918
if( !scene.isLoaded )
913919
{
914-
if( lazySearch )
920+
if( searchParameters.lazySceneSearch )
915921
{
916922
searchResult.Add( new SearchResultGroup( scenePath, SearchResultGroup.GroupType.Scene, true, true ) );
917923
return;
@@ -927,6 +933,16 @@ private void SearchScene( string scenePath, List<SearchResultGroup> searchResult
927933
for( int i = 0; i < rootGameObjects.Length; i++ )
928934
SearchGameObjectRecursively( rootGameObjects[i] );
929935

936+
// Search through Lighting Settings (it requires changing the active scene but don't do that in play mode)
937+
if( searchParameters.searchInSceneLightingSettings && ( !isInPlayMode || SceneManager.GetActiveScene() == scene ) )
938+
{
939+
if( !isInPlayMode && EditorSceneManager.GetActiveScene() != scene )
940+
EditorSceneManager.SetActiveScene( scene );
941+
942+
BeginSearchObject( lightmapSettingsGetter() );
943+
BeginSearchObject( renderSettingsGetter() );
944+
}
945+
930946
// If no references are found in the scene and if the scene is not part of the initial scene setup, close it
931947
if( currentSearchResultGroup.NumberOfReferences == 0 )
932948
{

Plugins/AssetUsageDetector/Editor/AssetUsageDetectorSearchFunctions.cs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,12 @@ private static string ExtractGUIDFromString( string str )
205205
private readonly FieldInfoGetter fieldInfoGetter = (FieldInfoGetter) Delegate.CreateDelegate( typeof( FieldInfoGetter ), typeof( Editor ).Assembly.GetType( "UnityEditor.ScriptAttributeUtility" ).GetMethod( "GetFieldInfoFromProperty", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static ) );
206206
#endif
207207

208+
private readonly Func<Object> lightmapSettingsGetter = (Func<Object>) Delegate.CreateDelegate( typeof( Func<Object> ), typeof( LightmapEditorSettings ).GetMethod( "GetLightmapSettings", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static ) );
209+
private readonly Func<Object> renderSettingsGetter = (Func<Object>) Delegate.CreateDelegate( typeof( Func<Object> ), typeof( RenderSettings ).GetMethod( "GetRenderSettings", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static ) );
210+
#if UNITY_2021_2_OR_NEWER
211+
private readonly Func<Cubemap> defaultReflectionProbeGetter = (Func<Cubemap>) Delegate.CreateDelegate( typeof( Func<Cubemap> ), typeof( RenderSettings ).GetProperty( "defaultReflection", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static ).GetGetMethod( true ) );
212+
#endif
213+
208214
#if ASSET_USAGE_ADDRESSABLES
209215
private readonly Func<SpriteAtlas, Sprite[]> spriteAtlasPackedSpritesGetter = (Func<SpriteAtlas, Sprite[]>) Delegate.CreateDelegate( typeof( Func<SpriteAtlas, Sprite[]> ), typeof( SpriteAtlasExtensions ).GetMethod( "GetPackedSprites", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static ) );
210216
private readonly PropertyInfo assetReferenceSubObjectTypeGetter = typeof( AssetReference ).GetProperty( "SubOjbectType", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance );
@@ -235,6 +241,8 @@ private void InitializeSearchFunctionsData( Parameters searchParameters )
235241
{ typeof( BlendTree ), SearchBlendTree },
236242
{ typeof( AnimationClip ), SearchAnimationClip },
237243
{ typeof( TerrainData ), SearchTerrainData },
244+
{ typeof( LightmapSettings ), SearchLightmapSettings },
245+
{ typeof( RenderSettings ), SearchRenderSettings },
238246
#if UNITY_2017_1_OR_NEWER
239247
{ typeof( SpriteAtlas ), SearchSpriteAtlas },
240248
#endif
@@ -1007,6 +1015,36 @@ private ReferenceNode SearchTerrainData( object obj )
10071015
return referenceNode;
10081016
}
10091017

1018+
private ReferenceNode SearchLightmapSettings( object obj )
1019+
{
1020+
ReferenceNode referenceNode = PopReferenceNode( obj );
1021+
1022+
referenceNode.AddLinkTo( SearchObject( LightmapSettings.lightProbes ), "Light Probes" );
1023+
1024+
LightmapData[] lightmaps = LightmapSettings.lightmaps;
1025+
if( lightmaps != null )
1026+
{
1027+
for( int i = 0; i < lightmaps.Length; i++ )
1028+
referenceNode.AddLinkTo( SearchObject( lightmaps[i] ), "Lightmap" );
1029+
}
1030+
1031+
SearchVariablesWithSerializedObject( referenceNode, true );
1032+
return referenceNode;
1033+
}
1034+
1035+
private ReferenceNode SearchRenderSettings( object obj )
1036+
{
1037+
ReferenceNode referenceNode = PopReferenceNode( obj );
1038+
1039+
#if UNITY_2021_2_OR_NEWER
1040+
referenceNode.AddLinkTo( SearchObject( defaultReflectionProbeGetter() ), "Default Reflection Probe" );
1041+
#else
1042+
referenceNode.AddLinkTo( SearchObject( ReflectionProbe.defaultTexture ), "Default Reflection Probe" );
1043+
#endif
1044+
SearchVariablesWithSerializedObject( referenceNode, true );
1045+
return referenceNode;
1046+
}
1047+
10101048
#if UNITY_2017_1_OR_NEWER
10111049
private ReferenceNode SearchSpriteAtlas( object obj )
10121050
{
@@ -1396,9 +1434,9 @@ private void SearchShaderSourceCodeForCGIncludes( ReferenceNode referenceNode )
13961434
}
13971435

13981436
// Search through variables of an object with SerializedObject
1399-
private void SearchVariablesWithSerializedObject( ReferenceNode referenceNode )
1437+
private void SearchVariablesWithSerializedObject( ReferenceNode referenceNode, bool forceUseSerializedObject = false )
14001438
{
1401-
if( !isInPlayMode || referenceNode.nodeObject.IsAsset() )
1439+
if( !isInPlayMode || referenceNode.nodeObject.IsAsset() || forceUseSerializedObject )
14021440
{
14031441
#if ASSET_USAGE_ADDRESSABLES
14041442
// See: https://github.com/yasirkula/UnityAssetUsageDetector/issues/29

Plugins/AssetUsageDetector/Editor/AssetUsageDetectorWindow.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class AssetUsageDetectorWindow : EditorWindow, IHasCustomMenu
2222
private enum WindowFilter { AlwaysReturnActive, ReturnActiveIfNotLocked, AlwaysReturnNew };
2323

2424
private const string PREFS_SEARCH_SCENES = "AUD_SceneSearch";
25+
private const string PREFS_SEARCH_SCENE_LIGHTING_SETTINGS = "AUD_LightingSettingsSearch";
2526
private const string PREFS_SEARCH_ASSETS = "AUD_AssetsSearch";
2627
private const string PREFS_SEARCH_PROJECT_SETTINGS = "AUD_ProjectSettingsSearch";
2728
private const string PREFS_DONT_SEARCH_SOURCE_ASSETS = "AUD_AssetsExcludeSrc";
@@ -73,6 +74,7 @@ private bool IsLocked
7374
private bool searchInScenesInBuild = true; // Scenes in build
7475
private bool searchInScenesInBuildTickedOnly = true; // Scenes in build (ticked only or not)
7576
private bool searchInAllScenes = true; // All scenes (including scenes that are not in build)
77+
private bool searchInSceneLightingSettings = true; // Window-Rendering-Lighting settings
7678
private bool searchInAssetsFolder = true; // Assets in Project window
7779
private bool dontSearchInSourceAssets = true; // objectsToSearch won't be searched for internal references
7880
private bool searchInProjectSettings = true; // Player Settings, Graphics Settings etc.
@@ -313,6 +315,7 @@ private void ShowAndSearchInternal( IEnumerable<Object> searchObjects, AssetUsag
313315
if( searchParameters != null )
314316
{
315317
ParseSceneSearchMode( searchParameters.searchInScenes );
318+
searchInSceneLightingSettings = searchParameters.searchInSceneLightingSettings;
316319
searchInAssetsFolder = searchParameters.searchInAssetsFolder;
317320
dontSearchInSourceAssets = searchParameters.dontSearchInSourceAssets;
318321
searchInProjectSettings = searchParameters.searchInProjectSettings;
@@ -396,6 +399,7 @@ private void OnDestroy()
396399
private void SavePrefs()
397400
{
398401
EditorPrefs.SetInt( PREFS_SEARCH_SCENES, (int) GetSceneSearchMode( false ) );
402+
EditorPrefs.SetBool( PREFS_SEARCH_SCENE_LIGHTING_SETTINGS, searchInSceneLightingSettings );
399403
EditorPrefs.SetBool( PREFS_SEARCH_ASSETS, searchInAssetsFolder );
400404
EditorPrefs.SetBool( PREFS_DONT_SEARCH_SOURCE_ASSETS, dontSearchInSourceAssets );
401405
EditorPrefs.SetBool( PREFS_SEARCH_PROJECT_SETTINGS, searchInProjectSettings );
@@ -417,7 +421,7 @@ private void SavePrefs()
417421
private void LoadPrefs()
418422
{
419423
ParseSceneSearchMode( (SceneSearchMode) EditorPrefs.GetInt( PREFS_SEARCH_SCENES, (int) ( SceneSearchMode.OpenScenes | SceneSearchMode.ScenesInBuildSettingsTickedOnly | SceneSearchMode.AllScenes ) ) );
420-
424+
searchInSceneLightingSettings = EditorPrefs.GetBool( PREFS_SEARCH_SCENE_LIGHTING_SETTINGS, true );
421425
searchInAssetsFolder = EditorPrefs.GetBool( PREFS_SEARCH_ASSETS, true );
422426
dontSearchInSourceAssets = EditorPrefs.GetBool( PREFS_DONT_SEARCH_SOURCE_ASSETS, true );
423427
searchInProjectSettings = EditorPrefs.GetBool( PREFS_SEARCH_PROJECT_SETTINGS, true );
@@ -554,6 +558,10 @@ private void OnGUI()
554558
GUILayout.EndVertical();
555559
GUILayout.EndHorizontal();
556560

561+
EditorGUI.BeginDisabledGroup( !searchInOpenScenes && !searchInScenesInBuild && !searchInAllScenes );
562+
searchInSceneLightingSettings = WordWrappingToggleLeft( "Scene Lighting Settings (WARNING: This may change the active scene during search)", searchInSceneLightingSettings );
563+
EditorGUI.EndDisabledGroup();
564+
557565
Utilities.DrawSeparatorLine();
558566

559567
searchInProjectSettings = WordWrappingToggleLeft( "Project Settings (Player Settings, Graphics Settings etc.)", searchInProjectSettings );
@@ -695,6 +703,7 @@ private void InitiateSearch()
695703
{
696704
objectsToSearch = !objectsToSearch.IsEmpty() ? new ObjectToSearchEnumerator( objectsToSearch ).ToArray() : null,
697705
searchInScenes = GetSceneSearchMode( true ),
706+
searchInSceneLightingSettings = searchInSceneLightingSettings,
698707
searchInAssetsFolder = searchInAssetsFolder,
699708
searchInAssetsSubset = !searchInAssetsSubset.IsEmpty() ? searchInAssetsSubset.ToArray() : null,
700709
excludedAssetsFromSearch = !excludedAssets.IsEmpty() ? excludedAssets.ToArray() : null,

0 commit comments

Comments
 (0)