Compare commits
13 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7f424c0f97 | |||
| 2934eea84e | |||
| f781ad7373 | |||
| e5a5157b3c | |||
| 69830f3583 | |||
| c9b5193e51 | |||
| a8bcece352 | |||
| 70f2cdaf16 | |||
| 97c4e632bc | |||
| 93d7e13f62 | |||
| f522e0f285 | |||
| d061f748b8 | |||
| afb3089943 |
+2
-1
@@ -1,4 +1,5 @@
|
|||||||
#if UNITY_2019_3_OR_NEWER
|
// NOTE: managedReferenceValue getter is available only in Unity 2021.3 or later.
|
||||||
|
#if UNITY_2021_3_OR_NEWER
|
||||||
using UnityEditor;
|
using UnityEditor;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
|
|||||||
+8
-3
@@ -8,14 +8,19 @@ namespace MackySoft.SerializeReferenceExtensions.Editor {
|
|||||||
public static class ManagedReferenceUtility {
|
public static class ManagedReferenceUtility {
|
||||||
|
|
||||||
public static object SetManagedReference (this SerializedProperty property,Type type) {
|
public static object SetManagedReference (this SerializedProperty property,Type type) {
|
||||||
object result;
|
object result = null;
|
||||||
if (property.managedReferenceValue != null)
|
|
||||||
|
#if UNITY_2021_3_OR_NEWER
|
||||||
|
// NOTE: managedReferenceValue getter is available only in Unity 2021.3 or later.
|
||||||
|
if (property.managedReferenceValue != null && type != null)
|
||||||
{
|
{
|
||||||
// Restore an previous values from json.
|
// Restore an previous values from json.
|
||||||
string json = JsonUtility.ToJson(property.managedReferenceValue);
|
string json = JsonUtility.ToJson(property.managedReferenceValue);
|
||||||
result = JsonUtility.FromJson(json, type);
|
result = JsonUtility.FromJson(json, type);
|
||||||
}
|
}
|
||||||
else
|
#endif
|
||||||
|
|
||||||
|
if (result == null)
|
||||||
{
|
{
|
||||||
result = (type != null) ? Activator.CreateInstance(type) : null;
|
result = (type != null) ? Activator.CreateInstance(type) : null;
|
||||||
}
|
}
|
||||||
|
|||||||
+34
-2
@@ -25,6 +25,8 @@ namespace MackySoft.SerializeReferenceExtensions.Editor
|
|||||||
|
|
||||||
static Type GetCustomPropertyDrawerType (Type type)
|
static Type GetCustomPropertyDrawerType (Type type)
|
||||||
{
|
{
|
||||||
|
Type[] interfaceTypes = type.GetInterfaces();
|
||||||
|
|
||||||
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
|
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
|
||||||
{
|
{
|
||||||
foreach (Type drawerType in assembly.GetTypes())
|
foreach (Type drawerType in assembly.GetTypes())
|
||||||
@@ -36,9 +38,39 @@ namespace MackySoft.SerializeReferenceExtensions.Editor
|
|||||||
if (field != null)
|
if (field != null)
|
||||||
{
|
{
|
||||||
var fieldType = field.GetValue(customPropertyDrawer) as Type;
|
var fieldType = field.GetValue(customPropertyDrawer) as Type;
|
||||||
if (fieldType != null && fieldType == type)
|
if (fieldType != null)
|
||||||
{
|
{
|
||||||
return drawerType;
|
if (fieldType == type)
|
||||||
|
{
|
||||||
|
return drawerType;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the property drawer also allows for being applied to child classes, check if they match
|
||||||
|
var useForChildrenField = customPropertyDrawer.GetType().GetField("m_UseForChildren", BindingFlags.NonPublic | BindingFlags.Instance);
|
||||||
|
if (useForChildrenField != null)
|
||||||
|
{
|
||||||
|
object useForChildrenValue = useForChildrenField.GetValue(customPropertyDrawer);
|
||||||
|
if (useForChildrenValue is bool && (bool)useForChildrenValue)
|
||||||
|
{
|
||||||
|
// Check interfaces
|
||||||
|
if (Array.Exists(interfaceTypes, interfaceType => interfaceType == fieldType))
|
||||||
|
{
|
||||||
|
return drawerType;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check derived types
|
||||||
|
Type baseType = type.BaseType;
|
||||||
|
while (baseType != null)
|
||||||
|
{
|
||||||
|
if (baseType == fieldType)
|
||||||
|
{
|
||||||
|
return drawerType;
|
||||||
|
}
|
||||||
|
|
||||||
|
baseType = baseType.BaseType;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+12
-10
@@ -35,12 +35,14 @@ namespace MackySoft.SerializeReferenceExtensions.Editor
|
|||||||
EditorGUI.BeginProperty(position,label,property);
|
EditorGUI.BeginProperty(position,label,property);
|
||||||
|
|
||||||
if (property.propertyType == SerializedPropertyType.ManagedReference) {
|
if (property.propertyType == SerializedPropertyType.ManagedReference) {
|
||||||
// Draw the subclass selector popup.
|
|
||||||
Rect popupPosition = new Rect(position);
|
|
||||||
popupPosition.width -= EditorGUIUtility.labelWidth;
|
|
||||||
popupPosition.x += EditorGUIUtility.labelWidth;
|
|
||||||
popupPosition.height = EditorGUIUtility.singleLineHeight;
|
|
||||||
|
|
||||||
|
// render label first to avoid label overlap for lists
|
||||||
|
Rect foldoutLabelRect = new Rect(position);
|
||||||
|
foldoutLabelRect.height = EditorGUIUtility.singleLineHeight;
|
||||||
|
foldoutLabelRect.x += EditorGUI.indentLevel * 12;
|
||||||
|
Rect popupPosition = EditorGUI.PrefixLabel(foldoutLabelRect, label);
|
||||||
|
|
||||||
|
// Draw the subclass selector popup.
|
||||||
if (EditorGUI.DropdownButton(popupPosition,GetTypeName(property),FocusType.Keyboard)) {
|
if (EditorGUI.DropdownButton(popupPosition,GetTypeName(property),FocusType.Keyboard)) {
|
||||||
TypePopupCache popup = GetTypePopup(property);
|
TypePopupCache popup = GetTypePopup(property);
|
||||||
m_TargetProperty = property;
|
m_TargetProperty = property;
|
||||||
@@ -54,8 +56,8 @@ namespace MackySoft.SerializeReferenceExtensions.Editor
|
|||||||
// Draw the property with custom property drawer.
|
// Draw the property with custom property drawer.
|
||||||
Rect foldoutRect = new Rect(position);
|
Rect foldoutRect = new Rect(position);
|
||||||
foldoutRect.height = EditorGUIUtility.singleLineHeight;
|
foldoutRect.height = EditorGUIUtility.singleLineHeight;
|
||||||
property.isExpanded = EditorGUI.Foldout(foldoutRect, property.isExpanded, label, true);
|
property.isExpanded = EditorGUI.Foldout(foldoutRect, property.isExpanded, GUIContent.none, true);
|
||||||
|
|
||||||
if (property.isExpanded)
|
if (property.isExpanded)
|
||||||
{
|
{
|
||||||
using (new EditorGUI.IndentLevelScope())
|
using (new EditorGUI.IndentLevelScope())
|
||||||
@@ -70,7 +72,7 @@ namespace MackySoft.SerializeReferenceExtensions.Editor
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
EditorGUI.PropertyField(position, property, label, true);
|
EditorGUI.PropertyField(position, property, GUIContent.none, true);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
EditorGUI.LabelField(position,label,k_IsNotManagedReferenceLabel);
|
EditorGUI.LabelField(position,label,k_IsNotManagedReferenceLabel);
|
||||||
@@ -95,7 +97,7 @@ namespace MackySoft.SerializeReferenceExtensions.Editor
|
|||||||
|
|
||||||
if (!m_TypePopups.TryGetValue(managedReferenceFieldTypename,out TypePopupCache result)) {
|
if (!m_TypePopups.TryGetValue(managedReferenceFieldTypename,out TypePopupCache result)) {
|
||||||
var state = new AdvancedDropdownState();
|
var state = new AdvancedDropdownState();
|
||||||
|
|
||||||
Type baseType = ManagedReferenceUtility.GetType(managedReferenceFieldTypename);
|
Type baseType = ManagedReferenceUtility.GetType(managedReferenceFieldTypename);
|
||||||
var popup = new AdvancedTypePopup(
|
var popup = new AdvancedTypePopup(
|
||||||
TypeCache.GetTypesDerivedFrom(baseType).Append(baseType).Where(p =>
|
TypeCache.GetTypesDerivedFrom(baseType).Append(baseType).Where(p =>
|
||||||
@@ -118,7 +120,7 @@ namespace MackySoft.SerializeReferenceExtensions.Editor
|
|||||||
|
|
||||||
object obj = individualProperty.SetManagedReference(type);
|
object obj = individualProperty.SetManagedReference(type);
|
||||||
individualProperty.isExpanded = (obj != null);
|
individualProperty.isExpanded = (obj != null);
|
||||||
|
|
||||||
individualObject.ApplyModifiedProperties();
|
individualObject.ApplyModifiedProperties();
|
||||||
individualObject.Update();
|
individualObject.Update();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
"name": "com.mackysoft.serializereference-extensions",
|
"name": "com.mackysoft.serializereference-extensions",
|
||||||
"displayName": "SerializeReference Extensions",
|
"displayName": "SerializeReference Extensions",
|
||||||
"author": { "name": "MackySoft", "url": "https://github.com/mackysoft" },
|
"author": { "name": "MackySoft", "url": "https://github.com/mackysoft" },
|
||||||
"version": "1.2.0",
|
"version": "1.2.2",
|
||||||
"unity": "2021.3",
|
"unity": "2021.3",
|
||||||
"description": "Provide popup to specify the type of the field serialized by the [SerializeReference] attribute in the inspector.",
|
"description": "Provide popup to specify the type of the field serialized by the [SerializeReference] attribute in the inspector.",
|
||||||
"keywords": [ "SerializeReference", "Editor" ],
|
"keywords": [ "SerializeReference", "Editor" ],
|
||||||
|
|||||||
@@ -15,9 +15,18 @@ The `SubclassSelector` attribute allows you to easily set subclasses of those ab
|
|||||||
- Easily set subclass by popup.
|
- Easily set subclass by popup.
|
||||||
- **[New]** Type finding by fuzzy finder.
|
- **[New]** Type finding by fuzzy finder.
|
||||||
- **[New]** Override the type name and path by the `AddTypeMenu` attribute.
|
- **[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)
|
||||||
|
|
||||||
|
> 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
|
## 📥 Installation
|
||||||
|
|
||||||
|
#### Install via `.unitypackage`
|
||||||
|
|
||||||
Download any version from releases.
|
Download any version from releases.
|
||||||
|
|
||||||
Releases: https://github.com/mackysoft/Unity-SerializeReferenceExtensions/releases
|
Releases: https://github.com/mackysoft/Unity-SerializeReferenceExtensions/releases
|
||||||
@@ -26,10 +35,17 @@ Releases: https://github.com/mackysoft/Unity-SerializeReferenceExtensions/releas
|
|||||||
|
|
||||||
Or, you can add this package by opening PackageManager and entering
|
Or, you can add this package by opening PackageManager and entering
|
||||||
|
|
||||||
`https://github.com/mackysoft/Unity-SerializeReferenceExtensions.git?path=Assets/MackySoft/MackySoft.SerializeReferenceExtensions`
|
```
|
||||||
|
https://github.com/mackysoft/Unity-SerializeReferenceExtensions.git?path=Assets/MackySoft/MackySoft.SerializeReferenceExtensions
|
||||||
|
```
|
||||||
|
|
||||||
from the `Add package from git URL` option.
|
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
|
#### Install via Open UPM
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user