🐝How to Use Mosquito.NET

This page describes how you can use Mosquito.NET.

To use Mosquito.NET in your Unity project, first import the Mosquito.NET asset and check out the example scenes included.

Initialization

To initialize Mosquito.NET, add the MosquitoInitializer script to a gameobject in your scene. This component has several settings that you can adjust, such as enabling logging for the initialization process.

You can also speed up the start time by enabling multithreaded initialization in the inspector. This will initialize Mosquito.NET in a separate thread, so it doesn't block the main thread.

If you use multithreading, it's a good idea to subscribe to the OnInitialized event in the MosquitoInitializer class (exposed as a UnityEvent in the inspector). This will ensure that all of your checks run after Mosquito.NET has completed its initialization. Otherwise, some checks may be missed if they run before Mosquito.NET has finished setting up.

Methods

Mosquito.NET includes built-in null-check attributes, but you can also create your own validation attributes if you need to.

Inputs

You can validate method inputs one at a time or all at once. Here are some examples:

// This function should never receive any null arguments.
[ParamsNotNull(throwException: false)]
public void ParameterExample(string item, int id)
{
    return;
}

// The item parameter should never be null.
public void SpecificParameterExample([ParamNotNull] string item)
{
    return;
}

Note that if the parameter type is a struct (which cannot be null), it will be ignored.

Outputs

Method return values can also be validated using the appropriate method attribute. Here are some examples:

// This function should never return null!
[ReturnNotNull(throwException: true)]
public string ReturnExample()
{
    return null;
}

Note that the return type should be nullable in order for Mosquito.NET to validate it.

Properties

In addition to methods, Mosquito.NET can also validate property setters and getters to ensure that they are not set or returned as null. The library includes built-in null-check attributes, but you can also create your own validation attributes if you need to. Custom method attributes must inherit from ValidationAttribute in order to be detected during initialization.

Setters

You can use the SetNotNull attribute to validate the set method of a property, as shown in the example below:

// This property should never be set to null
[SetNotNull(throwException: false)]
public string Id { get; set; }

Note that the property type must be nullable for Mosquito.NET to validate it.

Getters

In addition to setters, you can also use Mosquito.NET to validate the get method of a property. This can help you catch and prevent errors caused by trying to access a null property. Here's an example:

Examples:

// This property should never return null.
[GetNotNull(throwException: false)]
public string Id { get; set; }

As with property setters, the property type must be nullable for Mosquito.NET to validate it.

Last updated