Skip to content

Make your Unity reference fields required, so they always must be set.

License

MIT, Unknown licenses found

Licenses found

MIT
LICENSE
Unknown
LICENSE.meta
Notifications You must be signed in to change notification settings

Incantium/Required

Repository files navigation

Required

Unity version .NET version C# version

Overview

Required example

One of the most common errors in any Unity project are the NullReferenceException, haunting developers nightmares. This package makes it easier to spot when a serialized fields in the Unity Editor is left empty, even though they should be set. With this new [Required] attribute, you (as a software developer) can be sure required fields in the Unity Editor are communicate with your team members, no matter what.

Installation instructions

  • Open the Package Manager in a Unity project.
  • Click on the "+" button to add a new package.
  • Click on "Install package from git URL...".
  • Put in https://github.com/Incantium/Required.git.
  • Click on "Install" or press enter.
  • Enjoy!

Limitations

  • It is impossible to reference classes that cannot be referenced through the Unity Editor (that do not inherit from Object or IRequireable).

Workflow

To make any referenceable field required in Unity, you only have to do the following.

using Incantium.Attributes;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    [SerializeField]
    [Tooltip("The target position of the enemy. Not setting this field will give a warning in the Unity Editor.")]
    [Required]
    private Transform target;
}

References

Class Description
IRequireable Interface for other classes able to be required.
Required The Required attribute to show a warning in the Unity Editor when found to be empty.
RequireStatus The different statuses a required field can be in.

Frequently Asked Questions

Which Unity versions are compatible with this package?

This package is heavily tested in Unity 2022.3.44f1 and Unity 6000.0.25f1. It is expected that this package also works in older and newer versions of the Unity Editor because it is not dependent on any other Unity package.

Why does a warning appear when I use Required?

Non-referenceable

This warning shows up when the field to be required is not referenceable through the Unity Editor (like all primitive data structures such as integers, floats, and booleans). Only the classes inherited from Object are referenceable through the Unity Editor. The exception is the IRequireable interface, which also makes it possible to be required.

It is possible for custom classes to be requireable?

Yes. It is possible for classes that don't inherit from Object to be requireable, such as serializable classes. These classes need to implement the IRequireable interface to function properly with the Required attribute.

About

Make your Unity reference fields required, so they always must be set.

Topics

Resources

License

MIT, Unknown licenses found

Licenses found

MIT
LICENSE
Unknown
LICENSE.meta

Stars

Watchers

Forks

Contributors 2

  •  
  •  

Languages