This repository has been archived by the owner on Jul 1, 2024. It is now read-only.
Implement support for Unity (de)serializing fields of injected MonoBehaviors #53
Labels
class-injection
For things concerning class injection/delegate conversion
enhancement
New feature or request
Right now injected classes have no (extra) fields. This is largely due to the fact that field storage is allocated in the managed-side object, with the il2cpp side object only holding a gchandle to the managed one.
This presents an issue with cloning gameobjects with components of injected types, and loading them from assetbundles - Unity can't access managed fields, and, as such, they are not copied during instantiation/bundle load.
Preliminary research indicates that Unity uses field offsets and (optionally)
il2cpp_gc_wbarrier_set_field
to directly access fields on il2cpp objects - this means that the usual approach of "hook set_field so it redirects to the managed field" is likely non-viable.This means that an alternative approach for handling fields on injected types is necessary. I have a few approaches that I'd consider viable:
RedirectToNativeField
. When performing class injection, a field in il2cpp class would be created for every property marked with that attribute, and its getter and setter would be patched to access the field in native object.SerializeField
andFormerlySerializedAsAttribute
attributes can be applied to auto-property backing fields to make them serialized and have the appropriate name. Alternatively, injected fields can be made to match property backing field name.{ get; set; }
block is relatively easy.NativeFieldWrapper<T>
. Fields of this type can be declared in the injected class, and class injector would generate a matching field in the il2cpp class for every field of that type. The wrapper would provide access to those il2cpp fields.ISerializationCallbackReceiver
to manually serialize/deserialize state to a single hardcoded string/byte[] field. This would likely work fairly well with editor interop, but might not be compatible with older unity versions if they're missing this interface. Additionally, this would require interface implementation to be supported by class injection.A thing to consider for injected fields would be GC integration - IL2CPP can use either boehm (which requires no special GC magic), or something sgen-like which would likely require correct GC descriptors on classes to tell it where reference-typed fields are located in the object.
The text was updated successfully, but these errors were encountered: