11namespace UnityHierarchyFolders . Editor
22{
3+ using System ;
34 using System . IO ;
45 using System . Linq ;
56 using Runtime ;
67 using UnityEditor ;
78 using UnityEditor . Build ;
89 using UnityEditor . Build . Reporting ;
10+ using UnityEngine ;
11+ using Object = UnityEngine . Object ;
912
1013 [ InitializeOnLoad ]
1114 public class PrefabFolderStripper : IPreprocessBuildWithReport , IPostprocessBuildWithReport
@@ -36,7 +39,6 @@ private static void HandlePrefabsOnPlayMode(PlayModeStateChange state)
3639 if ( ! StripSettings . StripFoldersFromPrefabsInPlayMode || StripSettings . PlayMode == StrippingMode . DoNothing )
3740 return ;
3841
39-
4042 if ( state == PlayModeStateChange . ExitingEditMode )
4143 {
4244 // Stripping folders from all prefabs in the project instead of only the ones referenced in the scenes
@@ -55,7 +57,7 @@ private static void StripFoldersFromDependentPrefabs()
5557 var dependentAssetsPaths = AssetDatabase . GetDependencies ( scenePaths , true ) ;
5658
5759 var prefabsWithLabel = dependentAssetsPaths . Where ( path =>
58- AssetDatabase . GetLabels ( AssetDatabase . GUIDFromAssetPath ( path ) ) . Contains ( LabelHandler . FolderPrefabLabel ) )
60+ AssetDatabase . GetLabels ( GetAssetForLabel ( path ) ) . Contains ( LabelHandler . FolderPrefabLabel ) )
5961 . ToArray ( ) ;
6062
6163 _changedPrefabs = new ( string , string ) [ prefabsWithLabel . Length ] ;
@@ -68,6 +70,21 @@ private static void StripFoldersFromDependentPrefabs()
6870 }
6971 }
7072
73+ private static
74+ #if UNITY_2020_1_OR_NEWER
75+ GUID
76+ #else
77+ Object
78+ #endif
79+ GetAssetForLabel( string path )
80+ {
81+ #if UNITY_2020_1_OR_NEWER
82+ return AssetDatabase . GUIDFromAssetPath ( path ) ;
83+ #else
84+ return AssetDatabase . LoadAssetAtPath < GameObject > ( path ) ;
85+ #endif
86+ }
87+
7188 private static void StripFoldersFromAllPrefabs ( )
7289 {
7390 var prefabGUIDs = AssetDatabase . FindAssets ( $ "l: { LabelHandler . FolderPrefabLabel } ") ;
@@ -85,9 +102,9 @@ private static void StripFoldersFromAllPrefabs()
85102
86103 private static void StripFoldersFromPrefab ( string prefabPath , StrippingMode strippingMode )
87104 {
88- using ( var temp = new PrefabUtility . EditPrefabContentsScope ( prefabPath ) )
105+ using ( var temp = new EditPrefabContentsScope ( prefabPath ) )
89106 {
90- var folders = temp . prefabContentsRoot . GetComponentsInChildren < Folder > ( ) ;
107+ var folders = temp . PrefabContentsRoot . GetComponentsInChildren < Folder > ( ) ;
91108
92109 foreach ( Folder folder in folders )
93110 {
@@ -103,5 +120,27 @@ private static void RevertChanges()
103120 File . WriteAllText ( path , content ) ;
104121 }
105122 }
123+
124+ /// <summary>
125+ /// A copy of <see cref="PrefabUtility.EditPrefabContentsScope"/> for backwards compatibility with Unity 2019.
126+ /// </summary>
127+ private readonly struct EditPrefabContentsScope : IDisposable
128+ {
129+ public readonly GameObject PrefabContentsRoot ;
130+
131+ private readonly string _assetPath ;
132+
133+ public EditPrefabContentsScope ( string assetPath )
134+ {
135+ PrefabContentsRoot = PrefabUtility . LoadPrefabContents ( assetPath ) ;
136+ _assetPath = assetPath ;
137+ }
138+
139+ public void Dispose ( )
140+ {
141+ PrefabUtility . SaveAsPrefabAsset ( PrefabContentsRoot , _assetPath ) ;
142+ PrefabUtility . UnloadPrefabContents ( PrefabContentsRoot ) ;
143+ }
144+ }
106145 }
107146}
0 commit comments