Skip to content

AlexeyTaranov/SerializeReferenceDropdown

Repository files navigation

SerializeReferenceDropdown

Unity 2019+ License: MIT

Unity Editor dropdown for SerializeReference Attribute.

RefTo (Reference to SerializeReference).

Features:

Recommended:

  • Unity 2023+
  • Default UI Toolkit Inpsector
Why?

Serialization: Objects referenced from SerializeReference fields now have stable ids, which reduces risk of conflicts when multiple users collaborate on a scene file. This also improves support for undo and prefabs, especially when SerializeReference is used inside arrays and lists. There is a new format for references, with backward compatibility support for older assets.

Serialization: SerializeReference now allow more granular handling of missing types. SerializeReference instances for which the type is missing will be replaced by null. Other instances will be editable and if fields who were previously referring to the missing type are still null the missing type will be preserved.

Installation:

  1. Select in UPM "Add package from git URL..."
  2. Install package with link.
https://github.com/AlexeyTaranov/SerializeReferenceDropdown.git

Select type for Serialize Reference

SRD_SelectType.mov
Code Example
public class TestShapesForSRD : MonoBehaviour
{
    [SerializeReferenceDropdown]
    [SerializeReference]
    private IShape _singleShape;
    
    [SerializeReferenceDropdown]
    [SerializeReference]
    private IShape[] _shapesArray;
}

public interface IShape
{
}

[Serializable]
public class Circle : IShape
{
    [SerializeField]
    private float _radius;
}

[Serializable]
public class Rectangle : IShape
{
    [SerializeField]
    private float _sideA;
        
    [SerializeField]
    private float _sideB;
}

Copy/Paste context menu

SRD_CopyPaste.mov

Generics

Unity 2023.2+

You can use generics . Unspecified arguments need select in additional window.

Generics

Code Example
public class TestShapesForSRD : MonoBehaviour
{
    [SerializeReference, SerializeReferenceDropdown]
    private ISimpleGenericData<int> _intData;
}

public interface ISimpleGenericData<TData> : IAbstractData
{
    public TData Data { get; }
}

[Serializable]
public class GenericData<TData> : ISimpleGenericData<TData>
{
    [SerializeField] private TData _data;

    public TData Data => _data;
}

[Serializable]
public class GenericKeyValuePair<TKeyData, TValueData> : ISimpleGenericData<TKeyData>, IAbstractData
{
    [SerializeField] private TKeyData _key;
    [SerializeField] private TValueData _value;
    public TKeyData Data => _key;
}

Keep Data with new type

SRD_KeepData.mov

Fix cross references

SRD_CrossReferences.mov

Open source file

SRD_OpenSourceFile.mov

RefTo type: References to Serialize Reference

This class support references to Serialize References! You can assign RefTo with Drag and Drop or with context menu.

Inspector will help avoid not to assign wrong object types, runtime type checks - "as operator" cast.

SRD_RefTo.mov
[SerializeField] private RefTo<IShape,MonoBehaviour> _refShape;

public void Execute()
{
    var sampleString = _refShape.Get()?.ToString();
}    

Search Tool

SRD_SearchTool_trim.mov

Rider Integration

Rider plugin available here - https://github.com/AlexeyTaranov/SerializeReferenceDropdownIntegration

SRD_RiderIntergration.mov

About

(Unity) Editor tool for SerializeReference

Topics

Resources

License

MIT, Unknown licenses found

Licenses found

MIT
LICENSE
Unknown
LICENSE.meta

Stars

Watchers

Forks

Contributors 2

  •  
  •  

Languages