A small game can assign prefab references through the Unity editor and retrieve instantiated copies through a singleton asset manager. This keeps references private while making asset access explicit through dedicated methods. section 7.2.1
The manager supports straightforward loading during initialization:
- Load menu UI once and keep it in memory; load the player once; initialize an enemy pool for the Board to manage. section 7.2.1
- Use a separate getter for each prefab or UI asset, making it clear which asset a call retrieves. section 7.2.1
Listing 7.18 The AssetManager class
public class AssetManager : Singleton<AssetManager>
{
[SerializeField] GameObject m_playerPrefab; #A
[SerializeField] GameObject m_enemyPrefab; #A
[SerializeField] GameObject m_UIInGame; #B
[SerializeField] GameObject m_UIMainMenu; #B
[SerializeField] GameObject m_UIGameOver; #B
public GameObject GetPlayerGameObject(Transform playerParent) #C
{
return Instantiate(m_playerPrefab, playerParent);
}
public GameObject GetEnemyGameObject(Transform enemyParent) #C
{
return Instantiate(m_enemyPrefab, enemyParent);
}
public GameObject GetInGameUI() #C
{
return Instantiate(m_UIInGame);
}
public GameObject GetMainMenuUI() #C
{
return Instantiate(m_UIMainMenu);
}
public GameObject GetGameOverUI() #C
{
return Instantiate(m_UIGameOver);
}
}The AssetManager listing shows private serialized prefab references and dedicated methods that instantiate their assets. section 7.2.1
