Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 05efde5001 | |||
| f275c468ec | |||
| c141acf129 | |||
| fdc0a286c8 | |||
| e5627b1caf | |||
| 5f54c254f3 | |||
| b8c11e7836 | |||
| f656cd0f12 | |||
| f69bfc6773 |
+41
-1
@@ -1,17 +1,20 @@
|
||||
// NOTE: managedReferenceValue getter is available only in Unity 2021.3 or later.
|
||||
#if UNITY_2021_3_OR_NEWER
|
||||
using System;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace MackySoft.SerializeReferenceExtensions.Editor
|
||||
{
|
||||
public static class CopyAndPasteProperty
|
||||
public static class ManagedReferenceContextualPropertyMenu
|
||||
{
|
||||
|
||||
const string kCopiedPropertyPathKey = "SerializeReferenceExtensions.CopiedPropertyPath";
|
||||
const string kClipboardKey = "SerializeReferenceExtensions.CopyAndPasteProperty";
|
||||
|
||||
static readonly GUIContent kPasteContent = new GUIContent("Paste Property");
|
||||
static readonly GUIContent kNewInstanceContent = new GUIContent("New Instance");
|
||||
static readonly GUIContent kResetAndNewInstanceContent = new GUIContent("Reset and New Instance");
|
||||
|
||||
[InitializeOnLoadMethod]
|
||||
static void Initialize ()
|
||||
@@ -38,6 +41,20 @@ namespace MackySoft.SerializeReferenceExtensions.Editor
|
||||
{
|
||||
menu.AddDisabledItem(kPasteContent);
|
||||
}
|
||||
|
||||
menu.AddSeparator("");
|
||||
|
||||
bool hasInstance = clonedProperty.managedReferenceValue != null;
|
||||
if (hasInstance)
|
||||
{
|
||||
menu.AddItem(kNewInstanceContent, false, NewInstance, clonedProperty);
|
||||
menu.AddItem(kResetAndNewInstanceContent, false, ResetAndNewInstance, clonedProperty);
|
||||
}
|
||||
else
|
||||
{
|
||||
menu.AddDisabledItem(kNewInstanceContent);
|
||||
menu.AddDisabledItem(kResetAndNewInstanceContent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,6 +79,29 @@ namespace MackySoft.SerializeReferenceExtensions.Editor
|
||||
JsonUtility.FromJsonOverwrite(json, property.managedReferenceValue);
|
||||
property.serializedObject.ApplyModifiedProperties();
|
||||
}
|
||||
|
||||
static void NewInstance (object customData)
|
||||
{
|
||||
SerializedProperty property = (SerializedProperty)customData;
|
||||
string json = JsonUtility.ToJson(property.managedReferenceValue);
|
||||
|
||||
Undo.RecordObject(property.serializedObject.targetObject, "New Instance");
|
||||
property.managedReferenceValue = JsonUtility.FromJson(json, property.managedReferenceValue.GetType());
|
||||
property.serializedObject.ApplyModifiedProperties();
|
||||
|
||||
Debug.Log($"Create new instance of \"{property.propertyPath}\".");
|
||||
}
|
||||
|
||||
static void ResetAndNewInstance (object customData)
|
||||
{
|
||||
SerializedProperty property = (SerializedProperty)customData;
|
||||
|
||||
Undo.RecordObject(property.serializedObject.targetObject, "Reset and New Instance");
|
||||
property.managedReferenceValue = Activator.CreateInstance(property.managedReferenceValue.GetType());
|
||||
property.serializedObject.ApplyModifiedProperties();
|
||||
|
||||
Debug.Log($"Reset property and created new instance of \"{property.propertyPath}\".");
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
+4
-2
@@ -4,7 +4,9 @@ using System.Reflection;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace MackySoft.SerializeReferenceExtensions.Editor {
|
||||
namespace MackySoft.SerializeReferenceExtensions.Editor
|
||||
{
|
||||
|
||||
public static class ManagedReferenceUtility {
|
||||
|
||||
public static object SetManagedReference (this SerializedProperty property,Type type) {
|
||||
@@ -12,7 +14,7 @@ namespace MackySoft.SerializeReferenceExtensions.Editor {
|
||||
|
||||
#if UNITY_2021_3_OR_NEWER
|
||||
// NOTE: managedReferenceValue getter is available only in Unity 2021.3 or later.
|
||||
if (property.managedReferenceValue != null && type != null)
|
||||
if ((type != null) && (property.managedReferenceValue != null))
|
||||
{
|
||||
// Restore an previous values from json.
|
||||
string json = JsonUtility.ToJson(property.managedReferenceValue);
|
||||
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
#if UNITY_2019_3_OR_NEWER
|
||||
using System.Collections.Generic;
|
||||
using UnityEditor;
|
||||
|
||||
namespace MackySoft.SerializeReferenceExtensions.Editor
|
||||
{
|
||||
public static class SerializedPropertyExtensions
|
||||
{
|
||||
public static IEnumerable<SerializedProperty> GetChildProperties (this SerializedProperty parent, int depth = 1)
|
||||
{
|
||||
parent = parent.Copy();
|
||||
|
||||
int depthOfParent = parent.depth;
|
||||
var enumerator = parent.GetEnumerator();
|
||||
|
||||
while (enumerator.MoveNext())
|
||||
{
|
||||
if (enumerator.Current is not SerializedProperty childProperty)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (childProperty.depth > (depthOfParent + depth))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
yield return childProperty.Copy();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b934aeca38cb7a24cabd6047fe0e298a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+42
-20
@@ -31,51 +31,74 @@ namespace MackySoft.SerializeReferenceExtensions.Editor
|
||||
|
||||
SerializedProperty m_TargetProperty;
|
||||
|
||||
public override void OnGUI (Rect position,SerializedProperty property,GUIContent label) {
|
||||
EditorGUI.BeginProperty(position,label,property);
|
||||
public override void OnGUI (Rect position, SerializedProperty property, GUIContent label)
|
||||
{
|
||||
EditorGUI.BeginProperty(position, label, property);
|
||||
|
||||
if (property.propertyType == SerializedPropertyType.ManagedReference) {
|
||||
|
||||
// render label first to avoid label overlap for lists
|
||||
if (property.propertyType == SerializedPropertyType.ManagedReference)
|
||||
{
|
||||
// Render label first to avoid label overlap for lists
|
||||
Rect foldoutLabelRect = new Rect(position);
|
||||
foldoutLabelRect.height = EditorGUIUtility.singleLineHeight;
|
||||
foldoutLabelRect.x += EditorGUI.indentLevel * 12;
|
||||
foldoutLabelRect = EditorGUI.IndentedRect(foldoutLabelRect);
|
||||
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);
|
||||
m_TargetProperty = property;
|
||||
popup.TypePopup.Show(popupPosition);
|
||||
}
|
||||
|
||||
// Check if a custom property drawer exists for this type.
|
||||
PropertyDrawer customDrawer = GetCustomPropertyDrawer(property);
|
||||
if (customDrawer != null)
|
||||
// Draw the foldout.
|
||||
if (!string.IsNullOrEmpty(property.managedReferenceFullTypename))
|
||||
{
|
||||
// Draw the property with custom property drawer.
|
||||
Rect foldoutRect = new Rect(position);
|
||||
foldoutRect.height = EditorGUIUtility.singleLineHeight;
|
||||
foldoutRect.x -= 12;
|
||||
property.isExpanded = EditorGUI.Foldout(foldoutRect, property.isExpanded, GUIContent.none, true);
|
||||
}
|
||||
|
||||
if (property.isExpanded)
|
||||
// Draw property if expanded.
|
||||
if (property.isExpanded)
|
||||
{
|
||||
using (new EditorGUI.IndentLevelScope())
|
||||
{
|
||||
using (new EditorGUI.IndentLevelScope())
|
||||
// Check if a custom property drawer exists for this type.
|
||||
PropertyDrawer customDrawer = GetCustomPropertyDrawer(property);
|
||||
if (customDrawer != null)
|
||||
{
|
||||
// Draw the property with custom property drawer.
|
||||
Rect indentedRect = position;
|
||||
float foldoutDifference = EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
|
||||
indentedRect.height = customDrawer.GetPropertyHeight(property, label);
|
||||
indentedRect.y += foldoutDifference;
|
||||
customDrawer.OnGUI(indentedRect, property, label);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Draw the properties of the child elements.
|
||||
// NOTE: In the following code, since the foldout layout isn't working properly, I'll iterate through the properties of the child elements myself.
|
||||
// EditorGUI.PropertyField(position, property, GUIContent.none, true);
|
||||
|
||||
Rect childPosition = position;
|
||||
childPosition.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
|
||||
foreach (SerializedProperty childProperty in property.GetChildProperties())
|
||||
{
|
||||
float height = EditorGUI.GetPropertyHeight(childProperty, new GUIContent(childProperty.displayName, childProperty.tooltip), true);
|
||||
childPosition.height = height;
|
||||
EditorGUI.PropertyField(childPosition, childProperty, true);
|
||||
|
||||
childPosition.y += height + EditorGUIUtility.standardVerticalSpacing;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
EditorGUI.PropertyField(position, property, GUIContent.none, true);
|
||||
}
|
||||
} else {
|
||||
EditorGUI.LabelField(position,label,k_IsNotManagedReferenceLabel);
|
||||
}
|
||||
else
|
||||
{
|
||||
EditorGUI.LabelField(position, label, k_IsNotManagedReferenceLabel);
|
||||
}
|
||||
|
||||
EditorGUI.EndProperty();
|
||||
@@ -117,7 +140,6 @@ namespace MackySoft.SerializeReferenceExtensions.Editor
|
||||
foreach (var targetObject in m_TargetProperty.serializedObject.targetObjects) {
|
||||
SerializedObject individualObject = new SerializedObject(targetObject);
|
||||
SerializedProperty individualProperty = individualObject.FindProperty(m_TargetProperty.propertyPath);
|
||||
|
||||
object obj = individualProperty.SetManagedReference(type);
|
||||
individualProperty.isExpanded = (obj != null);
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
"name": "com.mackysoft.serializereference-extensions",
|
||||
"displayName": "SerializeReference Extensions",
|
||||
"author": { "name": "MackySoft", "url": "https://github.com/mackysoft" },
|
||||
"version": "1.2.2",
|
||||
"version": "1.3.0",
|
||||
"unity": "2021.3",
|
||||
"description": "Provide popup to specify the type of the field serialized by the [SerializeReference] attribute in the inspector.",
|
||||
"keywords": [ "SerializeReference", "Editor" ],
|
||||
|
||||
Reference in New Issue
Block a user