- Home /
What happens when i Copy an object (From Programming point of view)
Hello.
Pure curiosity... I wonder, what actually happens, when i go to Edit > Copy, while selecting a gameObject (In the Hierarchy), under the hood?
I see that all of the referencing, that the Components of the copied object are kept, even if i delete it and then paste it in the scene....
•Does Unity creates something like a virtual instance of that object?
• Does Unity goes through all of the properties of all components and records their state, at the moment of copy?
1: Can i replicate the copy via C# in unity (Without doing any DLL stuff)?
I was wondering if i can do it for many objects, for example i have a button and select object (One by one, NOT all, at the same time) and add them to (so to speak) copy List... So i can choose, which one to paste afterwards...
Thanks :)
I was wondering the same thing. I copied a gameobject that has animation clips associated with it. To my surprise the movieclips still work even with the copied gameobject!
Answer by Bunny83 · Dec 07, 2015 at 01:51 AM
Well, we can't tell how the editor actually performs a copy and paste action, however it most likely (99%) uses Unity's serialization system to serialize the selected objects when you copy them and deserialize them when pasting. At least the ISerializationCallbackReceiver callbacks are called whenever you copy / paste objects in the hierarchy.
I'm not sure what you actually want to do. Unity's serialization system only works inside the editor. You can't save things at runtime. If you want to copy multiple objects in the editor you can simply hold the CTRL key and select as many objects as you want. When you press CTRL+C you will copy all objects you have selected.
If you want to automate things inside the Unity editor you can use the Selection class in an editor script to select any object inside the editor. With EditorApplication.ExecuteMenuItem you can trigger "Edit/Copy" and "Edit/Paste".
If you want such functionality at runtime you have to do everything yourself. At runtime Unity's serialization system is not available.
As i said in the beginning it's not clear what and where / when you want copy "something". If my answer isn't enough you should edit your question and be more clear about what you want to do.
Hi @Bunny83 thanks for the answer :)
1; I was just curious, there's nothing specific, that i want to do.
If unity uses the Serialization system, then i guess it won't be possible to copy gameObject or Component at runtime and paste it after you exit runtime, but that works, even if some changes are made, while in Runtime. So i guess it doesn't use the Serialization, since you say, that, it does Not work in Runtime.
I was just thinking of some way to keep changes in the components, done in Runtime...
No, you get that wrong. By runtime i talk about the Unity engine itself. The copy and paste feature as well as the AssetDatabase and serialization system are part of the Unity editor application. If you run your game inside the editor you still have all editor features available. Once you create a build you only have the Unity engine available.
Why would you want to keep changes done at runtime? The runtime is when you're executing your game. It seems that you mix game functionality with editor functionality. You can write editor scripts to perform any special actions at edit time. What you can do is creating prefabs of objects at runtime so they are saved, even when you stop playmode. However You might loose certain references to objects which aren't saved as asset. This is not the intended use of those mechanics. The playmode exists to test your game, not to edit something.
With editor scripts you have way more flexibility compared to what you can do at runtime. With editor scripts you can actually create "real" prefab instances via code which isn't possible in game code at all.
Since you don't have a concrete usecase we can't help you any further...
There are allways some cases, where you need to adjust something, while seeing it (Speed of movement \ rotation, etc)... And by now, you have to remember the value of component, that is adjusted in Playmode and then re-add it, when exit playmode... This is why i was asking and wondering if i can keep runtime changes. I have allready tried with Prefabs, but (as you mentioned) the references to other scene objects is lost, so this doesn't work at all...
thanks for the explanation, on the Serialized Objects...
Your answer
Follow this Question
Related Questions
Find a Component/GameObject Using an Interface Reference 2 Answers
Disabling A Script on a GameObject From a Different Script 2 Answers
Distribute terrain in zones 3 Answers
Add component to a gameobject created through code. 1 Answer
Is it OK to use public accessers for unique gameobjects (C#) ? 3 Answers