99using System . Text ;
1010using UnityEditor ;
1111using UnityEngine ;
12+ using UnityEditor . Callbacks ;
1213
1314
1415namespace SatorImaging . UnitySourceGenerator
@@ -19,6 +20,8 @@ public class USGEngine : AssetPostprocessor
1920 public static bool IgnoreOverwriteSettingByAttribute = false ;
2021
2122
23+ const string EDITOR_PREFS_LENGTH = "__STMG_USG__TARGET_LENGTH" ;
24+ const string EDITOR_PREFS_PREFIX = "__STMG_USG__TARGET_" ;
2225 const int BUFFER_LENGTH = 61_440 ;
2326 const int BUFFER_MAX_CHAR_LENGTH = BUFFER_LENGTH / 3 ; // worst case of UTF-8
2427 const string GENERATOR_PREFIX = "." ;
@@ -64,24 +67,40 @@ static void OnPostprocessAllAssets(
6467 // menu command but OnPostprocessAllAssets event doesn't work as expected.
6568 // (script runs with static field cleared even though .Clear() is only in ProcessingFiles().
6669 // 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 ++ )
6873 {
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 ( ) ;
7084 } ;
7185 }
7286
7387
7488 readonly static HashSet < string > s_updatedGeneratorNames = new ( ) ;
75- static void ProcessingFiles ( string [ ] targetPaths )
89+ static void ProcessingFiles ( )
7690 {
7791 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 ++ )
7996 {
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 ) ;
83102
84- if ( ProcessFile ( targetPaths [ i ] ) )
103+ if ( ProcessFile ( path ) )
85104 somethingUpdated = true ;
86105 }
87106
0 commit comments