Skip to content

Provide popup to specify the type of the field serialized by the [SerializeReference] attribute in the inspector.

License

Notifications You must be signed in to change notification settings

Depra-Inc/SerializeReference.Extensions

Repository files navigation

SerializeReference.Extensions

Table of Contents

Introduction

The SerializeReference attribute, added in Unity 2019.3, makes it possible to serialize references to interfaces and abstract classes.

The SerializeReferenceDropdown attribute allows you to easily set subclasses of those abstract classes in the Editor that are serialized by SerializeReference attribute.

Inspired by this repo.

🦾 Features

  • Easily set subclass by popup.
  • Type finding through fuzzy searching.
  • The SerializeReferenceDropdown attribute supports types that meet the following conditions:
    • ✅ Public
    • ✅ Not abstract
    • ✅ Not generic
    • ✅ Not a Unity object
    • ✅ Serializable attribute is applied.
  • Override the type name, path and order by the SerializeReferenceMenuPath attribute.

📥 Installation

Download any version from releases.

Install via git URL

Alternatively, you can add this package by opening the PackageManager and entering

https://github.com/Depra-Inc/SerializeReference.Extensions.git

from the Add package from git URL option.

🔰 Usage

  1. Implement an interface or an abstract class:
internal interface ISampleCommand
{
    void Execute();
}
  1. Define a serialized reference or an array of them:
[SerializeReferenceDropdown] [SerializeReference] private ISampleCommand _command;
[SerializeReferenceDropdown] [SerializeReference] private ISampleCommand[] _commands;
  1. Implement public class or struct od record. They can be nested.
[Serializable]
public sealed class ClassCommand : ISampleCommand
{
    void ISampleCommand.Execute() { }
}

[Serializable]
public readonly struct StructCommand : ISampleCommand
{
    void ISampleCommand.Execute() { }
}

[Serializable]
public sealed record RecordCommand : ISampleCommand
{
    void ISampleCommand.Execute() { }
}
  1. Add SerializeReferenceMenuPath attribute to override the type name, path, and order:
[Serializable]
[SerializeReferenceMenuPath(nameof(CommandWithCustomTypeMenu))]
public sealed class CommandWithCustomTypeMenu : ISampleCommand
{
    void ISampleCommand.Execute() => { }
}

❓ FAQ

If the type is renamed, the reference is lost.

It is a limitation of SerializeReference of Unity.

When serializing a SerializeReference reference, the type name, namespace, and assembly name are used, so if any of these are changed, the reference cannot be resolved during deserialization.

To solve this problem, UnityEngine.Scripting.APIUpdating.MovedFromAttribute can be used.

Also, this thread will be helpful.

References

🤝 Contribute

I welcome feature requests and bug reports in issues and pull requests.

Support

I am an independent developer, and most of the development on this project is done in my spare time. If you're interested in collaboration or hiring me for a project, please check out my portfolio and reach out!

License

Apache-2.0

Copyright (c) 2023-2024 Nikolay Melnikov n.melnikov@depra.org

About

Provide popup to specify the type of the field serialized by the [SerializeReference] attribute in the inspector.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages