Merge pull request #88 from moz-moz/fix/unity-2023-compatibility

Fix for InternalAPIHelper Error in Unity 2023.2.15f1 and Later
This commit is contained in:
Annulus Games 2024-05-01 18:57:15 +09:00 committed by GitHub
commit 5fef637954
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 22 additions and 4 deletions

View File

@ -32,8 +32,9 @@ namespace Alchemy.Editor.Elements
break;
case SerializedPropertyType.Generic:
var targetType = property.GetPropertyType(isArrayElement);
var isManagedReferenceProperty = property.propertyType == SerializedPropertyType.ManagedReference;
if (InternalAPIHelper.GetDrawerTypeForType(targetType) != null)
if (InternalAPIHelper.GetDrawerTypeForType(targetType, isManagedReferenceProperty) != null)
{
element = new PropertyField(property);
}

View File

@ -118,10 +118,11 @@ namespace Alchemy.Editor
VisualElement element = null;
var property = findPropertyFunc(member.Name);
var isManagedReferenceProperty = property?.propertyType == SerializedPropertyType.ManagedReference;
// Add default PropertyField if the property has a custom PropertyDrawer
if ((member is FieldInfo fieldInfo && InternalAPIHelper.GetDrawerTypeForType(fieldInfo.FieldType) != null) ||
(member is PropertyInfo propertyInfo && InternalAPIHelper.GetDrawerTypeForType(propertyInfo.PropertyType) != null))
if ((member is FieldInfo fieldInfo && InternalAPIHelper.GetDrawerTypeForType(fieldInfo.FieldType, isManagedReferenceProperty) != null) ||
(member is PropertyInfo propertyInfo && InternalAPIHelper.GetDrawerTypeForType(propertyInfo.PropertyType, isManagedReferenceProperty) != null))
{
if (property != null)
{

View File

@ -18,7 +18,7 @@ namespace Alchemy.Editor
const string Name_ScriptAttributeUtility = "UnityEditor.ScriptAttributeUtility";
public static Type GetDrawerTypeForType(Type classType)
public static Type GetDrawerTypeForType(Type classType, bool isManagedReferenceProperty)
{
var instance = EditorAssembly.CreateInstance(Name_ScriptAttributeUtility);
var utilityType = instance.GetType();
@ -26,7 +26,23 @@ namespace Alchemy.Editor
var bindingFlags = BindingFlags.NonPublic | BindingFlags.Static;
var methodInfo = utilityType.GetMethod(nameof(GetDrawerTypeForType), bindingFlags);
#if UNITY_2023_3_OR_NEWER
return (Type)methodInfo.Invoke(instance, new object[] { classType, null, isManagedReferenceProperty });
#elif UNITY_2023_2_OR_NEWER
// Unity 2023.2.15f1 added a new parameter to the method
var version = UnityEditorInternal.InternalEditorUtility.GetUnityVersion();
if (version.Build >= 15)
{
return (Type)methodInfo.Invoke(instance, new object[] { classType, isManagedReferenceProperty });
}
else
{
return (Type)methodInfo.Invoke(instance, new object[] { classType });
}
#else
_ = isManagedReferenceProperty; // discard
return (Type)methodInfo.Invoke(instance, new object[] { classType });
#endif
}
const string Name_M_Clickable = "m_Clickable";