diff --git a/Editor/Extensions/Dropdown Field.meta b/Editor/Extensions/DropdownField.meta similarity index 100% rename from Editor/Extensions/Dropdown Field.meta rename to Editor/Extensions/DropdownField.meta diff --git a/Editor/Extensions/Dropdown Field/DropdownFieldExtensions.cs b/Editor/Extensions/DropdownField/DropdownFieldExtensions.cs similarity index 87% rename from Editor/Extensions/Dropdown Field/DropdownFieldExtensions.cs rename to Editor/Extensions/DropdownField/DropdownFieldExtensions.cs index fbee6cc..651a071 100644 --- a/Editor/Extensions/Dropdown Field/DropdownFieldExtensions.cs +++ b/Editor/Extensions/DropdownField/DropdownFieldExtensions.cs @@ -1,130 +1,129 @@ -#if UNITY_EDITOR - -using System; -using System.Linq.Expressions; -using System.Reflection; -using UnityEditor; -using UnityEngine.UIElements; - -namespace Utilities.Extensions.UIToolkit -{ - public static class DropdownFieldExtensions - { - private static Type genericOSMenuType; - private static Type iGenericMenuInterfaceType; - - private const string CreateMenuCallback_Property_Name = "createMenuCallback"; - private const string GenericOsMenu_Type_Name = "UnityEditor.UIElements.GenericOSMenu"; - private const string IGenericMenu_Type_Name = "IGenericMenu"; - - private const string FormatSelectedValueCallback_Property_Name = "formatSelectedValueCallback"; - - #region Generic Menu Assigning - - public static void AssignGenericMenu(this DropdownField dropdownField, - Func genericMenuBuildingFunc) - { - Func genericOSMenuFunc = () => - { - GenericMenu menu = genericMenuBuildingFunc(); - return GetGenericOSMenu(menu); - }; - - object boxedfunc = ConvertFuncToDesiredType(genericOSMenuFunc); - - FieldInfo createMenuCallbackFieldInfo = typeof(DropdownField) - .GetField(CreateMenuCallback_Property_Name, BindingFlags.FlattenHierarchy | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); - - createMenuCallbackFieldInfo.SetValue(dropdownField, boxedfunc); - } - - #region Generic OS Menu Creation - - private static object GetGenericOSMenu(GenericMenu menu) - { - Type type = GetGenericOSMenuType(); - object[] args = new object[] { menu }; - return Activator.CreateInstance(type, args); - } - - #endregion - - #region Type helpers - - private static Type GetGenericOSMenuType() - { - if (genericOSMenuType == null) - { - genericOSMenuType = typeof(UnityEditor.UIElements.ColorField).Assembly.GetType(GenericOsMenu_Type_Name, false, true); - } - - return genericOSMenuType; - } - - private static Type GetIGenericMenuInterfaceType() - { - if (iGenericMenuInterfaceType == null) - { - Type genericOSType = GetGenericOSMenuType(); - iGenericMenuInterfaceType = genericOSType.GetInterface(IGenericMenu_Type_Name); - } - - return iGenericMenuInterfaceType; - } - - #endregion - - #region Func Conversion - - private static Delegate ConvertFuncToDesiredType(Func func) - { - Type interfaceType = GetIGenericMenuInterfaceType(); - Type resultType = typeof(Func<>).MakeGenericType(interfaceType); - - Expression> expressionFunc = FuncToExpression(func); - - InvocationExpression invokedExpression = Expression.Invoke(expressionFunc); - UnaryExpression convertedReturnValue = Expression.Convert(invokedExpression, interfaceType); - - LambdaExpression lambda = Expression.Lambda(delegateType: resultType, body: convertedReturnValue); - - return lambda.Compile(); - } - - private static Expression> FuncToExpression(Func f) - { - return () => f(); - } - - #endregion - - #endregion - - #region Formatting Callback Assigning - - public static void AssignFormattingCallback(this DropdownField dropdownField, - Func formattingCallback) - { -#if UNITY_2022_2_OR_NEWER - dropdownField.formatSelectedValueCallback = formattingCallback; -#else - AssignFormattingCallbackViaReflection(dropdownField, formattingCallback); -#endif - } - - private static void AssignFormattingCallbackViaReflection(this DropdownField functionDropdown, - Func formattingCallback) - { - PropertyInfo propertyInfo = typeof(DropdownField) - .GetProperty(FormatSelectedValueCallback_Property_Name, BindingFlags.FlattenHierarchy | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); - - object[] args = new object[] { formattingCallback }; - - propertyInfo.SetMethod.Invoke(functionDropdown, args); - } - - #endregion - } -} - +#if UNITY_EDITOR + +using System; +using System.Linq.Expressions; +using System.Reflection; +using UnityEditor; + +namespace SerializableFunc.Editor.Extensions.DropdownField +{ + public static class DropdownFieldExtensions + { + private static Type genericOSMenuType; + private static Type iGenericMenuInterfaceType; + + private const string CreateMenuCallback_Property_Name = "createMenuCallback"; + private const string GenericOsMenu_Type_Name = "UnityEditor.UIElements.GenericOSMenu"; + private const string IGenericMenu_Type_Name = "IGenericMenu"; + + private const string FormatSelectedValueCallback_Property_Name = "formatSelectedValueCallback"; + + #region Generic Menu Assigning + + public static void AssignGenericMenu(this UnityEngine.UIElements.DropdownField dropdownField, + Func genericMenuBuildingFunc) + { + Func genericOSMenuFunc = () => + { + GenericMenu menu = genericMenuBuildingFunc(); + return GetGenericOSMenu(menu); + }; + + object boxedfunc = ConvertFuncToDesiredType(genericOSMenuFunc); + + FieldInfo createMenuCallbackFieldInfo = typeof(UnityEngine.UIElements.DropdownField) + .GetField(CreateMenuCallback_Property_Name, BindingFlags.FlattenHierarchy | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); + + createMenuCallbackFieldInfo.SetValue(dropdownField, boxedfunc); + } + + #region Generic OS Menu Creation + + private static object GetGenericOSMenu(GenericMenu menu) + { + Type type = GetGenericOSMenuType(); + object[] args = new object[] { menu }; + return Activator.CreateInstance(type, args); + } + + #endregion + + #region Type helpers + + private static Type GetGenericOSMenuType() + { + if (genericOSMenuType == null) + { + genericOSMenuType = typeof(UnityEditor.UIElements.ColorField).Assembly.GetType(GenericOsMenu_Type_Name, false, true); + } + + return genericOSMenuType; + } + + private static Type GetIGenericMenuInterfaceType() + { + if (iGenericMenuInterfaceType == null) + { + Type genericOSType = GetGenericOSMenuType(); + iGenericMenuInterfaceType = genericOSType.GetInterface(IGenericMenu_Type_Name); + } + + return iGenericMenuInterfaceType; + } + + #endregion + + #region Func Conversion + + private static Delegate ConvertFuncToDesiredType(Func func) + { + Type interfaceType = GetIGenericMenuInterfaceType(); + Type resultType = typeof(Func<>).MakeGenericType(interfaceType); + + Expression> expressionFunc = FuncToExpression(func); + + InvocationExpression invokedExpression = Expression.Invoke(expressionFunc); + UnaryExpression convertedReturnValue = Expression.Convert(invokedExpression, interfaceType); + + LambdaExpression lambda = Expression.Lambda(delegateType: resultType, body: convertedReturnValue); + + return lambda.Compile(); + } + + private static Expression> FuncToExpression(Func f) + { + return () => f(); + } + + #endregion + + #endregion + + #region Formatting Callback Assigning + + public static void AssignFormattingCallback(this UnityEngine.UIElements.DropdownField dropdownField, + Func formattingCallback) + { +#if UNITY_2022_2_OR_NEWER + dropdownField.formatSelectedValueCallback = formattingCallback; +#else + AssignFormattingCallbackViaReflection(dropdownField, formattingCallback); +#endif + } + + private static void AssignFormattingCallbackViaReflection(this UnityEngine.UIElements.DropdownField functionDropdown, + Func formattingCallback) + { + PropertyInfo propertyInfo = typeof(UnityEngine.UIElements.DropdownField) + .GetProperty(FormatSelectedValueCallback_Property_Name, BindingFlags.FlattenHierarchy | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); + + object[] args = new object[] { formattingCallback }; + + propertyInfo.SetMethod.Invoke(functionDropdown, args); + } + + #endregion + } +} + #endif \ No newline at end of file diff --git a/Editor/Extensions/Dropdown Field/DropdownFieldExtensions.cs.meta b/Editor/Extensions/DropdownField/DropdownFieldExtensions.cs.meta similarity index 100% rename from Editor/Extensions/Dropdown Field/DropdownFieldExtensions.cs.meta rename to Editor/Extensions/DropdownField/DropdownFieldExtensions.cs.meta diff --git a/Editor/Extensions/SerializedProperty/SerializedPropertyExtensions.cs b/Editor/Extensions/SerializedProperty/SerializedPropertyExtensions.cs index 6d5a041..6a90614 100644 --- a/Editor/Extensions/SerializedProperty/SerializedPropertyExtensions.cs +++ b/Editor/Extensions/SerializedProperty/SerializedPropertyExtensions.cs @@ -4,15 +4,14 @@ using System.Collections; using System.Linq; using System.Reflection; using System.Text.RegularExpressions; -using UnityEditor; -namespace UnityUtilities.SerializableDataHelpers.UnityEditorUtilities +namespace SerializableFunc.Editor.Extensions.SerializedProperty { public static class SerializedPropertyExtensions { 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(); #if UNITY_2023_2_OR_NEWER @@ -22,7 +21,7 @@ namespace UnityUtilities.SerializableDataHelpers.UnityEditorUtilities #endif } - public static void SetBoxedValue(this SerializedProperty property, object value) + public static void SetBoxedValue(this UnityEditor.SerializedProperty property, object value) { property.serializedObject.Update(); #if UNITY_2023_2_OR_NEWER @@ -33,7 +32,7 @@ namespace UnityUtilities.SerializableDataHelpers.UnityEditorUtilities property.serializedObject.ApplyModifiedProperties(); } - public static T GetValue(this SerializedProperty property) where T : class + public static T GetValue(this UnityEditor.SerializedProperty property) where T : class { object obj = property.serializedObject.targetObject; string path = property.propertyPath.Replace(".Array.data", string.Empty); @@ -53,7 +52,7 @@ namespace UnityUtilities.SerializableDataHelpers.UnityEditorUtilities return (T)obj; } - public static bool SetValue(this SerializedProperty property, T value) where T : class + public static bool SetValue(this UnityEditor.SerializedProperty property, T value) where T : class { object obj = property.serializedObject.targetObject; string path = property.propertyPath.Replace(".Array.data", ""); diff --git a/Editor/Extensions/Type Extensions.meta b/Editor/Extensions/TypeExtensions.meta similarity index 100% rename from Editor/Extensions/Type Extensions.meta rename to Editor/Extensions/TypeExtensions.meta diff --git a/Editor/Extensions/Type Extensions/TypeExtensions.cs b/Editor/Extensions/TypeExtensions/TypeExtensions.cs similarity index 94% rename from Editor/Extensions/Type Extensions/TypeExtensions.cs rename to Editor/Extensions/TypeExtensions/TypeExtensions.cs index 7db0023..3b461e1 100644 --- a/Editor/Extensions/Type Extensions/TypeExtensions.cs +++ b/Editor/Extensions/TypeExtensions/TypeExtensions.cs @@ -1,60 +1,60 @@ -using System; -using System.Collections.Generic; -using System.Linq; - -namespace Utilities.Extensions.SystemExtensions -{ - public static class TypeExtensions - { - private static Dictionary PrimitiveTypesLookup = new Dictionary - { - { typeof(bool), "Bool" }, - { typeof(byte), "Byte" }, - { typeof(char), "Char" }, - { typeof(decimal), "Decimal" }, - { typeof(double), "Double" }, - { typeof(float), "Float" }, - { typeof(int), "Int" }, - { typeof(long), "Long" }, - { typeof(sbyte), "Sbyte" }, - { typeof(short), "Short" }, - { typeof(string), "String" }, - { typeof(uint), "Uint" }, - { typeof(ulong), "Ulong" }, - { typeof(ushort), "Ushort" }, - }; - - public static string NicifyTypeName(this Type type) - { - if (type.IsArray) - { - Type elementType = type.GetElementType(); - string elementTypeString = NicifyTypeName(elementType); - return $"{elementTypeString}[]"; - } - - if (!type.IsGenericType) - { - return GetSingleTypeName(type); - } - - if (type.IsNested && type.DeclaringType.IsGenericType) return type.Name; - - string genericTypeString = $"{type.Name[..type.Name.IndexOf('`')]}"; - - IEnumerable genericArgumentNames = type.GetGenericArguments() - .Select(x => NicifyTypeName(x)); - - string genericArgumentsString = string.Join(", ", genericArgumentNames); - - return $"{genericTypeString}<{genericArgumentsString}>"; - } - - private static string GetSingleTypeName(Type type) - { - if (PrimitiveTypesLookup.TryGetValue(type, out string result)) return result; - - return type.Name; - } - } +using System; +using System.Collections.Generic; +using System.Linq; + +namespace SerializableFunc.Editor.Extensions.TypeExtensions +{ + public static class TypeExtensions + { + private static Dictionary PrimitiveTypesLookup = new Dictionary + { + { typeof(bool), "Bool" }, + { typeof(byte), "Byte" }, + { typeof(char), "Char" }, + { typeof(decimal), "Decimal" }, + { typeof(double), "Double" }, + { typeof(float), "Float" }, + { typeof(int), "Int" }, + { typeof(long), "Long" }, + { typeof(sbyte), "Sbyte" }, + { typeof(short), "Short" }, + { typeof(string), "String" }, + { typeof(uint), "Uint" }, + { typeof(ulong), "Ulong" }, + { typeof(ushort), "Ushort" }, + }; + + public static string NicifyTypeName(this Type type) + { + if (type.IsArray) + { + Type elementType = type.GetElementType(); + string elementTypeString = NicifyTypeName(elementType); + return $"{elementTypeString}[]"; + } + + if (!type.IsGenericType) + { + return GetSingleTypeName(type); + } + + if (type.IsNested && type.DeclaringType.IsGenericType) return type.Name; + + string genericTypeString = $"{type.Name[..type.Name.IndexOf('`')]}"; + + IEnumerable genericArgumentNames = type.GetGenericArguments() + .Select(x => NicifyTypeName(x)); + + string genericArgumentsString = string.Join(", ", genericArgumentNames); + + return $"{genericTypeString}<{genericArgumentsString}>"; + } + + private static string GetSingleTypeName(Type type) + { + if (PrimitiveTypesLookup.TryGetValue(type, out string result)) return result; + + return type.Name; + } + } } \ No newline at end of file diff --git a/Editor/Extensions/Type Extensions/TypeExtensions.cs.meta b/Editor/Extensions/TypeExtensions/TypeExtensions.cs.meta similarity index 100% rename from Editor/Extensions/Type Extensions/TypeExtensions.cs.meta rename to Editor/Extensions/TypeExtensions/TypeExtensions.cs.meta diff --git a/Editor/PropertyDrawer/SerializableFuncBasePropertyDrawer.cs b/Editor/PropertyDrawer/SerializableFuncBasePropertyDrawer.cs index 535e199..f503113 100644 --- a/Editor/PropertyDrawer/SerializableFuncBasePropertyDrawer.cs +++ b/Editor/PropertyDrawer/SerializableFuncBasePropertyDrawer.cs @@ -5,21 +5,21 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; -using SerializableFunc; +using SerializableFunc.Editor.Extensions.DropdownField; +using SerializableFunc.Editor.Extensions.SerializedProperty; +using SerializableFunc.Editor.Extensions.TypeExtensions; using UnityEditor; using UnityEditor.UIElements; using UnityEditorInternal; using UnityEngine; using UnityEngine.UIElements; -using Utilities.Extensions.SystemExtensions; -using Utilities.Extensions.UIToolkit; using Object = UnityEngine.Object; -namespace UnityUtilities.SerializableDataHelpers.UnityEditorUtilities +namespace SerializableFunc.Editor.PropertyDrawer { [CustomPropertyDrawer(typeof(SerializableAction), 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_Object_Label = "Target Object"; diff --git a/Editor/SerializableFunc.Editor.asmdef b/Editor/SerializableFunc.Editor.asmdef index a18ebd0..6872564 100644 --- a/Editor/SerializableFunc.Editor.asmdef +++ b/Editor/SerializableFunc.Editor.asmdef @@ -1,6 +1,6 @@ { "name": "SerializableFunc.Editor", - "rootNamespace": "", + "rootNamespace": "SerializableFunc", "references": [ "GUID:e72a8e3728c787f4fafc146b590e6be5" ],