11#nullable enable
22using System ;
33using System . Collections . Generic ;
4- using System . IO ;
54using System . Linq ;
65using UnityEditor ;
7- using UnityEditor . SceneManagement ;
86using UnityEngine ;
9- using UnityEngine . SceneManagement ;
107
118namespace Injecter . Unity . Editor
129{
@@ -15,99 +12,49 @@ public static class MonoInjecterFinder
1512 private static readonly Dictionary < Type , bool > _typeCache = new Dictionary < Type , bool > ( ) ;
1613
1714 [ MenuItem ( "Tools / Injecter / Ensure injection scripts in every prefab" ) ]
18- public static void AddComponentsToEveryPrefab ( )
19- {
20- foreach ( var prefabPath in AssetDatabase . GetAllAssetPaths ( ) . Where ( s => s . EndsWith ( ".prefab" ) ) )
21- {
22- var prefab = AssetDatabase . LoadMainAssetAtPath ( prefabPath ) ;
23-
24- if ( prefab is GameObject prefabObject )
25- {
26- AddScriptsToGameObject ( prefabObject , $ "prefab: { prefabPath } ") ;
27- AssetDatabase . SaveAssets ( ) ;
28- }
29- }
30- }
15+ public static void AddComponentsToEveryPrefab ( ) => GameObjectPatcher . AddComponentsToEveryPrefab ( FilterInjectables , AddScriptsToGameObject ) ;
3116
3217 [ MenuItem ( "Tools / Injecter / Ensure injection scripts in current scene" ) ]
33- public static void AddComponentsToCurrentScene ( )
34- {
35- var sceneName = SceneManager . GetActiveScene ( ) . name ;
18+ public static void AddComponentsToCurrentScene ( ) => GameObjectPatcher . AddComponentsToCurrentScene ( FilterInjectables , AddScriptsToGameObject ) ;
3619
37- var rootObjects = SceneManager
38- . GetActiveScene ( )
39- . GetRootGameObjects ( ) ;
20+ [ MenuItem ( "Tools / Injecter / Ensure injection scripts in every scene" ) ]
21+ public static void AddComponentsToEveryScene ( ) => GameObjectPatcher . AddComponentsToEveryScene ( FilterInjectables , AddScriptsToGameObject ) ;
4022
41- foreach ( var gameObject in rootObjects )
42- {
43- AddScriptsToGameObject ( gameObject , $ "scene: { sceneName } ") ;
44- }
45- }
23+ [ MenuItem ( "Tools / Injecter / Ensure injection scripts on everyting" ) ]
24+ public static void AddComponentsToEverything ( ) => GameObjectPatcher . AddComponentsToEverything ( FilterInjectables , AddScriptsToGameObject ) ;
4625
47- [ MenuItem ( "Tools / Injecter / Ensure injection scripts in every scene" ) ]
48- public static void AddComponentsToEveryScene ( )
26+ private static bool FilterInjectables ( GameObject gameObject ) => gameObject
27+ . GetComponents < MonoBehaviour > ( )
28+ . Any ( b => CanBeInjected ( b ) ) ;
29+
30+ private static bool CanBeInjected ( MonoBehaviour component )
4931 {
50- var dataPathFull = Path . GetFullPath ( Application . dataPath ) ;
51- var scenes = Directory
52- . EnumerateFiles ( dataPathFull , "*.unity" , SearchOption . AllDirectories )
53- . Select ( s => s . Replace ( dataPathFull , string . Empty ) )
54- . Select ( s => $ "Assets{ s } ")
55- . ToArray ( ) ;
32+ if ( component == null ) return false ;
5633
57- var originalScenePath = SceneManager . GetActiveScene ( ) . path ;
58- EditorSceneManager . SaveOpenScenes ( ) ;
34+ var type = component . GetType ( ) ;
5935
60- foreach ( var scene in scenes )
36+ if ( ! _typeCache . TryGetValue ( type , out var isInjectable ) )
6137 {
62- EditorSceneManager . OpenScene ( scene ) ;
63- AddComponentsToCurrentScene ( ) ;
64- EditorSceneManager . SaveOpenScenes ( ) ;
38+ var members = TypeHelpers . GetInjectableMembers ( type ) ;
39+ isInjectable = members . Count != 0 ;
40+ _typeCache . Add ( type , isInjectable ) ;
6541 }
6642
67- EditorSceneManager . OpenScene ( originalScenePath ) ;
68- }
69-
70- [ MenuItem ( "Tools / Injecter / Ensure injection scripts on everyting" ) ]
71- public static void AddComponentsToEverything ( )
72- {
73- AddComponentsToEveryPrefab ( ) ;
74- AddComponentsToEveryScene ( ) ;
43+ return isInjectable ;
7544 }
7645
7746 private static void AddScriptsToGameObject ( GameObject gameObject , string location )
7847 {
7948 var instances = gameObject
8049 . GetComponentsInChildren < MonoBehaviour > ( true )
81- . Where ( b => b != null )
82- . Where ( b =>
83- {
84- var type = b . GetType ( ) ;
85-
86- if ( ! _typeCache . TryGetValue ( type , out var isInjectable ) )
87- {
88- var members = TypeHelpers . GetInjectableMembers ( type ) ;
89- isInjectable = members . Count != 0 ;
90- _typeCache . Add ( type , isInjectable ) ;
91- }
92-
93- return isInjectable ;
94- } )
50+ . Where ( b => CanBeInjected ( b ) )
9551 . ToArray ( ) ;
9652
9753 for ( var i = 0 ; i < instances . Length ; i ++ )
9854 {
9955 var instance = instances [ i ] ;
100- EnsureComponent < MonoInjector > ( instance , location ) ;
101- EnsureComponent < MonoDisposer > ( instance , location ) ;
102- }
103- }
104-
105- private static void EnsureComponent < T > ( MonoBehaviour instance , string location ) where T : Component
106- {
107- if ( ! instance . gameObject . TryGetComponent < T > ( out var _ ) )
108- {
109- Debug . Log ( $ "Adding script: { typeof ( T ) . Name } to GameObjec: { instance . gameObject . name } at { location } ") ;
110- Undo . AddComponent < T > ( instance . gameObject ) ;
56+ GameObjectPatcher . EnsureComponentSafe < MonoInjector > ( instance , gameObject , location ) ;
57+ GameObjectPatcher . EnsureComponentSafe < MonoDisposer > ( instance , gameObject , location ) ;
11158 }
11259 }
11360 }
0 commit comments