88 using UnityEditor . Build ;
99 using UnityEditor . Build . Reporting ;
1010 using UnityEngine ;
11- using Object = UnityEngine . Object ;
1211
1312 [ InitializeOnLoad ]
1413 public class PrefabFolderStripper : IPreprocessBuildWithReport , IPostprocessBuildWithReport
1514 {
16- private static ( string path , string assetContent ) [ ] _changedPrefabs ;
17-
1815 static PrefabFolderStripper ( )
1916 {
2017 EditorApplication . playModeStateChanged += HandlePrefabsOnPlayMode ;
@@ -39,10 +36,12 @@ private static void HandlePrefabsOnPlayMode(PlayModeStateChange state)
3936 if ( ! StripSettings . StripFoldersFromPrefabsInPlayMode || StripSettings . PlayMode == StrippingMode . DoNothing )
4037 return ;
4138
39+ // Calling it not in EnteredPlayMode because scripts may instantiate prefabs in Awake or OnEnable
40+ // which happens before EnteredPlayMode.
4241 if ( state == PlayModeStateChange . ExitingEditMode )
4342 {
4443 // Stripping folders from all prefabs in the project instead of only the ones referenced in the scenes
45- // because a prefab can be hot-swapped in Play Mode.
44+ // because a prefab may be hot-swapped in Play Mode.
4645 StripFoldersFromAllPrefabs ( ) ;
4746 }
4847 else if ( state == PlayModeStateChange . EnteredEditMode )
@@ -60,21 +59,23 @@ private static void StripFoldersFromDependentPrefabs()
6059 AssetDatabase . GetLabels ( GetAssetForLabel ( path ) ) . Contains ( LabelHandler . FolderPrefabLabel ) )
6160 . ToArray ( ) ;
6261
63- _changedPrefabs = new ( string , string ) [ prefabsWithLabel . Length ] ;
62+ ChangedPrefabs . Initialize ( prefabsWithLabel . Length ) ;
6463
6564 for ( int i = 0 ; i < prefabsWithLabel . Length ; i ++ )
6665 {
6766 string path = prefabsWithLabel [ i ] ;
68- _changedPrefabs [ i ] = ( path , File . ReadAllText ( path ) ) ;
67+ ChangedPrefabs . Instance [ i ] = ( path , File . ReadAllText ( path ) ) ;
6968 StripFoldersFromPrefab ( path , StripSettings . Build ) ;
7069 }
70+
71+ // Serialization of ChangedPrefabs is not needed here because domain doesn't reload before changes are reverted.
7172 }
7273
7374 private static
7475#if UNITY_2020_1_OR_NEWER
7576 GUID
7677#else
77- Object
78+ UnityEngine . Object
7879#endif
7980 GetAssetForLabel( string path )
8081 {
@@ -88,16 +89,20 @@ private static
8889 private static void StripFoldersFromAllPrefabs ( )
8990 {
9091 var prefabGUIDs = AssetDatabase . FindAssets ( $ "l: { LabelHandler . FolderPrefabLabel } ") ;
91- _changedPrefabs = new ( string , string ) [ prefabGUIDs . Length ] ;
92+ ChangedPrefabs . Initialize ( prefabGUIDs . Length ) ;
9293
9394 for ( int i = 0 ; i < prefabGUIDs . Length ; i ++ )
9495 {
9596 string guid = prefabGUIDs [ i ] ;
9697 string path = AssetDatabase . GUIDToAssetPath ( guid ) ;
9798
98- _changedPrefabs [ i ] = ( path , File . ReadAllText ( path ) ) ;
99+ ChangedPrefabs . Instance [ i ] = ( path , File . ReadAllText ( path ) ) ;
99100 StripFoldersFromPrefab ( path , StripSettings . PlayMode ) ;
100101 }
102+
103+ // If domain reload is enabled in Play Mode Options, serialization of the changed prefabs is necessary
104+ // so that changes can be reverted after leaving play mode.
105+ ChangedPrefabs . SerializeIfNeeded ( ) ;
101106 }
102107
103108 private static void StripFoldersFromPrefab ( string prefabPath , StrippingMode strippingMode )
@@ -115,7 +120,7 @@ private static void StripFoldersFromPrefab(string prefabPath, StrippingMode stri
115120
116121 private static void RevertChanges ( )
117122 {
118- foreach ( ( string path , string content ) in _changedPrefabs )
123+ foreach ( ( string path , string content ) in ChangedPrefabs . Instance )
119124 {
120125 File . WriteAllText ( path , content ) ;
121126 }
0 commit comments