9
9
using System . Text ;
10
10
using UnityEditor ;
11
11
using UnityEngine ;
12
+ using UnityEditor . Callbacks ;
12
13
13
14
14
15
namespace SatorImaging . UnitySourceGenerator
@@ -19,6 +20,8 @@ public class USGEngine : AssetPostprocessor
19
20
public static bool IgnoreOverwriteSettingByAttribute = false ;
20
21
21
22
23
+ const string EDITOR_PREFS_LENGTH = "__STMG_USG__TARGET_LENGTH" ;
24
+ const string EDITOR_PREFS_PREFIX = "__STMG_USG__TARGET_" ;
22
25
const int BUFFER_LENGTH = 61_440 ;
23
26
const int BUFFER_MAX_CHAR_LENGTH = BUFFER_LENGTH / 3 ; // worst case of UTF-8
24
27
const string GENERATOR_PREFIX = "." ;
@@ -64,24 +67,40 @@ static void OnPostprocessAllAssets(
64
67
// menu command but OnPostprocessAllAssets event doesn't work as expected.
65
68
// (script runs with static field cleared even though .Clear() is only in ProcessingFiles().
66
69
// it's weird that event happens and asset paths retrieved but hashset items gone.)
67
- ////EditorApplication.delayCall += () =>
70
+ // NOTE: Use EditorPrefs as a temporary storage.
71
+ var nPaths = 0 ;
72
+ for ( int i = 0 ; i < importedAssets . Length ; i ++ )
68
73
{
69
- ProcessingFiles ( importedAssets ) ;
74
+ if ( ! IsAppropriateTarget ( importedAssets [ i ] ) ) continue ;
75
+ EditorPrefs . SetString ( EDITOR_PREFS_PREFIX + nPaths ++ , importedAssets [ i ] ) ;
76
+ Debug . Log ( $ "[USG]: Saved into EditorPrefs: { importedAssets [ i ] } ") ;
77
+ }
78
+ EditorPrefs . SetInt ( EDITOR_PREFS_LENGTH , nPaths ) ;
79
+
80
+ // NOTE: [DidReloadScripts] is executed before AssetPostprocessor, cannot be used.
81
+ EditorApplication . delayCall += ( ) =>
82
+ {
83
+ ProcessingFiles ( ) ;
70
84
} ;
71
85
}
72
86
73
87
74
88
readonly static HashSet < string > s_updatedGeneratorNames = new ( ) ;
75
- static void ProcessingFiles ( string [ ] targetPaths )
89
+ static void ProcessingFiles ( )
76
90
{
77
91
bool somethingUpdated = false ;
78
- for ( int i = 0 ; i < targetPaths . Length ; i ++ )
92
+
93
+ var nPaths = EditorPrefs . GetInt ( EDITOR_PREFS_LENGTH , 0 ) ;
94
+ EditorPrefs . DeleteKey ( EDITOR_PREFS_LENGTH ) ;
95
+ for ( int i = 0 ; i < nPaths ; i ++ )
79
96
{
80
- // NOTE: Do NOT early return in this method.
81
- // check path here to allow generator class can be lie outside of Assets/ folder.
82
- if ( ! IsAppropriateTarget ( targetPaths [ i ] ) ) continue ;
97
+ var key = EDITOR_PREFS_PREFIX + i ;
98
+ //if (!EditorPrefs.HasKey(key)) continue;
99
+
100
+ var path = EditorPrefs . GetString ( key ) ;
101
+ EditorPrefs . DeleteKey ( key ) ;
83
102
84
- if ( ProcessFile ( targetPaths [ i ] ) )
103
+ if ( ProcessFile ( path ) )
85
104
somethingUpdated = true ;
86
105
}
87
106
0 commit comments