Closed jeffcampbellmakesgames closed 4 years ago
Accomplished much of the implementation for a more nuanced copy logic for component members based on whether they are capable of supporting deep copy logic or not, looking to update/create unit tests and update documentation.
This has been merged to develop.
Is your feature request related to a problem? Please describe. Currently the entity API
CopyComponentTo
performs a shallow copy of a component where a template instance's members's vales are assigned to a new component instance's members. This results in reference types being assigned directly, thus maintaining the reference instance of the same member between two different component instances. This includes members that are collections where the instance of the collection is assigned to both component instances. There is not currently an option that enables for deep copying a reference type member and/or copying the contents of a list to a new component collection member rather than assigning the collection itself.Describe the solution you'd like Ideally, the outcome of a reference type member on a component being copied should be:
MonoBehaviour
orScriptableObject
(really anything fromUnityEngine.Object
) the intent is more likely to assign by ref since they usually don't have a default constructor or straightforward way to copy over all the instance data.ICloneable
, rather than assign the ref value directly call theClone
method on assignment to create a new separate instance of that ref type. By implementingICloneable
on a type used as a member on a component, in effect that will result inCopyComponentTo
deep-copying the ref instance to a new one.IList
,IReadOnlyList
, iterate over the list item in a for loop and assign/copy each member using these same default/ICloneable
, collection rules.IList
,IReadOnlyList
, iterate over the collection in a foreach loop and assign/copy each member using these same default/ICloneable
, collection rules.Describe alternatives you've considered I'd like to avoid heavy usage of runtime reflection as much as possible to reduce down on the performance hit this operation should take (especially since it has the capacity to be used in bulk for many entities from blueprints). Any reflection that takes place should either be small or cached for reuse.
Tasks
ICloneable
on several example ref type componentsICloneable
code-generation logic forCopyComponentTo
method code-generatorList
code-generation logic forCopyComponentTo
method code-generatorArray
code-generation logic forCopyComponentTo
method code-generatorDictionary
code-generation logic forCopyComponentTo
method code-generator