Rename editor namespaces.
This commit is contained in:
@@ -1,130 +1,129 @@
|
|||||||
#if UNITY_EDITOR
|
#if UNITY_EDITOR
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Linq.Expressions;
|
using System.Linq.Expressions;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using UnityEditor;
|
using UnityEditor;
|
||||||
using UnityEngine.UIElements;
|
|
||||||
|
namespace SerializableFunc.Editor.Extensions.DropdownField
|
||||||
namespace Utilities.Extensions.UIToolkit
|
{
|
||||||
{
|
public static class DropdownFieldExtensions
|
||||||
public static class DropdownFieldExtensions
|
{
|
||||||
{
|
private static Type genericOSMenuType;
|
||||||
private static Type genericOSMenuType;
|
private static Type iGenericMenuInterfaceType;
|
||||||
private static Type iGenericMenuInterfaceType;
|
|
||||||
|
private const string CreateMenuCallback_Property_Name = "createMenuCallback";
|
||||||
private const string CreateMenuCallback_Property_Name = "createMenuCallback";
|
private const string GenericOsMenu_Type_Name = "UnityEditor.UIElements.GenericOSMenu";
|
||||||
private const string GenericOsMenu_Type_Name = "UnityEditor.UIElements.GenericOSMenu";
|
private const string IGenericMenu_Type_Name = "IGenericMenu";
|
||||||
private const string IGenericMenu_Type_Name = "IGenericMenu";
|
|
||||||
|
private const string FormatSelectedValueCallback_Property_Name = "formatSelectedValueCallback";
|
||||||
private const string FormatSelectedValueCallback_Property_Name = "formatSelectedValueCallback";
|
|
||||||
|
#region Generic Menu Assigning
|
||||||
#region Generic Menu Assigning
|
|
||||||
|
public static void AssignGenericMenu(this UnityEngine.UIElements.DropdownField dropdownField,
|
||||||
public static void AssignGenericMenu(this DropdownField dropdownField,
|
Func<GenericMenu> genericMenuBuildingFunc)
|
||||||
Func<GenericMenu> genericMenuBuildingFunc)
|
{
|
||||||
{
|
Func<object> genericOSMenuFunc = () =>
|
||||||
Func<object> genericOSMenuFunc = () =>
|
{
|
||||||
{
|
GenericMenu menu = genericMenuBuildingFunc();
|
||||||
GenericMenu menu = genericMenuBuildingFunc();
|
return GetGenericOSMenu(menu);
|
||||||
return GetGenericOSMenu(menu);
|
};
|
||||||
};
|
|
||||||
|
object boxedfunc = ConvertFuncToDesiredType(genericOSMenuFunc);
|
||||||
object boxedfunc = ConvertFuncToDesiredType(genericOSMenuFunc);
|
|
||||||
|
FieldInfo createMenuCallbackFieldInfo = typeof(UnityEngine.UIElements.DropdownField)
|
||||||
FieldInfo createMenuCallbackFieldInfo = typeof(DropdownField)
|
.GetField(CreateMenuCallback_Property_Name, BindingFlags.FlattenHierarchy | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
|
||||||
.GetField(CreateMenuCallback_Property_Name, BindingFlags.FlattenHierarchy | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
|
|
||||||
|
createMenuCallbackFieldInfo.SetValue(dropdownField, boxedfunc);
|
||||||
createMenuCallbackFieldInfo.SetValue(dropdownField, boxedfunc);
|
}
|
||||||
}
|
|
||||||
|
#region Generic OS Menu Creation
|
||||||
#region Generic OS Menu Creation
|
|
||||||
|
private static object GetGenericOSMenu(GenericMenu menu)
|
||||||
private static object GetGenericOSMenu(GenericMenu menu)
|
{
|
||||||
{
|
Type type = GetGenericOSMenuType();
|
||||||
Type type = GetGenericOSMenuType();
|
object[] args = new object[] { menu };
|
||||||
object[] args = new object[] { menu };
|
return Activator.CreateInstance(type, args);
|
||||||
return Activator.CreateInstance(type, args);
|
}
|
||||||
}
|
|
||||||
|
#endregion
|
||||||
#endregion
|
|
||||||
|
#region Type helpers
|
||||||
#region Type helpers
|
|
||||||
|
private static Type GetGenericOSMenuType()
|
||||||
private static Type GetGenericOSMenuType()
|
{
|
||||||
{
|
if (genericOSMenuType == null)
|
||||||
if (genericOSMenuType == null)
|
{
|
||||||
{
|
genericOSMenuType = typeof(UnityEditor.UIElements.ColorField).Assembly.GetType(GenericOsMenu_Type_Name, false, true);
|
||||||
genericOSMenuType = typeof(UnityEditor.UIElements.ColorField).Assembly.GetType(GenericOsMenu_Type_Name, false, true);
|
}
|
||||||
}
|
|
||||||
|
return genericOSMenuType;
|
||||||
return genericOSMenuType;
|
}
|
||||||
}
|
|
||||||
|
private static Type GetIGenericMenuInterfaceType()
|
||||||
private static Type GetIGenericMenuInterfaceType()
|
{
|
||||||
{
|
if (iGenericMenuInterfaceType == null)
|
||||||
if (iGenericMenuInterfaceType == null)
|
{
|
||||||
{
|
Type genericOSType = GetGenericOSMenuType();
|
||||||
Type genericOSType = GetGenericOSMenuType();
|
iGenericMenuInterfaceType = genericOSType.GetInterface(IGenericMenu_Type_Name);
|
||||||
iGenericMenuInterfaceType = genericOSType.GetInterface(IGenericMenu_Type_Name);
|
}
|
||||||
}
|
|
||||||
|
return iGenericMenuInterfaceType;
|
||||||
return iGenericMenuInterfaceType;
|
}
|
||||||
}
|
|
||||||
|
#endregion
|
||||||
#endregion
|
|
||||||
|
#region Func Conversion
|
||||||
#region Func Conversion
|
|
||||||
|
private static Delegate ConvertFuncToDesiredType(Func<object> func)
|
||||||
private static Delegate ConvertFuncToDesiredType(Func<object> func)
|
{
|
||||||
{
|
Type interfaceType = GetIGenericMenuInterfaceType();
|
||||||
Type interfaceType = GetIGenericMenuInterfaceType();
|
Type resultType = typeof(Func<>).MakeGenericType(interfaceType);
|
||||||
Type resultType = typeof(Func<>).MakeGenericType(interfaceType);
|
|
||||||
|
Expression<Func<object>> expressionFunc = FuncToExpression(func);
|
||||||
Expression<Func<object>> expressionFunc = FuncToExpression(func);
|
|
||||||
|
InvocationExpression invokedExpression = Expression.Invoke(expressionFunc);
|
||||||
InvocationExpression invokedExpression = Expression.Invoke(expressionFunc);
|
UnaryExpression convertedReturnValue = Expression.Convert(invokedExpression, interfaceType);
|
||||||
UnaryExpression convertedReturnValue = Expression.Convert(invokedExpression, interfaceType);
|
|
||||||
|
LambdaExpression lambda = Expression.Lambda(delegateType: resultType, body: convertedReturnValue);
|
||||||
LambdaExpression lambda = Expression.Lambda(delegateType: resultType, body: convertedReturnValue);
|
|
||||||
|
return lambda.Compile();
|
||||||
return lambda.Compile();
|
}
|
||||||
}
|
|
||||||
|
private static Expression<Func<T>> FuncToExpression<T>(Func<T> f)
|
||||||
private static Expression<Func<T>> FuncToExpression<T>(Func<T> f)
|
{
|
||||||
{
|
return () => f();
|
||||||
return () => f();
|
}
|
||||||
}
|
|
||||||
|
#endregion
|
||||||
#endregion
|
|
||||||
|
#endregion
|
||||||
#endregion
|
|
||||||
|
#region Formatting Callback Assigning
|
||||||
#region Formatting Callback Assigning
|
|
||||||
|
public static void AssignFormattingCallback(this UnityEngine.UIElements.DropdownField dropdownField,
|
||||||
public static void AssignFormattingCallback(this DropdownField dropdownField,
|
Func<string, string> formattingCallback)
|
||||||
Func<string, string> formattingCallback)
|
{
|
||||||
{
|
#if UNITY_2022_2_OR_NEWER
|
||||||
#if UNITY_2022_2_OR_NEWER
|
dropdownField.formatSelectedValueCallback = formattingCallback;
|
||||||
dropdownField.formatSelectedValueCallback = formattingCallback;
|
#else
|
||||||
#else
|
AssignFormattingCallbackViaReflection(dropdownField, formattingCallback);
|
||||||
AssignFormattingCallbackViaReflection(dropdownField, formattingCallback);
|
#endif
|
||||||
#endif
|
}
|
||||||
}
|
|
||||||
|
private static void AssignFormattingCallbackViaReflection(this UnityEngine.UIElements.DropdownField functionDropdown,
|
||||||
private static void AssignFormattingCallbackViaReflection(this DropdownField functionDropdown,
|
Func<string, string> formattingCallback)
|
||||||
Func<string, string> formattingCallback)
|
{
|
||||||
{
|
PropertyInfo propertyInfo = typeof(UnityEngine.UIElements.DropdownField)
|
||||||
PropertyInfo propertyInfo = typeof(DropdownField)
|
.GetProperty(FormatSelectedValueCallback_Property_Name, BindingFlags.FlattenHierarchy | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
|
||||||
.GetProperty(FormatSelectedValueCallback_Property_Name, BindingFlags.FlattenHierarchy | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
|
|
||||||
|
object[] args = new object[] { formattingCallback };
|
||||||
object[] args = new object[] { formattingCallback };
|
|
||||||
|
propertyInfo.SetMethod.Invoke(functionDropdown, args);
|
||||||
propertyInfo.SetMethod.Invoke(functionDropdown, args);
|
}
|
||||||
}
|
|
||||||
|
#endregion
|
||||||
#endregion
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -4,15 +4,14 @@ using System.Collections;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using UnityEditor;
|
|
||||||
|
|
||||||
namespace UnityUtilities.SerializableDataHelpers.UnityEditorUtilities
|
namespace SerializableFunc.Editor.Extensions.SerializedProperty
|
||||||
{
|
{
|
||||||
public static class SerializedPropertyExtensions
|
public static class SerializedPropertyExtensions
|
||||||
{
|
{
|
||||||
static readonly Regex rgx = new Regex(@"\[\d+\]", RegexOptions.Compiled);
|
static readonly Regex rgx = new Regex(@"\[\d+\]", RegexOptions.Compiled);
|
||||||
|
|
||||||
public static object GetBoxedValue(this SerializedProperty property)
|
public static object GetBoxedValue(this UnityEditor.SerializedProperty property)
|
||||||
{
|
{
|
||||||
property.serializedObject.ApplyModifiedProperties();
|
property.serializedObject.ApplyModifiedProperties();
|
||||||
#if UNITY_2023_2_OR_NEWER
|
#if UNITY_2023_2_OR_NEWER
|
||||||
@@ -22,7 +21,7 @@ namespace UnityUtilities.SerializableDataHelpers.UnityEditorUtilities
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void SetBoxedValue(this SerializedProperty property, object value)
|
public static void SetBoxedValue(this UnityEditor.SerializedProperty property, object value)
|
||||||
{
|
{
|
||||||
property.serializedObject.Update();
|
property.serializedObject.Update();
|
||||||
#if UNITY_2023_2_OR_NEWER
|
#if UNITY_2023_2_OR_NEWER
|
||||||
@@ -33,7 +32,7 @@ namespace UnityUtilities.SerializableDataHelpers.UnityEditorUtilities
|
|||||||
property.serializedObject.ApplyModifiedProperties();
|
property.serializedObject.ApplyModifiedProperties();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static T GetValue<T>(this SerializedProperty property) where T : class
|
public static T GetValue<T>(this UnityEditor.SerializedProperty property) where T : class
|
||||||
{
|
{
|
||||||
object obj = property.serializedObject.targetObject;
|
object obj = property.serializedObject.targetObject;
|
||||||
string path = property.propertyPath.Replace(".Array.data", string.Empty);
|
string path = property.propertyPath.Replace(".Array.data", string.Empty);
|
||||||
@@ -53,7 +52,7 @@ namespace UnityUtilities.SerializableDataHelpers.UnityEditorUtilities
|
|||||||
return (T)obj;
|
return (T)obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static bool SetValue<T>(this SerializedProperty property, T value) where T : class
|
public static bool SetValue<T>(this UnityEditor.SerializedProperty property, T value) where T : class
|
||||||
{
|
{
|
||||||
object obj = property.serializedObject.targetObject;
|
object obj = property.serializedObject.targetObject;
|
||||||
string path = property.propertyPath.Replace(".Array.data", "");
|
string path = property.propertyPath.Replace(".Array.data", "");
|
||||||
|
|||||||
@@ -1,60 +1,60 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
namespace Utilities.Extensions.SystemExtensions
|
namespace SerializableFunc.Editor.Extensions.TypeExtensions
|
||||||
{
|
{
|
||||||
public static class TypeExtensions
|
public static class TypeExtensions
|
||||||
{
|
{
|
||||||
private static Dictionary<Type, string> PrimitiveTypesLookup = new Dictionary<Type, string>
|
private static Dictionary<Type, string> PrimitiveTypesLookup = new Dictionary<Type, string>
|
||||||
{
|
{
|
||||||
{ typeof(bool), "Bool" },
|
{ typeof(bool), "Bool" },
|
||||||
{ typeof(byte), "Byte" },
|
{ typeof(byte), "Byte" },
|
||||||
{ typeof(char), "Char" },
|
{ typeof(char), "Char" },
|
||||||
{ typeof(decimal), "Decimal" },
|
{ typeof(decimal), "Decimal" },
|
||||||
{ typeof(double), "Double" },
|
{ typeof(double), "Double" },
|
||||||
{ typeof(float), "Float" },
|
{ typeof(float), "Float" },
|
||||||
{ typeof(int), "Int" },
|
{ typeof(int), "Int" },
|
||||||
{ typeof(long), "Long" },
|
{ typeof(long), "Long" },
|
||||||
{ typeof(sbyte), "Sbyte" },
|
{ typeof(sbyte), "Sbyte" },
|
||||||
{ typeof(short), "Short" },
|
{ typeof(short), "Short" },
|
||||||
{ typeof(string), "String" },
|
{ typeof(string), "String" },
|
||||||
{ typeof(uint), "Uint" },
|
{ typeof(uint), "Uint" },
|
||||||
{ typeof(ulong), "Ulong" },
|
{ typeof(ulong), "Ulong" },
|
||||||
{ typeof(ushort), "Ushort" },
|
{ typeof(ushort), "Ushort" },
|
||||||
};
|
};
|
||||||
|
|
||||||
public static string NicifyTypeName(this Type type)
|
public static string NicifyTypeName(this Type type)
|
||||||
{
|
{
|
||||||
if (type.IsArray)
|
if (type.IsArray)
|
||||||
{
|
{
|
||||||
Type elementType = type.GetElementType();
|
Type elementType = type.GetElementType();
|
||||||
string elementTypeString = NicifyTypeName(elementType);
|
string elementTypeString = NicifyTypeName(elementType);
|
||||||
return $"{elementTypeString}[]";
|
return $"{elementTypeString}[]";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!type.IsGenericType)
|
if (!type.IsGenericType)
|
||||||
{
|
{
|
||||||
return GetSingleTypeName(type);
|
return GetSingleTypeName(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type.IsNested && type.DeclaringType.IsGenericType) return type.Name;
|
if (type.IsNested && type.DeclaringType.IsGenericType) return type.Name;
|
||||||
|
|
||||||
string genericTypeString = $"{type.Name[..type.Name.IndexOf('`')]}";
|
string genericTypeString = $"{type.Name[..type.Name.IndexOf('`')]}";
|
||||||
|
|
||||||
IEnumerable<string> genericArgumentNames = type.GetGenericArguments()
|
IEnumerable<string> genericArgumentNames = type.GetGenericArguments()
|
||||||
.Select(x => NicifyTypeName(x));
|
.Select(x => NicifyTypeName(x));
|
||||||
|
|
||||||
string genericArgumentsString = string.Join(", ", genericArgumentNames);
|
string genericArgumentsString = string.Join(", ", genericArgumentNames);
|
||||||
|
|
||||||
return $"{genericTypeString}<{genericArgumentsString}>";
|
return $"{genericTypeString}<{genericArgumentsString}>";
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string GetSingleTypeName(Type type)
|
private static string GetSingleTypeName(Type type)
|
||||||
{
|
{
|
||||||
if (PrimitiveTypesLookup.TryGetValue(type, out string result)) return result;
|
if (PrimitiveTypesLookup.TryGetValue(type, out string result)) return result;
|
||||||
|
|
||||||
return type.Name;
|
return type.Name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -5,21 +5,21 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using SerializableFunc;
|
using SerializableFunc.Editor.Extensions.DropdownField;
|
||||||
|
using SerializableFunc.Editor.Extensions.SerializedProperty;
|
||||||
|
using SerializableFunc.Editor.Extensions.TypeExtensions;
|
||||||
using UnityEditor;
|
using UnityEditor;
|
||||||
using UnityEditor.UIElements;
|
using UnityEditor.UIElements;
|
||||||
using UnityEditorInternal;
|
using UnityEditorInternal;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEngine.UIElements;
|
using UnityEngine.UIElements;
|
||||||
using Utilities.Extensions.SystemExtensions;
|
|
||||||
using Utilities.Extensions.UIToolkit;
|
|
||||||
using Object = UnityEngine.Object;
|
using Object = UnityEngine.Object;
|
||||||
|
|
||||||
namespace UnityUtilities.SerializableDataHelpers.UnityEditorUtilities
|
namespace SerializableFunc.Editor.PropertyDrawer
|
||||||
{
|
{
|
||||||
[CustomPropertyDrawer(typeof(SerializableAction), true)]
|
[CustomPropertyDrawer(typeof(SerializableAction), true)]
|
||||||
[CustomPropertyDrawer(typeof(SerializableFuncBase<>), true)]
|
[CustomPropertyDrawer(typeof(SerializableFuncBase<>), true)]
|
||||||
public class SerializableFuncBasePropertyDrawer : PropertyDrawer
|
public class SerializableFuncBasePropertyDrawer : UnityEditor.PropertyDrawer
|
||||||
{
|
{
|
||||||
private const string Target_Function_Label = "Target Function";
|
private const string Target_Function_Label = "Target Function";
|
||||||
private const string Target_Object_Label = "Target Object";
|
private const string Target_Object_Label = "Target Object";
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "SerializableFunc.Editor",
|
"name": "SerializableFunc.Editor",
|
||||||
"rootNamespace": "",
|
"rootNamespace": "SerializableFunc",
|
||||||
"references": [
|
"references": [
|
||||||
"GUID:e72a8e3728c787f4fafc146b590e6be5"
|
"GUID:e72a8e3728c787f4fafc146b590e6be5"
|
||||||
],
|
],
|
||||||
|
|||||||
Reference in New Issue
Block a user