ItemFactory
Abstract ScriptableObject factory that spawns Item instances with networked prefab instantiation, geometry attachment, and outline baking. Subclass to create concrete item factories for specific item types.
Definition
Namespace: Paragon.Townskeep.ItemSystem Assembly: Townskeep.dll
public abstract class ItemFactory<TItem, TData> : NetworkScriptableFactory<TItem, TData>, IItemFactory<TItem, TData>
where TItem : Item, IFactorable<TData>
where TData : ItemDataInheritance: ScriptableObject → ScriptableFactory<TItem, TData> → NetworkScriptableFactory<TItem, TData> → ItemFactory<TItem, TData> Implements: IItemFactory<TItem, TData>, INetworkScriptableFactory<TItem, TData>
Remarks
ItemFactory adds two item-specific behaviors on top of the network factory pipeline:
Geometry setup — A serialized
geometryGameObject is baked for smooth normals at initialization and instantiated as a child of each spawned item with the name"Geometry".Outline baking — After geometry is attached, the item's
Outlinecomponent is baked for rendering.
The factory's GetHashCode() returns ItemData.ItemID, which is used by ItemDatabase as the dictionary key for factory lookups.
Each ItemFactory ScriptableObject asset represents one item type. Variants are handled by the Scriptable Factory variant system — the base ItemData fields marked [Overridable] can be customized per variant.
Quick Lookup
Create a concrete factory
Subclass ItemFactory<MyItem, MyData>, create as ScriptableObject
Spawn locally
factory.Spawn() (inherited)
Spawn networked
factory.NetworkSpawn() (inherited)
Spawn variant
factory.NetworkSpawn(variantId) (inherited)
Get item data
factory.GetData() (inherited)
Hash code = item ID
factory.GetHashCode() returns ItemData.ItemID
Fields
geometry (private, serialized)
The 3D model prefab instantiated as a child of each spawned item.
Smooth normals are baked on this geometry during Initialize() via SmoothNormals.Bake(). This is done once per factory, not per spawn.
Methods
Initialize (override)
Calls base.Initialize() (registers network prefab) then bakes smooth normals on the geometry.
InstantiateObject (override)
Creates an item instance via the base prefab handler, then instantiates the geometry as a child and bakes the outline.
Pipeline:
base.InstantiateObject()— instantiates the network prefabInstantiates
geometryas a child with name"Geometry"Calls
item.GetComponent<Outline>().Bake()for outline rendering
Returns: The fully configured TItem instance.
GetHashCode (override)
Returns ItemData.ItemID, used by ItemDatabase for indexing.
Returns: The ItemID field from the factory's ItemData.
Extension Points
Required Overrides
None — ItemFactory is abstract via its generic parameters, but all methods have implementations. Concrete subclasses only need to exist as ScriptableObject assets.
Optional Overrides
Initialize()
Additional factory-level initialization (call base.Initialize() first)
InstantiateObject()
Additional per-spawn setup (call base.InstantiateObject() first)
Implementation Requirements
When subclassing ItemFactory, you MUST:
Define concrete
TItemandTDatatype parameters (e.g.,ItemFactory<Weapon, WeaponData>)Ensure
TItemextendsItemand implementsIFactorable<TData>Ensure
TDataextendsItemDataCreate the factory as a ScriptableObject asset in the project
You SHOULD:
Assign the
geometryfield in the Inspector to the item's 3D model prefabEnsure the item prefab has an
Outlinecomponent (required byInstantiateObject())Set
ItemData.ItemIDto a unique value for ItemDatabase indexing
Common Pitfalls
Missing geometry reference If geometry is null, Initialize() will throw a NullReferenceException during SmoothNormals.Bake() and InstantiateObject() will fail during Instantiate().
Missing Outline component InstantiateObject() calls item.GetComponent<Outline>().Bake(). If the item prefab lacks an Outline component, this will throw NullReferenceException.
Duplicate ItemID values If two factories share the same ItemData.ItemID, they will collide in the ItemDatabase dictionary. The second factory to register will overwrite the first. Ensure each factory has a unique ItemID.
Examples
Minimal Concrete Factory
Custom Factory with Extra Init
See Also
IItemFactory — interface this class implements
ItemDatabase — registry that indexes factories
ItemData — data record defining item properties
Item — runtime MonoBehaviour spawned by this factory
Last updated