-
Notifications
You must be signed in to change notification settings - Fork 2k
Factory
Without some sort of abstraction, generic systems required to handle a variety of types must implement the creation of each type independently, leading to a lot of boilerplate code. This can be the case when you have to new
many non-abstract types (such as Customer, User, etc.), Instantiate
MonoBehaviours as components on a GameObject, or CreateInstance
of ScriptableObjects. The abstract Factory pattern allows for the creation of various types without the knowledge of their specific creation implementation.
In this game for instance, we use it a lot in our Object Pooling solution.
- New-able type with an empty constructor
- New-able type with constructor arguments
- Components
- Components on prefabs
- ScriptableObjects
- More info
using UnityEngine;
using UOP1.Factory;
[CreateAssetMenu(fileName = "NewCustomerFactory",menuName="Factory/Customer")]
public class CustomerFactorySO : FactorySO<Customer>
{
public override Customer Create(){
return new Customer();
}
}
Note: The CreateAssetMenu attribute may be omitted if the factory is only created via CreateInstance at runtime.
using UnityEngine;
using UOP1.Factory;
[CreateAssetMenu(fileName = "NewCustomerFactory",menuName="Factory/Customer")]
public class CustomerFactorySO : FactorySO<Customer>
{
[SerializeField]
private int _name;
public int Name{
set{
_name = value;
}
}
public override Customer Create(){
return new Customer(_name);
}
}
Note: The public property setter may be omitted if the factory is only created as an asset in the project.
using UnityEngine;
using UOP1.Factory;
[CreateAssetMenu(fileName = "NewAudioSourceFactory",menuName="Factory/AudioSource")]
public class AudioSourceFactorySO : FactorySO<AudioSource>
{
public override AudioSource Create(){
return new GameObject("AudioSource").AddComponent<AudioSource>();
}
}
using UnityEngine;
using UOP1.Factory;
[CreateAssetMenu(fileName = "NewAudioSourceFactory",menuName="Factory/AudioSource")]
public class AudioSourceFactorySO : FactorySO<AudioSource>
{
[SerializeField]
private AudioSource _prefab;
public int Prefab{
set{
_prefab = value;
}
}
public override AudioSource Create(){
return Instantiate(_prefab);
}
}
using UnityEngine;
using UOP1.Factory;
[CreateAssetMenu(fileName = "NewConfigFactory",menuName="Factory/Config")]
public class ConfigFactorySO : FactorySO<ConfigSO>
{
public override ConfigSO Create(){
return ScriptableObject.CreateInstance<ConfigSO>();
}
}
Unity Open Projects - Open-source games made by Unity + the community
We are looking forward to see what you will create ❤
- the Unity Creator Advocacy team
- Game architecture overview
- Playing the game in Unity
- Creating a new playable scene
- Building the game
The game systems explained, with API examples. For programmers.
- Event system
- State machine
- Input
- Object pooling
- Runtime anchors
- Audio system
- Narrative
- Dialogue system
- Cutscenes system
- Inventory and Cooking
- Combat
- User Interface
How-tos for designers to expand the game's gameplay.
- Adding quests
- Adding items
- Creating dialogues
- Making a cutscene