Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by PHLCollective · Jan 08 at 10:24 PM · serializationserializedobjectnodes

Duplicating a [SerializeReference] property?

I'm building an editor tool that involves a graph with nodes, and a custom Editor. I'm utilizing the [SerializeReference] attribute for the nodes, which has worked really well thus far, until I wanted to duplicate a node. That is, create a new node with all the same values as the previous node. Here are some things I've tried:

- Duplicating the element in the list of nodes. This simply creates another reference to the same node object.

- Using BinaryFormatter to create a deep copy of the node, and assigning that to the new node. This doesn't seem to work well with some built-in Unity classes like Vector2, and threw some errors.

- Creating a shallow copy with MemberwiseClone(). This was the closest I've gotten, however reference types, like Lists, were not cloned, and would reference the same List. So if I changed the list on one node, it would change on the other as well.

One thing to note is that I cannot manually go through each value in the node and assign them on the new node, because there are many derived versions of the nodes, and I do not want to implement a sort of .Clone() function on each child class.

Any thoughts?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by minhluongneo · Jan 09 at 05:36 PM

First way: use reflection to iterate over all public or private variables that have attribute [SerializeField] (Remember to ignore the public variable with attribute [NonSerialized]) Because when using [SerializeReference] you can get exactly the derived type of node, instead of the base type. But be careful, because reflection also causes some performance problems if used in build.


Second way: Use a ScriptableObject as a clone machine

 //NodeCloner is custom your class
     NodeCloner nodeCloner = ScriptableObject.CreateInstance<NodeCloner>();//You can cache it
     nodeCloner.nodeData = yourNodeData;
     
     NodeCloner newNodeCloner = ScriptableObject.Instantiate(nodeCloner);
     var newNodeData = newNodeCloner.nodeData;

However, as for performance in the build vs. reflection, I haven't tested it yet.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PHLCollective · Jan 10 at 12:03 AM 0
Share

Performance isn't an issue, as this is an editor tool, but thanks for looking out anyway!

The ScriptableObject cloner worked brilliantly, thanks for this really great idea!! The note I'll leave for anyone else looking to solve this issue, is to make sure the nodeData field is also marked as [SerializeReference], otherwise the new copied node will only be of the base node class.
Cheers!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

180 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Serialization Placeholder Value for Different Fields? 1 Answer

Question on SerializeField? 1 Answer

Loading a SerializedObject directly from disk 0 Answers

How to test an objects Unity serializability? 0 Answers

[CLOSED]Problem with serialization after PUN update 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges