-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
Develop - 0.13.0
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using UnityEditor; | ||
using UnityEngine; | ||
|
||
namespace Toolbox.Editor.ContextMenu | ||
{ | ||
internal interface IContextMenuOperation | ||
{ | ||
bool IsVisible(SerializedProperty property); | ||
bool IsEnabled(SerializedProperty property); | ||
void Perform(SerializedProperty property); | ||
|
||
GUIContent Label { get; } | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System.Collections.Generic; | ||
|
||
using UnityEditor; | ||
|
||
namespace Toolbox.Editor.ContextMenu.Management | ||
{ | ||
using Toolbox.Editor.ContextMenu.Operations; | ||
|
||
[InitializeOnLoad] | ||
internal static class ToolboxContextMenuManager | ||
{ | ||
private static readonly List<IContextMenuOperation> registeredOperations; | ||
|
||
static ToolboxContextMenuManager() | ||
{ | ||
registeredOperations = new List<IContextMenuOperation>() | ||
{ | ||
new CopySerializeReferenceOperation(), | ||
new PasteSerializeReferenceOperation(), | ||
new DuplicateSerializeReferenceArrayElementOperation() | ||
}; | ||
|
||
EditorApplication.contextualPropertyMenu -= OnContextMenuOpening; | ||
EditorApplication.contextualPropertyMenu += OnContextMenuOpening; | ||
} | ||
|
||
public static void AppendOpertation(IContextMenuOperation operation) | ||
{ | ||
registeredOperations.Add(operation); | ||
} | ||
|
||
public static bool RemoveOperation(IContextMenuOperation operation) | ||
{ | ||
return registeredOperations.Remove(operation); | ||
} | ||
|
||
private static void OnContextMenuOpening(GenericMenu menu, SerializedProperty property) | ||
{ | ||
foreach (var operation in registeredOperations) | ||
{ | ||
if (!operation.IsVisible(property)) | ||
{ | ||
continue; | ||
} | ||
|
||
var label = operation.Label; | ||
if (!operation.IsEnabled(property)) | ||
{ | ||
menu.AddDisabledItem(label); | ||
continue; | ||
} | ||
|
||
menu.AddItem(label, false, () => | ||
{ | ||
operation.Perform(property); | ||
}); | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System; | ||
|
||
namespace Toolbox.Editor.ContextMenu.Operations | ||
{ | ||
internal class CopySerializeReferenceCache | ||
{ | ||
public CopySerializeReferenceCache(Type referenceType, string data) | ||
{ | ||
ReferenceType = referenceType; | ||
Data = data; | ||
} | ||
|
||
public Type ReferenceType { get; } | ||
public string Data { get; } | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using UnityEditor; | ||
using UnityEngine; | ||
|
||
namespace Toolbox.Editor.ContextMenu.Operations | ||
{ | ||
internal class CopySerializeReferenceOperation : IContextMenuOperation | ||
{ | ||
internal static CopySerializeReferenceCache Cache { get; private set; } | ||
|
||
[InitializeOnLoadMethod] | ||
private static void Initialize() | ||
{ | ||
Cache = null; | ||
} | ||
|
||
public bool IsVisible(SerializedProperty property) | ||
{ | ||
#if UNITY_2021_3_OR_NEWER | ||
return property != null && property.propertyType == SerializedPropertyType.ManagedReference; | ||
#else | ||
return false; | ||
#endif | ||
} | ||
|
||
public bool IsEnabled(SerializedProperty property) | ||
{ | ||
return true; | ||
} | ||
|
||
public void Perform(SerializedProperty property) | ||
{ | ||
#if UNITY_2021_3_OR_NEWER | ||
var value = property.managedReferenceValue; | ||
if (value != null) | ||
{ | ||
var referenceType = value.GetType(); | ||
var data = JsonUtility.ToJson(value); | ||
Cache = new CopySerializeReferenceCache(referenceType, data); | ||
return; | ||
} | ||
|
||
Cache = new CopySerializeReferenceCache(null, null); | ||
#endif | ||
} | ||
|
||
public GUIContent Label => new GUIContent("Copy Serialize Reference"); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using UnityEditor; | ||
using UnityEngine; | ||
|
||
namespace Toolbox.Editor.ContextMenu.Operations | ||
{ | ||
internal class DuplicateSerializeReferenceArrayElementOperation : IContextMenuOperation | ||
{ | ||
public bool IsVisible(SerializedProperty property) | ||
{ | ||
#if UNITY_2021_3_OR_NEWER | ||
return property != null && property.propertyType == SerializedPropertyType.ManagedReference && | ||
PropertyUtility.IsSerializableArrayElement(property); | ||
#else | ||
return false; | ||
#endif | ||
} | ||
|
||
public bool IsEnabled(SerializedProperty property) | ||
{ | ||
return true; | ||
} | ||
|
||
public void Perform(SerializedProperty property) | ||
{ | ||
#if UNITY_2021_3_OR_NEWER | ||
var sourceProperty = property.Copy(); | ||
sourceProperty.serializedObject.Update(); | ||
var sourceValue = sourceProperty.managedReferenceValue; | ||
|
||
var arrayProperty = PropertyUtility.GetArray(sourceProperty); | ||
var newElementIndex = arrayProperty.arraySize; | ||
arrayProperty.arraySize = newElementIndex + 1; | ||
//NOTE: there will be null by default anyway | ||
if (sourceValue != null) | ||
{ | ||
var targetData = JsonUtility.ToJson(sourceValue); | ||
var targetValue = JsonUtility.FromJson(targetData, sourceValue.GetType()); | ||
var targetProperty = arrayProperty.GetArrayElementAtIndex(newElementIndex); | ||
targetProperty.managedReferenceValue = targetValue; | ||
} | ||
|
||
sourceProperty.serializedObject.ApplyModifiedProperties(); | ||
#endif | ||
} | ||
|
||
public GUIContent Label => new GUIContent("Duplicate Serialize Reference Array Element"); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.