Game.Prefabs.PrefabUtils
Assembly:
Namespace: Game.Prefabs
Type: public static class
Base: System.Object
Summary:
Utility helpers for working with prefab-related data and unlocks in Cities: Skylines 2 mod code. Provides small convenience conversions (HashSet -> array), helpers to check whether unlocked prefabs contain specific component types using ECS EntityManager/EntityQuery, and a helper to obtain DLC/content prerequisite names from a PrefabBase. The class relies on Unity.Entities and Unity.Collections APIs and is intended for use in systems or other code that has access to an EntityManager and unlock EntityQuery.
Fields
- This type defines no instance or static fields.
Utility class; all functionality is exposed via static methods.
Properties
- This type defines no properties.
All functionality is provided through static methods.
Constructors
- This static class has no public constructors.
Being a static utility class, it cannot be instantiated and contains only static methods.
Methods
-
public static T[] ToArray<T>(System.Collections.Generic.HashSet<T> hashSet)
Converts a HashSetinto a T[] by allocating a new array sized to hashSet.Count and copying the contents. Useful when an array is required by an API or for stable indexed iteration. This performs an allocation for the returned array. -
public static bool HasUnlockedPrefab<T>(Unity.Entities.EntityManager entityManager, Unity.Entities.EntityQuery unlockQuery) where T : unmanaged
Checks whether any entity returned by unlockQuery (expected to contain Unlock components) corresponds to a prefab entity that has component type T. Implementation details: - Returns false immediately if unlockQuery.IsEmptyIgnoreFilter is true.
- Calls unlockQuery.ToComponentDataArray
(Allocator.TempJob) to obtain a NativeArray , iterates it, and checks entityManager.HasComponent (unlock.m_Prefab) for each entry. - Disposes the NativeArray in a finally block to ensure no leak.
-
Returns true on first matching prefab; false otherwise. Notes: T must be an unmanaged component type. This method temporarily allocates a NativeArray (Allocator.TempJob) and relies on the caller having a valid EntityManager and an appropriate unlockQuery that yields Unlock components.
-
public static bool HasUnlockedPrefabAll<T1, T2>(Unity.Entities.EntityManager entityManager, Unity.Entities.EntityQuery unlockQuery) where T1 : unmanaged where T2 : unmanaged
Similar to HasUnlockedPrefabbut returns true when a prefab from the unlockQuery has both component types T1 and T2 (i.e., entityManager.HasComponent (prefab) && entityManager.HasComponent (prefab)). Uses the same pattern with ToComponentDataArray and proper disposal. -
public static bool HasUnlockedPrefabAny<T1, T2, T3, T4>(Unity.Entities.EntityManager entityManager, Unity.Entities.EntityQuery unlockQuery) where T1 : unmanaged where T2 : unmanaged where T3 : unmanaged where T4 : unmanaged
Checks if any unlocked prefab has at least one of the four component types T1..T4 (logical OR). Same allocation/iteration/disposal pattern as the other HasUnlockedPrefab methods. -
[CanBeNull] public static string GetContentPrerequisite(PrefabBase prefab)
Attempts to retrieve a DLC/content prerequisite name for the given PrefabBase: - Uses prefab.TryGet
(out var component) to obtain a ContentPrerequisite component if present. - If found, attempts component.m_ContentPrerequisite.TryGet
(out var requirement) and, if present, returns PlatformManager.instance.GetDlcName(requirement.m_Dlc). - Returns null when no ContentPrerequisite or DlcRequirement is present. Notes: Returns the localized DLC name string via PlatformManager. Marked [CanBeNull] to indicate it may return null.
Usage Example
// Convert a HashSet to an array
var set = new HashSet<string> { "a", "b", "c" };
string[] arr = PrefabUtils.ToArray(set);
// Example usage in a system or manager with an EntityManager and an unlockQuery already created:
// Check whether any unlocked prefab has a specific component type (e.g., MyComponent)
bool has = PrefabUtils.HasUnlockedPrefab<MyComponent>(entityManager, unlockQuery);
// Check whether any unlocked prefab has both components A and B
bool hasBoth = PrefabUtils.HasUnlockedPrefabAll<AComponent, BComponent>(entityManager, unlockQuery);
// Check whether any unlocked prefab has any of four components
bool hasAny = PrefabUtils.HasUnlockedPrefabAny<C1, C2, C3, C4>(entityManager, unlockQuery);
// Get DLC prerequisite name (may return null)
string dlcName = PrefabUtils.GetContentPrerequisite(prefab);
if (dlcName != null)
{
// prefab requires DLC with name contained in dlcName
}
Additional notes: - The HasUnlockedPrefab methods allocate a NativeArray with Allocator.TempJob; the implementation disposes it in a finally block, but be mindful of calling context and job-safety rules when using Allocator.TempJob. - All HasUnlockedPrefab methods require unmanaged component types for the generic constraints and use EntityManager.HasComponent to test prefab entities referenced by Unlock.m_Prefab.