Game.SceneFlow.AssetLibrary
Assembly:
Namespace: Game.SceneFlow
Type: class
Base: ScriptableObject
Summary:
AssetLibrary is a ScriptableObject that groups multiple AssetCollection instances and is responsible for adding the referenced prefabs to a PrefabSystem. It keeps simple progress counters and logs the total time taken to add the explicitly referenced prefabs. Loading supports cancellation via a CancellationToken and updates the PrefabSystem availability cache before adding prefabs.
Fields
-
public List<AssetCollection> m_Collections
A list of AssetCollection objects contained in this library. Each AssetCollection represents a set of prefabs that will be added to the provided PrefabSystem when Load is invoked. This list is typically populated in the editor (CreateAssetMenu) or by other initialization code. -
private int m_AssetCount
Internal counter storing the total number of assets across all collections (used for progress calculation). It is computed by GetCount() when Load starts. -
private int m_ProgressCount
Internal counter incremented as collections are processed during Load. Used to compute the public progress property.
Properties
public float progress { get; }
Returns a float in [0,1] representing the loading progress calculated as m_ProgressCount / (m_AssetCount - 1). If m_AssetCount is 0 or negative, progress returns 0. Note: the implementation subtracts 1 from m_AssetCount before dividing, which causes a slight off-by-one behavior for the reported progress when m_AssetCount is 1; this mirrors the original code.
Constructors
public AssetLibrary()
Default parameterless constructor (ScriptableObject-derived classes are typically instantiated by Unity via CreateAssetMenu or ScriptableObject.CreateInstance).
Methods
public void Load(PrefabSystem prefabSystem, CancellationToken token)
Loads all prefabs referenced by the contained AssetCollection instances into the given PrefabSystem.
Behavior details: - Obtains a logger via LogManager.GetLogger("SceneFlow") and logs the total number of explicitly referenced prefabs and elapsed time when complete. - Calls prefabSystem.UpdateAvailabilityCache() before adding prefabs. - Resets m_ProgressCount to 0 and computes m_AssetCount using GetCount(). - Iterates over each AssetCollection in m_Collections: - Throws OperationCanceledException if token is canceled (via token.ThrowIfCancellationRequested()). - Increments m_ProgressCount by collection.Count. - Calls collection.AddPrefabsTo(prefabSystem) to add the prefabs. - Measures the elapsed time of the operation via PerformanceCounter.Start and logs the summary when the using scope ends.
Exceptions: - OperationCanceledException if the provided CancellationToken is canceled during the load loop.
Notes: - The method assumes prefabSystem and collection implementations are safe to call from the context this method is invoked in (usually main thread). - m_Collections may be null if not initialized; callers or editors should ensure it is populated.
private int GetCount()
Returns the total number of assets across all AssetCollection instances in m_Collections by summing each collection.Count. If m_Collections is empty, returns 0.
Usage Example
// CreateAssetMenu attribute present on the class lets you create an AssetLibrary in Unity editor.
// Example of invoking Load from game code:
var assetLibrary = /* reference to an AssetLibrary ScriptableObject */;
var prefabSystem = /* obtain PrefabSystem instance */;
var cts = new CancellationTokenSource();
try
{
assetLibrary.Load(prefabSystem, cts.Token);
Debug.Log($"Load progress: {assetLibrary.progress}");
}
catch (OperationCanceledException)
{
Debug.Log("AssetLibrary.Load was cancelled.");
}