-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix: Instantiate() behavior for Prefab and Entity references #2914
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ddba122
fix: Instantiate() behavior for Prefab and Entity references
Eideren 8cf1b65
Update EntityCloner.Instantiate docs
Eideren 1d250a8
simplify null checkand assign with `??=`
Eideren ab42649
Document commented out test
Eideren 000eb75
Address latest review
Eideren File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,10 +29,13 @@ namespace Stride.Engine.Design | |
| [DataSerializerGlobal(typeof(CloneSerializer<OfflineRasterizedSpriteFont>), Profile = "Clone")] | ||
| [DataSerializerGlobal(typeof(CloneSerializer<RuntimeRasterizedSpriteFont>), Profile = "Clone")] | ||
| [DataSerializerGlobal(typeof(CloneSerializer<SignedDistanceFieldSpriteFont>), Profile = "Clone")] | ||
| [DataSerializerGlobal(typeof(CloneSerializer<Prefab>), Profile = "Clone")] | ||
| [DataSerializerGlobal(typeof(CloneSerializer<Entity>), Profile = "Clone")] | ||
| public class EntityCloner | ||
| { | ||
| private static readonly CloneContext cloneContext = new CloneContext(); | ||
| private static SerializerSelector cloneSerializerSelector = null; | ||
| private static SerializerSelector entitySerializerSelector = null; | ||
| public static readonly PropertyKey<CloneContext> CloneContextProperty = new PropertyKey<CloneContext>("CloneContext", typeof(EntityCloner)); | ||
|
|
||
| // CloneObject TLS used to clone entities, so that we don't create one everytime we clone | ||
|
|
@@ -45,13 +48,13 @@ private static HashSet<object> ClonedObjects() | |
| } | ||
|
|
||
| /// <summary> | ||
| /// Clones the specified prefab. | ||
| /// <see cref="Entity"/>, children <see cref="Entity"/> and their <see cref="EntityComponent"/> will be cloned. | ||
| /// Instantiates the content of the prefab provided. | ||
| /// <see cref="Prefab.Entities"/>, children <see cref="Entity"/> and their <see cref="EntityComponent"/> will be cloned. | ||
| /// Other assets will be shared. | ||
| /// </summary> | ||
| /// <param name="prefab">The prefab to clone.</param> | ||
| /// <returns>A cloned prefab</returns> | ||
| public static Prefab Clone(Prefab prefab) | ||
| /// <param name="prefab">The prefab to instantiate.</param> | ||
| /// <returns>A clone of this prefab's <see cref="Prefab.Entities"/></returns> | ||
| public static List<Entity> Instantiate(Prefab prefab) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe update the documentation since the method name has changed. |
||
| { | ||
| if (prefab == null) throw new ArgumentNullException(nameof(prefab)); | ||
| var clonedObjects = ClonedObjects(); | ||
|
|
@@ -61,7 +64,8 @@ public static Prefab Clone(Prefab prefab) | |
| { | ||
| CollectEntityTreeHelper(entity, clonedObjects); | ||
| } | ||
| return Clone(clonedObjects, null, prefab); | ||
|
|
||
| return Clone(clonedObjects, null, prefab.Entities); | ||
| } | ||
| finally | ||
| { | ||
|
|
@@ -127,17 +131,16 @@ internal static void CollectEntityTreeHelper(Entity entity, HashSet<object> enti | |
| /// <returns>The cloned object.</returns> | ||
| private static T Clone<T>(HashSet<object> clonedObjects, TryGetValueFunction<object, object> mappedObjects, T entity) where T : class | ||
| { | ||
| if (cloneSerializerSelector == null) | ||
| { | ||
| cloneSerializerSelector = new SerializerSelector(true, false, "Default", "Clone"); | ||
| } | ||
| cloneSerializerSelector ??= new SerializerSelector(true, false, "Default", "Clone"); | ||
|
|
||
| entitySerializerSelector ??= new SerializerSelector(true, false, "Default"); | ||
|
|
||
| // Initialize CloneContext | ||
| lock (cloneContext) | ||
| { | ||
| try | ||
| { | ||
| cloneContext.EntitySerializerSelector = cloneSerializerSelector; | ||
| cloneContext.EntitySerializerSelector = entitySerializerSelector; | ||
|
|
||
| cloneContext.ClonedObjects = clonedObjects; | ||
| cloneContext.MappedObjects = mappedObjects; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a comment around the commented out code to explain why it is kept around (maybe reference the issue/PR id as well).