I have a base item class:
public abstract class ItemBase
{
#region Private Fields
private int id;
private ItemType itemType;
private Transform transform;
private GameObject item;
#endregion
#region Public Fields
public GameObject prefab;
#endregion
#region Public Properties
public GameObject Item
{
get
{
if (this.item == null)
InstantiateItem();
return this.item;
}
}
public Transform Transform
{
get
{
return this.transform;
}
set
{
this.transform = value;
}
}
public ItemType ItemType
{
get
{
return this.itemType;
}
set
{
this.itemType = value;
}
}
#endregion
#region Constructor
public void Initialize(int id, ItemType itemType, Transform transform = null)
{
this.id = id;
this.itemType = itemType;
this.transform = transform;
}
#endregion
#region Private Methods
private void InstantiateItem()
{
if (prefab == null)
{
throw new Exception("Item prefab not defined.");
}
if (this.transform != null)
{
this.item = GameObject.Instantiate(this.prefab, this.transform);
}
else
{
this.item = GameObject.Instantiate(this.prefab);
}
}
#endregion
}
This structure allows me to create new instances of my items without having to instantiate GO’s.
Usually you would add an “Item” component to a GO (above example does the opposite).
I have managers which have references to these items which create, remove, etc.
I plan on using a database to read the item information and create references to them (only instantiating the item GO’s when needed).
My goal is to only instantiate GO’s when they physically need to be in the game.
Is this architecture/structure/implementation common, or am I going to be running into a lot of problems in the near future?