Modula supports easy integration for any type that extends MonoBehaviour.

Creating custom Module type

To create a new module type, simply add new class and paste the following:

using System;
using Modula;
using Modula.Optimization;
using UnityEngine;


public abstract class YourModule : CustomBehaviour, IModule
{
    public TimingConstraints UpdateInvocationConstraints => DefaultImplementation.UpdateConstraints;
    public virtual TypeList RequiredOtherModules { get; } = TypeList.None;

    private ModuleDefaultImplementation _defaultImplementation;
    // ReSharper disable once MemberCanBePrivate.Global
    protected ModuleDefaultImplementation DefaultImplementation
    {
        get { return _defaultImplementation ??= new ModuleDefaultImplementation(this); }
    }

    public ModularBehaviour Main => DefaultImplementation.Main;

    public void OnAdd()
    {
        DefaultImplementation.OnAdd();
    }

    public void AddModule(Type moduleType)
    {
        DefaultImplementation.AddModule(moduleType);
    }

    public string GetName()
    {
        return DefaultImplementation.GetName();
    }

    public DataLayer GetData()
    {
        return DefaultImplementation.GetData();
    }

    public virtual void ModuleUpdate()
    {
    }

    public virtual void Update()
    {
        DefaultImplementation.Update();
    }
}
  • Replace "YourModule" with your preferred name
  • Replace "CustomBehaviour" with any other class that extends MonoBehaviour

Now you can use YourModule class in the same way you use Module, but it also supports all the features implemented in CustomBehaviour!

Built-In integrations

These are integrations with 3rd party packages that are supported out-of-the-box:

Mirror

Mirror is an Open Source Networking library for Unity.

If you want to support all of the Mirror's NetworkBehaviour features in your modules, we got you!

Simply derive from NetworkModule instead of Module for the seampless Mirror - Modula integration:

public class Shooting : NetworkModule
{
    [SyncVar] public bool isShooting;

    public override TypeList RequiredOtherModules { get; } = new TypeList()
        .Add(typeof(Turret));
        
    private RigidBody _rb;

    public override void Awake()
    {
        base.Awake();
        _rb = GetComponent<Rigidbody>();
    }

    private void Update()
    {
        if (isLocalPlayer)
        {
            var shootingButtonPressed = Input.GetMouseButton(0);
            if (!isShooting && shootingButtonPressed)
                CmdSetShooting(true);
            else if (isShooting && !shootingButtonPressed) CmdSetShooting(false);
        }

        if (isServer) HandleShooting();
    }

    [Command]
    public void CmdSetShooting(bool shooting)
    {
        isShooting = shooting;
    }

    [Server]
    public void HandleShooting()
    {
        if (!isShooting) return;
        SpawnBullet( ... );
    }
}

All the attributes etc will work as if you were just using NetworkBehaviour.

All the Modula’s features are still supported tho! Such as ModuleUpdate()

#Important Notices While migrating to NetworkModule from NetworkBehaviour, don’t forget to override Awake() and put base.Awake() to use optimization features.