Rename editor namespaces.

This commit is contained in:
2025-11-05 23:57:44 +01:00
parent a4ec9f4801
commit 60544312be
9 changed files with 198 additions and 200 deletions

View File

@@ -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<GenericMenu> genericMenuBuildingFunc)
{
Func<object> 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<object> func)
{
Type interfaceType = GetIGenericMenuInterfaceType();
Type resultType = typeof(Func<>).MakeGenericType(interfaceType);
Expression<Func<object>> 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<Func<T>> FuncToExpression<T>(Func<T> f)
{
return () => f();
}
#endregion
#endregion
#region Formatting Callback Assigning
public static void AssignFormattingCallback(this DropdownField dropdownField,
Func<string, string> formattingCallback)
{
#if UNITY_2022_2_OR_NEWER
dropdownField.formatSelectedValueCallback = formattingCallback;
#else
AssignFormattingCallbackViaReflection(dropdownField, formattingCallback);
#endif
}
private static void AssignFormattingCallbackViaReflection(this DropdownField functionDropdown,
Func<string, string> 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<GenericMenu> genericMenuBuildingFunc)
{
Func<object> 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<object> func)
{
Type interfaceType = GetIGenericMenuInterfaceType();
Type resultType = typeof(Func<>).MakeGenericType(interfaceType);
Expression<Func<object>> 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<Func<T>> FuncToExpression<T>(Func<T> f)
{
return () => f();
}
#endregion
#endregion
#region Formatting Callback Assigning
public static void AssignFormattingCallback(this UnityEngine.UIElements.DropdownField dropdownField,
Func<string, string> 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<string, string> 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

View File

@@ -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<T>(this SerializedProperty property) where T : class
public static T GetValue<T>(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<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;
string path = property.propertyPath.Replace(".Array.data", "");

View File

@@ -1,60 +1,60 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace Utilities.Extensions.SystemExtensions
{
public static class TypeExtensions
{
private static Dictionary<Type, string> PrimitiveTypesLookup = new Dictionary<Type, string>
{
{ 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<string> 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<Type, string> PrimitiveTypesLookup = new Dictionary<Type, string>
{
{ 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<string> 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;
}
}
}