11using UnityEngine ;
22using UnityEditor ;
3- using RustMapEditor . Data ;
43using static WorldSerialization ;
54using Unity . EditorCoroutines . Editor ;
65using System . Collections ;
76using System . Linq ;
7+ using System . Collections . Generic ;
88
99public static class PrefabManager
1010{
1111 public static GameObject DefaultPrefab { get ; private set ; }
1212 public static Transform PrefabParent { get ; private set ; }
1313 public static GameObject PrefabToSpawn ;
14+
1415 public static PrefabDataHolder [ ] CurrentMapPrefabs { get => PrefabParent . gameObject . GetComponentsInChildren < PrefabDataHolder > ( ) ; }
1516
17+ public static Dictionary < string , Transform > PrefabCategories = new Dictionary < string , Transform > ( ) ;
18+
1619 public static bool IsChangingPrefabs { get ; private set ; }
1720
1821 [ InitializeOnLoadMethod ]
@@ -50,6 +53,18 @@ public static GameObject Load(uint id)
5053 return Load ( AssetManager . ToPath ( id ) ) ;
5154 }
5255
56+ public static Transform GetParent ( string category )
57+ {
58+ if ( PrefabCategories . TryGetValue ( category , out Transform transform ) )
59+ return transform ;
60+
61+ var obj = GameObject . Instantiate ( Resources . Load < GameObject > ( "Prefabs/PrefabCategory" ) , PrefabParent , false ) ;
62+ obj . transform . localPosition = Vector3 . zero ;
63+ obj . name = category ;
64+ PrefabCategories . Add ( category , obj . transform ) ;
65+ return obj . transform ;
66+ }
67+
5368 /// <summary>Sets up the prefabs loaded from the bundle file for use in the editor.</summary>
5469 /// <param name="go">GameObject to process, should be from one of the asset bundles.</param>
5570 /// <param name="filePath">Asset filepath of the gameobject, used to get and set the PrefabID.</param>
@@ -165,7 +180,7 @@ public static IEnumerator SpawnPrefabs(PrefabData[] prefabs, int progressID)
165180 Progress . Report ( progressID , ( float ) i / prefabs . Length , "Spawning Prefabs: " + i + " / " + prefabs . Length ) ;
166181 sw . Restart ( ) ;
167182 }
168- Spawn ( Load ( prefabs [ i ] . id ) , prefabs [ i ] , PrefabParent ) ;
183+ Spawn ( Load ( prefabs [ i ] . id ) , prefabs [ i ] , GetParent ( prefabs [ i ] . category ) ) ;
169184 }
170185 Progress . Report ( progressID , 0.99f , "Spawned " + prefabs . Length + " prefabs." ) ;
171186 Progress . Finish ( progressID , Progress . Status . Succeeded ) ;
@@ -206,7 +221,7 @@ public static IEnumerator ReplaceWithLoaded(PrefabDataHolder[] prefabs, int prog
206221 Progress . Report ( progressID , ( float ) i / prefabs . Length , "Replacing Prefabs: " + i + " / " + prefabs . Length ) ;
207222 sw . Restart ( ) ;
208223 }
209- Spawn ( Load ( prefabs [ i ] . prefabData . id ) , prefabs [ i ] . prefabData , PrefabParent ) ;
224+ Spawn ( Load ( prefabs [ i ] . prefabData . id ) , prefabs [ i ] . prefabData , GetParent ( prefabs [ i ] . prefabData . category ) ) ;
210225 GameObject . DestroyImmediate ( prefabs [ i ] . gameObject ) ;
211226 }
212227 Progress . Report ( progressID , 0.99f , "Replaced " + prefabs . Length + " prefabs." ) ;
@@ -228,7 +243,7 @@ public static IEnumerator ReplaceWithDefault(PrefabDataHolder[] prefabs, int pro
228243 Progress . Report ( progressID , ( float ) i / prefabs . Length , "Replacing Prefabs: " + i + " / " + prefabs . Length ) ;
229244 sw . Restart ( ) ;
230245 }
231- Spawn ( DefaultPrefab , prefabs [ i ] . prefabData , PrefabParent ) ;
246+ Spawn ( DefaultPrefab , prefabs [ i ] . prefabData , GetParent ( prefabs [ i ] . prefabData . category ) ) ;
232247 GameObject . DestroyImmediate ( prefabs [ i ] . gameObject ) ;
233248 }
234249 Progress . Report ( progressID , 0.99f , "Replaced " + prefabs . Length + " prefabs." ) ;
0 commit comments