Game.Prefabs.Modes.TreeObjectGlobalMode
Assembly: Assembly-CSharp
Namespace: Game.Prefabs.Modes
Type: class
Base: EntityQueryModePrefab
Summary:
A prefab mode that globally adjusts tree resource (wood) yields for tree objects in the ECS world. It defines a Burst-compiled IJobChunk (ModeJob) that multiplies each entity's TreeData.m_WoodAmount by a configurable multiplier. The mode also provides query information to select tree entities and a restore path that copies default m_WoodAmount values from the prefab definitions back onto entities.
Fields
private struct ModeJob
Burst-compiled nested job implementing IJobChunk. It contains:public float m_WoodAmountMultiplier
— multiplier applied to TreeData.m_WoodAmount for each tree in the chunk.-
public ComponentTypeHandle<TreeData> m_TreeType
— component type handle used to read/write TreeData arrays in the chunk. The job iterates the chunk's TreeData array and multiplies each element's m_WoodAmount by m_WoodAmountMultiplier. The job is scheduled with ScheduleParallel for scalability. -
public float m_WoodAmountMultiplier
Multiplier (set on the prefab) used by ModeJob to scale each tree's wood yield. Typical values: 1.0 (no change), >1 to increase wood, <1 to decrease.
Properties
- (none)
Constructors
public TreeObjectGlobalMode()
Default parameterless constructor (compiler-generated). Instances are normally created/managed by the prefab system.
Methods
public override EntityQueryDesc GetEntityQueryDesc()
Returns an EntityQueryDesc that matches entities having all of the following components (read-only):- PlantData
- TreeData
-
GrowthScaleData This restricts the mode to tree-like plant entities only.
-
protected override void RecordChanges(EntityManager entityManager, Entity entity)
Calls entityManager.GetComponentData(entity). This reads the TreeData for the given entity so the prefab system can record/track changes (ensures the component is accessed so the system knows this mode touches TreeData). -
public override JobHandle ApplyModeData(EntityManager entityManager, EntityQuery requestedQuery, JobHandle deps)
Schedules the nested ModeJob in parallel across the supplied entity query. ModeJob is constructed with: - m_WoodAmountMultiplier taken from this.m_WoodAmountMultiplier
-
m_TreeType obtained from entityManager.GetComponentTypeHandle
(isReadOnly: false) Returns the JobHandle for the scheduled burst job. Use this to chain dependencies. -
public override void RestoreDefaultData(EntityManager entityManager, ref NativeArray<Entity> entities, PrefabSystem prefabSystem)
Restores default TreeData.m_WoodAmount values on the provided entities by: - Iterating the entities array
- Attempting to fetch the prefab (PrefabBase) for each entity via prefabSystem.TryGetPrefab
- Attempting to get the exact TreeObject component from the prefab (prefabBase.TryGetExactly
) -
If successful, copying component.m_WoodAmount from the prefab's TreeObject into the entity's TreeData and setting the component on the entity If the prefab or TreeObject is not found the method logs a warning using ComponentBase.baseLog.Warn and continues. This method reverts runtime multiplier changes back to original prefab defaults.
-
(nested)
ModeJob.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
Implements the logic to iterate the chunk's TreeData NativeArray and scale each element's m_WoodAmount by m_WoodAmountMultiplier. The job explicitly implements IJobChunk.Execute and delegates to the typed Execute method.
Usage Example
// Example: set the multiplier on the prefab instance (e.g., in editor or prefab initializer)
// and let the prefab/mode system call ApplyModeData automatically.
//
// If creating dynamically:
var mode = new Game.Prefabs.Modes.TreeObjectGlobalMode
{
m_WoodAmountMultiplier = 1.5f // increase all tree wood yields by 50%
};
// The prefab system will call GetEntityQueryDesc(), then ApplyModeData(...) with the resulting query.
// ApplyModeData schedules a Burst IJobChunk (ModeJob) that multiplies TreeData.m_WoodAmount values.
Notes and tips: - ModeJob is Burst-compiled and scheduled in parallel; ensure TreeData writing is safe (the job uses a ComponentTypeHandle obtained with isReadOnly:false). - GetEntityQueryDesc restricts the job to entities that also have PlantData and GrowthScaleData, preventing accidental application to non-tree entities. - RestoreDefaultData relies on prefab metadata; if prefabs are missing or don't contain a TreeObject component, defaults cannot be restored and a warning is logged.