Game.CheckPrefabReferencesSystem
Assembly:
Namespace: Game.Serialization
Type: class
Base: GameSystemBase
Summary:
A Unity ECS system used during serialization to verify which prefab entities are referenced and to enable their PrefabData component accordingly. The system schedules a parallel job (CheckPrefabReferencesJob) that iterates over a NativeArray
Fields
-
private Unity.Collections.NativeArray<Entity> m_PrefabArray
Holds the array of prefab entities to check. Supplied via BeginPrefabCheck and consumed by the scheduled job. Cleared/reset by EndPrefabCheck. -
private Unity.Collections.LowLevel.Unsafe.UnsafeList<bool> m_ReferencedPrefabs
A temporary per-prefab boolean list indicating whether a prefab was referenced. Allocated in BeginPrefabCheck with Allocator.TempJob, resized to match the prefab array length and cleared. Accessed from the scheduled job and disposed in EndPrefabCheck using the combined dependencies. -
private Unity.Jobs.JobHandle m_DataDeps
Tracks data dependencies that must be satisfied before the check/scheduling. Provided into BeginPrefabCheck (dependencies parameter) and used to combine dependencies for scheduling and disposal. -
private Unity.Jobs.JobHandle m_UserDeps
Aggregates user-side job dependencies added by callers via AddPrefabReferencesUser. Combined with m_DataDeps and the system base dependency when scheduling the job. -
private System.Boolean m_IsLoading
Flag indicating whether the operation is part of a loading process. Passed into PrefabReferences returned by GetPrefabReferences, so callers can adapt behavior based on loading vs runtime.
Properties
This type exposes no public properties.
Constructors
public CheckPrefabReferencesSystem()
Default constructor. Marked with [Preserve] (to avoid code stripping). No special initialization logic is performed in the constructor; the system's working state is set up when BeginPrefabCheck is called.
Methods
protected override void OnUpdate()
Schedules the CheckPrefabReferencesJob as an IJobParallelFor. The job is constructed with:- m_PrefabArray: the prefab Entity array
- m_PrefabData: a ComponentLookup
obtained via GetComponentLookup () -
m_ReferencedPrefabs: the UnsafeList
marking referenced prefabs The job is scheduled with a batch size of 64 and JobHandle.CombineDependencies(m_DataDeps, m_UserDeps, base.Dependency). The resulting combined handle is stored into both m_DataDeps and m_UserDeps as well as base.Dependency, so the system's dependency tracks the scheduled work. -
public void BeginPrefabCheck(NativeArray<Entity> array, bool isLoading, JobHandle dependencies)
Initializes internal state for a prefab check run: - Stores the provided prefab entity array in m_PrefabArray.
- Allocates m_ReferencedPrefabs as a new UnsafeList
with Allocator.TempJob and resizes it to array.Length, cleared to false. - Stores the incoming dependencies into m_DataDeps.
-
Records isLoading into m_IsLoading. Call this before scheduling or otherwise using the system to indicate the set of prefabs to check.
-
public void EndPrefabCheck(out JobHandle dependencies)
Finalizes a prefab check run: - Combines m_DataDeps and m_UserDeps into the out parameter dependencies.
- Schedules disposal of m_ReferencedPrefabs using that combined dependencies (m_ReferencedPrefabs.Dispose(dependencies)).
-
Resets m_PrefabArray, m_ReferencedPrefabs, m_DataDeps, and m_UserDeps to their default values. This ensures the temporary UnsafeList is disposed only after all jobs that read it have completed.
-
public PrefabReferences GetPrefabReferences(SystemBase system, out JobHandle dependencies)
Returns a PrefabReferences helper constructed from the current internal state: - Provides m_PrefabArray and m_ReferencedPrefabs.
- Gets a read-only ComponentLookup
from the provided system (system.GetComponentLookup (isReadOnly: true)). -
Passes through m_IsLoading. Also sets the out parameter dependencies to m_DataDeps so callers can chain or synchronize on the same data dependencies.
-
public void AddPrefabReferencesUser(JobHandle dependencies)
Combines an external user JobHandle into the internal m_UserDeps. Use this to register additional job dependencies that must be considered before disposing the temporary list or before scheduling the check job. -
Nested job:
private struct CheckPrefabReferencesJob : IJobParallelFor
([BurstCompile])
Job fields: - [ReadOnly] NativeArray
m_PrefabArray - [NativeDisableParallelForRestriction] ComponentLookup
m_PrefabData - UnsafeList
m_ReferencedPrefabs Job Execute(int index): - If m_ReferencedPrefabs[index] is true, calls m_PrefabData.SetComponentEnabled(m_PrefabArray[index], value: true) to enable the PrefabData component on that prefab entity, and writes false back to m_ReferencedPrefabs[index] (clearing the mark). Notes:
- ComponentLookup
is marked without parallel-for restriction; SetComponentEnabled is used to enable components from multiple threads. - The job runs in parallel over the prefab array; the batch size used when scheduling is 64.
Notes and safety considerations:
- The system uses UnsafeList
Usage Example
// Example pattern for a serialization pipeline:
// Begin a prefab check pass (called from serialization code before scheduling dependent jobs)
var prefabArray = /* NativeArray<Entity> populated with prefab entities */;
JobHandle incomingDeps = /* any job handle that must complete first */;
checkPrefabReferencesSystem.BeginPrefabCheck(prefabArray, isLoading: true, incomingDeps);
// Allow other systems/jobs to add producer/user dependencies that also read/write the reference marks:
checkPrefabReferencesSystem.AddPrefabReferencesUser(otherJobHandle);
// Optionally obtain a PrefabReferences helper that callers can use to mark prefabs as referenced:
JobHandle depForUsers;
var prefabRefs = checkPrefabReferencesSystem.GetPrefabReferences(this /* SystemBase */, out depForUsers);
// pass prefabRefs into jobs that will mark entries in the UnsafeList
// The system's OnUpdate will schedule the CheckPrefabReferencesJob; when done, finalize:
checkPrefabReferencesSystem.EndPrefabCheck(out JobHandle finalDeps);
// Ensure finalDeps completes before freeing prefabArray if needed
finalDeps.Complete();
prefabArray.Dispose();
Notes:
- The system's OnUpdate schedules a Burst-compiled IJobParallelFor that enables PrefabData on referenced prefabs and clears the reference flags.
- Ensure you respect the dependency handles returned by GetPrefabReferences and EndPrefabCheck; UnsafeList