• Stars
    star
    753
  • Rank 60,280 (Top 2 %)
  • Language
    C#
  • License
    MIT License
  • Created over 3 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

Unity SerializeReferenceExtensions

Build Release openupm

Inspired by this post.

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

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

SubclassSelector

Features

  • Easily set subclass by popup.
  • [New] Type finding by fuzzy finder.
  • [New] Override the type name and path by the AddTypeMenu attribute.

📥 Installation

Download any version from releases.

Releases: https://github.com/mackysoft/Unity-SerializeReferenceExtensions/releases

Install via git URL

Or, you can add this package by opening PackageManager and entering

https://github.com/mackysoft/Unity-SerializeReferenceExtensions.git?path=Assets/MackySoft/MackySoft.SerializeReferenceExtensions

from the Add package from git URL option.

Install via Open UPM

Or, you can install this package from the Open UPM registry.

More details here.

openupm add com.mackysoft.serializereference-extensions

🔰 Usage

using System;
using UnityEngine;

public class Example : MonoBehaviour {

	// The type that implements ICommand will be displayed in the popup.
	[SerializeReference, SubclassSelector]
	ICommand m_Command;

	// Collection support
	[SerializeReference, SubclassSelector]
	ICommand[] m_Commands = Array.Empty<ICommand>();

	void Start () {
		m_Command?.Execute();

		foreach (ICommand command in m_Commands) {
			command?.Execute();
		}
	}

	// Nested type support
	[Serializable]
	public class NestedCommand : ICommand {
		public void Execute () {
			Debug.Log("Execute NestedCommand");
		}
	}

}

public interface ICommand {
	void Execute ();
}

[Serializable]
public class DebugCommand : ICommand {

	[SerializeField]
	string m_Message;

	public void Execute () {
		Debug.Log(m_Message);
	}
}

[Serializable]
public class InstantiateCommand : ICommand {

	[SerializeField]
	GameObject m_Prefab;

	public void Execute () {
		UnityEngine.Object.Instantiate(m_Prefab,Vector3.zero,Quaternion.identity);
	}
}

// Menu override support
[AddTypeMenu("Example/Add Type Menu Command")]
[Serializable]
public class AddTypeMenuCommand : ICommand {
	public void Execute () {
		Debug.Log("Execute AddTypeMenuCommand");
	}
}

[Serializable]
public struct StructCommand : ICommand {
	public void Execute () {
		Debug.Log("Execute StructCommand");
	}
}

Supported Types

The SubclassSelector attribute supports types that meet the following conditions.

  • Public
  • Not abstract
  • Not generic
  • Not unity object
  • Serializable attribute is applied.

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

Help & Contribute

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

If you feel that my works are worthwhile, I would greatly appreciate it if you could sponsor me.

GitHub Sponsors: https://github.com/sponsors/mackysoft

More Repositories

1

Vision

UnityEngine.CullingGroup API for everyone.
C#
140
star
2

Navigathena

Scene management framework for Unity. Provides a new generation of scene management.
C#
111
star
3

Choice

Weighted random selector for Unity.
C#
92
star
4

XPool

Object pooling system for Unity.
C#
62
star
5

Modiferty

Property Modification for Unity
C#
23
star
6

Unity-GitHubActions-Tutorials

A tutorials on CI using Unity and GitHub Actions. All tutorials include a example project.
17
star
7

PoolManager

[LEGACY] Object pooling system for Unity.
C#
15
star
8

Unity-GitHubActions-ExportPackage-Example

A example project of exporting a package using Unity and GitHub Actions.
C#
11
star
9

VContainer-Examples

Example of using VContainer and its integration (UniRx, MessagePipe, etc...)
C#
9
star
10

UniData

[PREVIEW] Data Management for Unity. It also useful for implementing Achievements, Quests, etc.
C#
6
star
11

MasterTools

An import tool for importing tables from excel or spreadsheet to generate a database.
C#
5
star
12

Unity-GitHubActions-Build-Example

An example of building a project using Unity and GitHub Actions.
3
star
13

UniVRM-UTS2Extensions

This package is to allow UniVRM to export / import UTS2 shader.
C#
2
star
14

mackysoft

My profile
2
star
15

Unity-ManualActivation

This is a repository of instructions on how to acquire the ALF and ULF files for activating Unity.
2
star
16

Unity-DOTS-Examples

An example project that uses the Unity DOTS (ECS, Job, Burst).
C#
1
star
17

MessagePack-AsyncReactivePropertyExtensions

An extension formatters to support serialization of UniTask.AsyncReactiveProperty in MessagePack-CSharp.
C#
1
star