Skip to content

Commit

Permalink
Fix: Resolved InternalAPIHelper error in Unity 2023.2.15f1 and later …
Browse files Browse the repository at this point in the history
…versions
  • Loading branch information
moz-moz committed Apr 30, 2024
1 parent 93a5675 commit 2c1f670
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ public AlchemyPropertyField(SerializedProperty property, Type type, bool isArray
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);
}
Expand Down
5 changes: 3 additions & 2 deletions Alchemy/Assets/Alchemy/Editor/Internal/InspectorHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,11 @@ public static void BuildElements(SerializedObject serializedObject, VisualElemen

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)
{
Expand Down
18 changes: 17 additions & 1 deletion Alchemy/Assets/Alchemy/Editor/Internal/InternalAPIHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,31 @@ internal static class InternalAPIHelper

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();

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";
Expand Down

0 comments on commit 2c1f670

Please sign in to comment.