-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Factory
Without some sort of abstraction, generic systems required to handle a variety of types must implement each creation implementation as well. For example, you must "new" non-abstract types such as Customer, "Instantiate" MonoBehaviours as a component on a GameObject, and "CreateInstance" ScriptableObjects. The abstract factory pattern allows for the creation of various types without the knowledge of their specific creation implementation.
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