@@ -132,7 +132,6 @@ internal override Type Internal_GetActualType(object obj)
132
132
return null ;
133
133
134
134
var type = obj . GetType ( ) ;
135
-
136
135
try
137
136
{
138
137
if ( IsString ( obj ) )
@@ -216,7 +215,7 @@ internal override object Internal_TryCast(object obj, Type castTo)
216
215
// from other structs to il2cpp object
217
216
else if ( typeof ( Il2CppSystem . Object ) . IsAssignableFrom ( castTo ) )
218
217
{
219
- return BoxIl2CppObject ( obj ) ;
218
+ return BoxIl2CppObject ( obj ) . TryCast ( castTo ) ;
220
219
}
221
220
else
222
221
return obj ;
@@ -295,7 +294,27 @@ public object UnboxCppObject(Il2CppObjectBase cppObj, Type toType)
295
294
try
296
295
{
297
296
if ( toType . IsEnum )
297
+ {
298
+ // Check for nullable enums
299
+ var type = cppObj . GetType ( ) ;
300
+ if ( type . IsGenericType && type . GetGenericTypeDefinition ( ) == typeof ( Il2CppSystem . Nullable < > ) )
301
+ {
302
+ var nullable = cppObj . TryCast ( type ) ;
303
+ var nullableHasValueProperty = type . GetProperty ( "HasValue" ) ;
304
+ if ( ( bool ) nullableHasValueProperty . GetValue ( nullable , null ) )
305
+ {
306
+ // nullable has a value.
307
+ var nullableValueProperty = type . GetProperty ( "Value" ) ;
308
+ return Enum . Parse ( toType , nullableValueProperty . GetValue ( nullable , null ) . ToString ( ) ) ;
309
+ }
310
+ // nullable and no current value.
311
+ return cppObj ;
312
+ }
313
+
298
314
return Enum . Parse ( toType , cppObj . ToString ( ) ) ;
315
+ }
316
+
317
+ // Not enum, unbox with Il2CppObjectBase.Unbox
299
318
300
319
var name = toType . AssemblyQualifiedName ;
301
320
0 commit comments