doyasu24 / SerializableFieldAssertions

SerializableFieldAssertions provides simple assertion methods for serializable fields with readable error messages in Unity.

Repository from Github https://github.comdoyasu24/SerializableFieldAssertionsRepository from Github https://github.comdoyasu24/SerializableFieldAssertions

Serializable Field Assertions

Provides simple assertion methods for serializable fields with readable error messages in Unity.

using System;
using System.Collections.Generic;
using UnityEngine;
using SerializableFieldAssertions;

public class SampleComponent : MonoBehaviour
{
    // UnityEngine.Object marked as [SerializeField] is serialized, and it can be null.
    [SerializeField] private GameObject myGameObject = null!;
    // public field UnityEngine.Object is serialized, and it can be null.
    public Transform MyTransform = null!;
    // T[] where T : UnityEngine.Object can contain null elements, but the array itself cannot be null.
    [SerializeField] private Transform[] myTransformArray = null!;

    private void Awake()
    {
        // Asserts that all serializable fields are not null with readable message.
        // 
        // --- output example ---
        // AssertionException: SampleComponent.myGameObject is null.
        // Hierarchy: MyGameObject
        // Scene: TestScene
        // 
        // --- output example (Array or List) ---
        // AssertionException: SampleComponent.myTransformArray contains null elements.
        // Index: 0
        // Hierarchy: MyGameObject
        // Scene: TestScene
        SerializableFieldAssert.AreNotNullAll(this);

        // Asserts that specified serializable field is not null with readable message.
        SerializableFieldAssert.IsNotNull(this, nameof(myGameObject));
    }
}

error-message

Features

By calling SerializableFieldAssert.AreNotNullAll(this); in the Awake method of a Component, you can assert that all serializable fields, such as those marked with [SerializeField], are not null.

private void Awake()
{
    // Asserts that all serializable fields are not null.
    SerializableFieldAssert.AreNotNullAll(this);
}

The assertion is marked with [Conditional("UNITY_ASSERTIONS")], the same as UnityEngine.Assertions.Assert.IsNotNull, so assertions are disabled in release build.

Nullable fields

If you want to allow null values for a specific field, you can use the nullable type like UnityEngine.Object?.

SerializableFieldAssert.AreNotNullAll does not validate fields that are nullable types.

// This field is validated for null by `SerializableFieldAssert.AreNotNullAll`.
[SerializeField] private GameObject myGameObject = null!;
// This field is not validated for null by `SerializableFieldAssert.AreNotNullAll`.
[SerializeField] private GameObject? nullableGameObject = null;

For more details about serializable fields, refer to the official documentation:

Unity - Manual: Script serialization

Install

You can add https://github.com/doyasu24/SerializableFieldAssertions.git?path=Assets/Plugins/SerializableFieldAssertions#0.1.0 to Package Manager.

License

MIT License

About

SerializableFieldAssertions provides simple assertion methods for serializable fields with readable error messages in Unity.

License:MIT License


Languages

Language:C# 100.0%