Game.Prefabs.AssetCollection
Assembly: Assembly-CSharp
Namespace: Game.Prefabs
Type: class
Base: UnityEngine.ScriptableObject
Summary:
AssetCollection is a ScriptableObject container used to group PrefabBase entries and nested AssetCollection references. It can be created from the Unity menu (Create → Colossal → Prefabs → AssetCollection) and is intended to aggregate and register prefabs into a PrefabSystem via AddPrefabsTo. It also provides an inspector context menu to sort its prefab list alphabetically.
Fields
-
public bool isActive
Controls whether this collection participates when AddPrefabsTo is called. If false, AddPrefabsTo will return immediately and no prefabs from this collection (or its nested collections) will be added. -
public List<PrefabBase> m_Prefabs
A list of prefab assets held by this collection. Intended to be populated in the inspector. Note: the field is not explicitly initialized in code; in Unity this will be serialized/initialized by the engine but code that creates instances at runtime should ensure the list is not null before accessing it. -
public List<AssetCollection> m_Collections
A list of nested AssetCollection objects. These nested collections are traversed recursively by AddPrefabsTo to add more prefabs to a PrefabSystem.
Properties
public int Count { get; }
Returns the number of entries in m_Prefabs (m_Prefabs.Count). Be aware that if m_Prefabs is null, accessing Count will throw a NullReferenceException; ensure the list is initialized before using this property at runtime.
Constructors
public AssetCollection()
Default parameterless constructor (compiler-provided). The source does not explicitly initialize fields; lists rely on Unity serialization or must be initialized by the caller if created at runtime.
Methods
public void AddPrefabsTo(PrefabSystem prefabSystem)
Adds all prefabs in this collection to the provided PrefabSystem by calling prefabSystem.AddPrefab(prefab, base.name) for each PrefabBase in m_Prefabs. If isActive is false the method returns immediately. After adding its own prefabs, it iterates m_Collections and calls AddPrefabsTo on each nested collection (recursive traversal). Notes and caveats:- The method does not perform null checks on m_Prefabs or m_Collections; if either is null, a NullReferenceException may occur. Ensure lists are initialized.
- Calls to PrefabSystem should be made on the main thread as they interact with Unity objects.
-
The second argument passed to AddPrefab is base.name (the ScriptableObject's name), which helps attribute prefabs to this collection.
-
public void SortAssets()
Sorts m_Prefabs in-place using an ordinal string comparison of prefab names (string.Compare(a.name, b.name, StringComparison.Ordinal)). The method is exposed to the Unity inspector via the ContextMenuItem attribute with the label "Sort Assets Alphabetically", allowing designers to sort the list from the inspector UI.
Usage Example
// Create or reference an AssetCollection (e.g. assigned via inspector)
public AssetCollection assetCollection;
public PrefabSystem prefabSystem;
void Start()
{
// Ensure lists are initialized if the collection was created at runtime
if (assetCollection.m_Prefabs == null)
assetCollection.m_Prefabs = new List<PrefabBase>();
if (assetCollection.m_Collections == null)
assetCollection.m_Collections = new List<AssetCollection>();
// Register all prefabs from this collection (and nested collections) with the PrefabSystem
if (assetCollection.isActive)
{
assetCollection.AddPrefabsTo(prefabSystem);
}
}
Additional notes: - Because AssetCollection is a ScriptableObject, create instances via the Unity asset menu (Create → Colossal → Prefabs → AssetCollection) so lists are serialized and initialized by the editor. - Consider adding defensive null checks or initializing lists in code if you instantiate AssetCollection at runtime to avoid NullReferenceExceptions.