Skip to content

Factory

Ciro Continisio edited this page Dec 3, 2020 · 7 revisions

Factory Diagram

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.

Table of Contents

New-able type with an empty constructor

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.

New-able type with constructor arguments

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.

Components

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>();
    }
}

Components on prefabs

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);
    }
}

ScriptableObjects

using UnityEngine;
using UOP1.Factory;

[CreateAssetMenu(fileName = "NewConfigFactory",menuName="Factory/Config")]
public class ConfigFactorySO : FactorySO<ConfigSO>
{
    public override ConfigSO Create(){
        return ScriptableObject.CreateInstance<ConfigSO>();
    }
}

More info

Home
Video materials

Basics

World building and Graphics

Game architecture

The game systems explained, with API examples. For programmers.

Game design

How-tos for designers to expand the game's gameplay.

  • Adding quests
  • Adding items
  • Creating dialogues
  • Making a cutscene
Clone this wiki locally