Unity Game Development

Tool Spotlight - Flume

by Rebecca McCready & Steve Halliwell · June 10, 2022

Actuator’s Unity dependency injection framework package. Flume focuses on service injection and provides support for working with regular C# classes, MonoBehaviours, and ScriptableObjects.

What is Flume?

Flume is a service installer. It stays small in its responsibilities, while having a big impact on your development pipeline. You Register services and use Dependants with Inject methods. Flume makes sure the correct instance is passed to the Inject method when the object is created.

Here’s a code sample:

    // Service Interface
    public class IScoreSystem {
        void AddScore(int points);
        int Score { get; }
    }

    // Service Concretion
    public class ScoreSystem : IScoreSystem {
        public int Score { get; private set; }

        public void AddScore(int points) {
            Score += points;
        }

    }

    // Service Registration with Flume
    public class DemoServiceInstaller : ServiceInstaller {
        protected override void InstallServices(FlumeServiceContainer container) {
            container.Register<IScoreSystem, ScoreSystem>();
        }
    }

    // DependentBehaviour is Flume’s Dependent aware MonoBehaviour
    public class ScoreDisplay : DependentBehaviour    {
        [SerialiseField] private Text _scoreText;
        private IScoreSystem _scoreSystem;

        // This will be called by Flume for us with the registered concretion
        public void Inject(IScoreSystem scoreSystem) {
            _scoreSystem = scoreSystem;
        }

        private void Update() {
            _scoreText.text = $"Score: {_scoreSystem.Score}";
        }
    }

Benefits of Flume:

Flume is Unity first, meaning it’s aware of Unity’s object life cycles and patterns. This allows you to bridge to and from Unity objects and regular C# instances. A monobehaviour can be a Service that is injected into a POCO Dependent, for example.

Flume simplifies wiring things up, making them more code driven and less drag and drop labour. But above all else, it highlights the D from SOLID, making it far easier to develop tests around services and classes. Allowing dependencies to be mocked, and automated tests developed, where otherwise you might have to fall back to a ‘test scene’ that is manually observed, or worse.

Why would you use it?

For us, if we are using Unity, we are using Flume. It’s simple, no frills approach to dealing with dependencies combined with our love of Test Driven Development mean it’s one of the first things added to a project.

Something neat!

Since all dependent objects are injected via interface, it’s easy to write tests around your dependent objects, even those that derive from Unity’s MonoBehaviour. Create mocks with something like NSubstitute and provide those to the Inject method directly in the arrange portion of your test.