Game.AssetPipeline.IPrefabFactory
Assembly: Game
Namespace: Game.AssetPipeline
Type: Interface
Base: (none)
Summary:
Defines a factory contract for creating prefab instances (subclasses of Game.Prefabs.PrefabBase) from an asset/source path. Used by the asset pipeline to instantiate typed prefab objects (e.g., buildings, props, vehicles) at a specific LOD level. Implementations bridge the project's prefab-loading APIs and higher-level code that consumes typed PrefabBase instances.
Fields
- This interface declares no fields.
Interfaces represent a contract only; state and fields are provided by implementations.
Properties
- This interface declares no properties.
Implementations may expose properties as needed but they are not required by the contract.
Constructors
- Interfaces do not define constructors.
Concrete factories must provide constructors as needed.
Methods
T CreatePrefab<T>(string sourcePath, string name, int lodLevel) where T : PrefabBase
Creates and returns a prefab instance of the requested generic type T, using asset data found at sourcePath, assigning it the provided name and LOD (level-of-detail) index. T must derive from Game.Prefabs.PrefabBase.
Parameters: - sourcePath — Path or identifier for the source asset to load (file path, package id, virtual path, etc.). Interpretation depends on the implementing factory. - name — Logical name to assign to the created prefab (used for registration, display, or debugging). - lodLevel — Level-of-detail index to build/initialize. Implementations decide how LOD is applied; 0 typically indicates full-detail.
Returns: - An instance of T that represents the loaded/created prefab. Null should generally not be returned unless documented; prefer throwing exceptions on unrecoverable errors.
Notes for implementors: - The interface does not mandate how assets are loaded (synchronous/async), whether the prefab is registered with global managers, or whether caching is performed. These behaviors should be documented by each implementation. - Because T is constrained to PrefabBase, implementations will commonly rely on the game's prefab lifecycle methods or constructors available on concrete PrefabBase subclasses.
Usage Example
// Example concrete factory (illustrative). Adapt to the game's prefab-loading API.
public class SimplePrefabFactory : IPrefabFactory
{
public T CreatePrefab<T>(string sourcePath, string name, int lodLevel) where T : PrefabBase
{
// Pseudocode — replace with actual game API calls:
// 1) Load raw asset data from sourcePath
// 2) Create an instance of the concrete prefab type
// 3) Initialize it with the loaded data, name and LOD
object rawData = MyAssetLoader.Load(sourcePath); // pseudo-API
// If concrete prefab types have parameterless constructors:
T prefab = Activator.CreateInstance<T>();
// Initialize the prefab using the game's API (pseudo-methods)
prefab.InitializeFromAsset(rawData, name, lodLevel);
// Optionally register prefab with managers, caches, etc.
// PrefabManager.Register(prefab);
return prefab;
}
}
// Example usage:
IPrefabFactory factory = new SimplePrefabFactory();
var building = factory.CreatePrefab<MyBuildingPrefab>("Assets/Buildings/house.asset", "SmallHouse", 0);
Notes: - Replace pseudo-APIs (MyAssetLoader, InitializeFromAsset, PrefabManager) with the actual asset/prefab loading and registration APIs available in Cities: Skylines 2 modding environment. - Consider thread-safety and synchronous vs asynchronous loading requirements when implementing the factory.