Malimbe
A collection of tools to simplify writing public API components in Unity.
Introduction
Malimbe is a collection of tools to simplify writing public API components in Unity.
By taking the assemblies that are created by build tools and changing the assembly itself, repetetive boilerplate can be reduced, new functionality can be introduced and logic written as part of the source code can be altered. This process is called Intermediate Language (IL) weaving and Malimbe uses Fody to do it.
Malimbe helps running Fody and Fody addins without MSBuild or Visual Studio and additionally offers running them inside Unity by integrating with Unity's compilation and build pipeline. Multiple weavers come with Malimbe to help with boilerplate one has to write when creating Unity components that are intended for public consumption. This includes a form of "serialized properties", getting rid of duplicated documentation through XML documentation and the [Tooltip]
attribute as well as weavers that help with ensuring the API is able to be called from UnityEvent
s and more.
Releases
Branch | Version | Explanation |
---|
latest | | Stable, production-ready |
next | | Experimental, not production-ready |
Releases follow the Semantic Versioning (SemVer) system.
Getting Started
Please follow these steps to install the package using a local location until Unity's Package Manager (UPM) allows third parties to publish packages to the UPM feed:
-
Download a release from the Releases page and extract it into your folder you use to keep your packages. It is recommended to make that folder part of your project and therefore version controlled.
-
Open your Unity (>= 2018.3
) project and follow Unity's instructions on how to add the package to your project using UPM.
-
Anywhere in your Unity project add a FodyWeavers.xml
file.
-
Configure the various weavers Malimbe offers, e.g.:
<?xml version="1.0" encoding="utf-8"?>
<Weavers>
<Malimbe.FodyRunner>
<LogLevel>Error, Warning</LogLevel>
</Malimbe.FodyRunner>
<Malimbe.BehaviourStateRequirementMethod/>
<Malimbe.MemberClearanceMethod/>
<Malimbe.PropertySerializationAttribute/>
<Malimbe.PropertySetterMethod/>
<Malimbe.PropertyValidationMethod/>
<Malimbe.XmlDocumentationAttribute IdentifierReplacementFormat="`{0}`"/>
</Weavers>
As with any Fody weaver configuration the order of weavers is important in case a weaver should be applying to the previous weaver's changes.
In case there are multiple configuration files all of them will be used. In that scenario, if multiple configuration files specify settings for the same weaver, a weaver will be configured using the values in the last configuration file found. A warning is logged to notify of this behavior and to allow fixing potential issues that may arise by ensuring only a single configuration exists for any used weaver.
Additional weavers are supported. To allow Malimbe's Unity integration to find the weavers' assemblies they have to be included anywhere in the Unity project or in one of the UPM packages the project uses.
What's In The Box
Malimbe is a collection of tools. Each project represents a solution to a specific issue.
FodyRunner
A standalone library that allows running Fody without MSBuild or Visual Studio.
- Use the XML element
LogLevel
to specify which log messages should be sent to the logger instance. Separate multiple levels by using multiple XML elements or separate inside an XML element by using any form of whitespace including newlines or commas. Valid values are
None
(or don't specify LogLevel
)Debug
Info
Warning
Error
All
FodyRunner.UnityIntegration
Weaves assemblies using FodyRunner
in the Unity Editor after Unity compiled them.
- There is no need to manually run the weaving process. The library just needs to be part of a Unity project (it's configured to only run in the Editor) to be used. It hooks into the various callbacks Unity offers and automatically weaves any assembly on startup as well as when they change.
- Once the library is loaded in the Editor a menu item
Tools/Malimbe/Weave All Assemblies
allows to manually trigger the weaving process for all assemblies in the current project. This is useful when a FodyWeavers.xml
file was changed.
BehaviourStateRequirementMethod
A Unity-specific weaver. Changes a method to return early if a combination of the GameObject's active state and the Behaviour's enabled state doesn't match the configured state.
- Annotate a method with
[RequiresBehaviourState]
to use this. The method needs to be defined in a type that derives from UnityEngine.Behaviour
, e.g. a MonoBehaviour
. - Use the attribute constructor's parameters to configure the specific state you need the GameObject and the Behaviour to be in.
MemberClearanceMethod.Fody
A generic weaver. Creates ClearMemberName()
methods for any member MemberName
that is of reference type. Sets the member to null
in this method.
PropertySerializationAttribute.Fody
A Unity-specific weaver. Ensures the backing field for a property is serialized.
- Annotate a property with
[Serialized]
to use this. The property needs both a getter and setter. - If the property's backing field doesn't use
[SerializeField]
it will be added. - If the property is an auto-implemented property the backing field will be renamed to match the property's name for viewing in the Unity inspector. All backing field usages inside methods of the declaring type will be updated to use this new name. Since C# doesn't allow multiple members of a type to share a name, the backing field's name will differ in the first character's case. E.g.:
public int Counter { get; set; }
will use a backing field called counter
.protected bool isValid { get; private set; }
will use a backing field called IsValid
.
PropertySetterMethod.Fody
A generic weaver. Calls a validation method at the start of a property's setter.
- Annotate a method with
[SetsProperty(nameof(SomeProperty))]
to use this. The method needs to follow the signature pattern T MethodName(T, T)
where T
is the property's type. The accessibility level of the method doesn't matter and the name lookup is case insensitive. A call to this method will be added to the start of the property's setter. - The property needs to be declared in the same type the method is declared in. Both a getter and setter are required for the property.
PropertyValidationMethod.Fody
A generic weaver (though made for Unity). Creates a OnValidate()
method that validates a property.
XmlDocumentationAttribute.Fody
A generic weaver (though made for Unity). Looks up the XML <summary>
documentation for a field and adds [Tooltip]
to that field with that summary.
UnityPackaging
Outputs a ready-to-use folder with the appropriate hierarchy to copy into a Unity project's Assets folder. The output includes both the Unity integration libraries as well as all weavers and their attributes listed above.
Contributing
If you want to raise a bug report or feature request please follow SUPPORT.md.
While we intend to add more features to Malimbe when we identify a need or use case, we're always open to take contributions! Please follow the contribution guidelines found in CONTRIBUTING.md.
Naming
Inspired by Fody's naming the name "Malimbe" comes from the small birds that belong to the weaver family Ploceidae.
Tools And Products Used
License
Malimbe is released under the MIT License.
Third-party notices can be found in THIRD_PARTY_NOTICES.md