Game.Prefabs.Modes.ServiceUpgradeGlobalMode
Assembly: Assembly-CSharp (typical for game scripts)
Namespace: Game.Prefabs.Modes
Type: class
Base: EntityQueryModePrefab
Summary:
ServiceUpgradeGlobalMode is a mode prefab that applies global multipliers to service upgrade component data (ServiceUpgradeData) across matching entities. It implements a Burst-compiled IJobChunk (ModeJob) to multiply upgrade cost and XP reward values in parallel, and it can restore default values from the original prefab data using the PrefabSystem.
Fields
-
private struct ModeJob.ModeJob
(nested struct)
ModeJob is a nested, [BurstCompile]-annotated IJobChunk implementation that performs the actual component data modifications per chunk. It contains the job-local parameters and the ComponentTypeHandle used to access ServiceUpgradeData. -
private float m_UpgradeCostMultiplier
(inside ModeJob)
Multiplier applied to ServiceUpgradeData.m_UpgradeCost inside the job. -
private float m_XPRewardMultiplier
(inside ModeJob)
Multiplier applied to ServiceUpgradeData.m_XPReward inside the job. -
private ComponentTypeHandle<ServiceUpgradeData> m_ServiceUpgradeType
(inside ModeJob)
ComponentTypeHandle used to read/write ServiceUpgradeData in the job (isReadOnly: false when scheduled). -
public float m_UpgradeCostMultiplier
Public field on the prefab used to configure the upgrade cost multiplier for the mode. This value is copied into the job when ApplyModeData is called. -
public float m_XPRewardMultiplier
Public field on the prefab used to configure the XP reward multiplier for the mode. This value is copied into the job when ApplyModeData is called.
Properties
- (none)
This class does not declare any C# properties. It uses public fields for configurable multipliers.
Constructors
public ServiceUpgradeGlobalMode()
No explicit constructor is defined in the source; the default parameterless constructor is used. Instances are typically created/managed by the prefab system and Unity's serialization.
Methods
public override EntityQueryDesc GetEntityQueryDesc()
Returns an EntityQueryDesc that targets entities containing ServiceUpgradeData and ServiceUpgradeBuilding (both read-only in the query). This determines which entities the mode will process.
Notes:
- The query's All array contains ComponentType.ReadOnly
-
protected override void RecordChanges(EntityManager entityManager, Entity entity)
Records the current component data for the given entity so changes can be tracked (implementation calls entityManager.GetComponentData(entity)). This method will read the component to ensure the original value is captured by the prefab mode system. -
public override JobHandle ApplyModeData(EntityManager entityManager, EntityQuery requestedQuery, JobHandle deps)
Schedules the ModeJob in parallel across chunks matching requestedQuery. The job instance is constructed with the prefab's multipliers and a writable ComponentTypeHandleretrieved from the provided EntityManager.
Important details:
- ModeJob is scheduled via JobChunkExtensions.ScheduleParallel(...)
- ComponentTypeHandle
public override void RestoreDefaultData(EntityManager entityManager, ref NativeArray<Entity> entities, PrefabSystem prefabSystem)
Restores ServiceUpgradeData values to their defaults from the prefab. For each entity in the provided array:- Attempts to get the prefab (PrefabBase) via prefabSystem.TryGetPrefab
(entity, out prefabBase). - Then attempts to get the exact ServiceUpgrade component instance from the prefab via prefabBase.TryGetExactly
(out component). - If successful, copies component.m_UpgradeCost and component.m_XPReward back to the entity's ServiceUpgradeData component via entityManager.SetComponentData.
-
If not found, emits a warning through ComponentBase.baseLog.Warn including the prefab/class/entity info and continues.
-
private struct ModeJob : IJobChunk
(nested)
ModeJob.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
Iterates all ServiceUpgradeData elements in chunk and applies: - m_UpgradeCost = (uint)((float)m_UpgradeCost * m_UpgradeCostMultiplier)
- m_XPReward = (int)((float)m_XPReward * m_XPRewardMultiplier)
Implementation details: - Uses chunk.GetNativeArray(ref m_ServiceUpgradeType) to access the array of ServiceUpgradeData. - Loops from 0 to chunk.Count and updates each element in place. - Has explicit IJobChunk.Execute entry that forwards to the declared Execute for Burst compilation.
Usage Example
// Example usage from a system or manager that has access to an EntityManager and dependencies
// Assume 'modePrefab' is an instantiated ServiceUpgradeGlobalMode prefab reference.
ServiceUpgradeGlobalMode modePrefab = /* obtain from prefab system or inspector */;
modePrefab.m_UpgradeCostMultiplier = 0.9f; // reduce upgrade costs by 10%
modePrefab.m_XPRewardMultiplier = 1.2f; // increase XP reward by 20%
EntityQueryDesc desc = modePrefab.GetEntityQueryDesc();
EntityQuery query = entityManager.CreateEntityQuery(desc);
// Schedule the job to apply changes. 'deps' is the current JobHandle dependency.
JobHandle handle = modePrefab.ApplyModeData(entityManager, query, deps);
// Use the returned handle for further dependencies
deps = handle;
Notes and tips: - The ModeJob is Burst-compiled for performance; ensure Burst and Jobs packages are available and configured. - Because ApplyModeData schedules a parallel job with a non-readonly ComponentTypeHandle, ensure no conflicting writes occur from other systems at the same time. - RestoreDefaultData relies on the prefab data being available via PrefabSystem; missing prefab entries are logged and skipped.