Game.Prefabs.RandomLocalization
Assembly:
Game (Game assembly / modding runtime)
Namespace:
Game.Prefabs
Type:
Class
Base:
Localization
Summary:
RandomLocalization is a prefab component used to wire up per-prefab localization data for randomized/localizable prefabs. It ensures the prefab/archetype has the necessary ECS components (LocalizationCount and RandomLocalizationIndex) and, at initialization time, writes the number of available localization entries for the prefab into the entity's LocalizationCount buffer. If the given localization ID is missing or has no entries, a warning is emitted via ComponentBase.baseLog.
Fields
- None declared in this class.
This class does not introduce any new instance fields; it relies on members inherited from Localization and interacts with ECS components (LocalizationCount and RandomLocalizationIndex) rather than storing additional managed fields.
Properties
- None declared in this class.
No new managed properties are exposed by RandomLocalization. It does, however, produce and rely on ECS component data (see Methods/Initialize).
Constructors
public RandomLocalization()
Default parameterless constructor (implicit). RandomLocalization has no explicit constructor in the source; the default is used.
Methods
-
public override void GetPrefabComponents(HashSet<ComponentType> components)
Adds required prefab-level component types to the provided set. Calls the base implementation then adds ComponentType.ReadWrite(). This ensures instances of the prefab carry a LocalizationCount dynamic buffer. -
public override void GetArchetypeComponents(HashSet<ComponentType> components)
Adds required archetype-level component types to the provided set. Calls the base implementation then adds ComponentType.ReadWrite(). This ensures the archetype includes the RandomLocalizationIndex component used at runtime for randomized selection. -
public override void Initialize(EntityManager entityManager, Entity entity)
Called during prefab initialization to set up ECS component data on the created entity. This implementation: - Calls base.Initialize(...)
- Calls GetLocalizationCount() to obtain how many localization entries are available for the prefab
- Adds a LocalizationCount value to the entity's LocalizationCount buffer with m_Count set to that number
This is where the prefab writes the computed count into ECS so systems can access it at runtime.
-
protected virtual int GetLocalizationCount()
Returns the number of localization indices available for this prefab. The default implementation delegates to GetLocalizationIndexCount(base.prefab, m_LocalizationID). This method is overridable so derived classes can change how the count is determined (for example, to clamp or filter entries). -
public static int GetLocalizationIndexCount(PrefabBase prefab, string id)
Lookup helper that queries GameManager.instance.localizationManager.activeDictionary.indexCounts for the given localization id and returns the count found. Behavior: - If id is null or not present in the indexCounts dictionary, returns -1 (or a value less than 1) and logs a warning: ComponentBase.baseLog.WarnFormat(prefab, "Warning: localizationID {0} not found for {1}", id, prefab.name);
- Otherwise returns the stored index count (int).
This is the central utility used by GetLocalizationCount to obtain the available localization entry count for a given prefab/localization ID.
Usage Example
// Example override to clamp localization count to a maximum:
public class MyRandomLocalization : RandomLocalization
{
protected override int GetLocalizationCount()
{
int count = base.GetLocalizationCount();
// ensure at least 1 and no more than 10 localizations are considered
if (count < 1) return 0;
return Math.Min(count, 10);
}
}
// Typical initialization behavior is handled by the base class:
// When the prefab is instantiated during scene/prefab setup, Initialize(...) will
// add a LocalizationCount entry to the entity buffer so runtime systems can read it.
Additional notes: - Dependencies: GameManager.instance.localizationManager.activeDictionary.indexCounts, ECS types LocalizationCount and RandomLocalizationIndex, and base Localization implementation. - Logging: Missing or invalid localization IDs are reported using ComponentBase.baseLog.WarnFormat(prefab, ...). - Intended use: attach to prefabs that should support randomized or indexed localization entries so systems can pick a localized variant at spawn/runtime.