Giter Club home page Giter Club logo

unity-serializereferenceextensions's Introduction

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.
  • [New] Available CustomPropertyDrawer for subclasses.
  • [New] Restore values of previous object from JSON when subclass is changed. (required Unity 2021.3 or later)
  • [New] Copy & Paste the subclass properties. (required Unity 2021.3 or later)
  • [New] Clear & reset the subclass properties. (required Unity 2021.3 or later)

See below for the reason for the limitation of versions less than Unity 2021.3.

https://blog.unity.com/engine-platform/serializereference-improvements-in-unity-2021-lts

๐Ÿ“ฅ Installation

Install via .unitypackage

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.

If you are specifying a version, enter #{VERSION} at the end, as shown below.

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

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

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

unity-serializereferenceextensions's People

Contributors

mackysoft avatar github-actions[bot] avatar yellowisher avatar christides11 avatar johannesdeml avatar shiena avatar repiteo avatar chadefranklin avatar theo-rapidfire avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.