|
Since the editor does not have a way to copy the child components of a game object and apply that copy to another game object, I am trying to make a Editor Wizard that will do this. My wizard is able to copy the components presently, but cannot copy the values of the source. Any ideas? Here's what I have so far (below). The commented-out code doesn't work (so, for example, I know that CloneComponent will just make clones in the source - makes a mess so don't use - it will duplicate your transform components, for example - not good!). I left the comments in so folks realize that I've tried the obvious code from the reference and so we don't "reinvent the broken wheel" so to speak.
} Thanks in advance!
(comments are locked)
|
|
Use reflection. This works for about 90% of unity objects, but there's some weird cases that'll bite you (like ParticleEmitters).
Mar 11 '10 at 06:33 PM
Mortoc
Thank you! I added using System.Reflection; and modified your code above to fit my current variables. Works great!!
Mar 23 '10 at 03:00 AM
mcroswell
Motorc: Thanks for the heads=up. I will probably try to put in special-case code for ParticleEmitters. Currently my project doesn't use them, but I am planning on making this general usage and sharing.
Mar 23 '10 at 05:36 AM
mcroswell
While I definitely wish you the best, I hope even more that unity gets this basic feature out-of-the-box eventually.
Jun 19 '10 at 09:15 PM
Waz
I suspect the "weird cases" actually have C# attribute markup on the fields (and properties?) that you need to consider. Have a look at FieldInfo.GetCustomAttributes (and PropertyInfo.GetCustomAttributes) and see how the Unity devs have marked up those weird cases.
Jan 09 '11 at 07:11 AM
yoyo
(comments are locked)
|
|
To add to that Reflection suggestion, if you are using private serialized variables, like me, you may want something more like this... You wrote NonPublic twice. I think you meant "BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic"
Feb 08 '12 at 08:55 AM
piojo
(comments are locked)
|
|
There's also a general solution for this problem: http://unitydevs.com/item/copypaste-component/ @Ranza: Useful. But again, notice it has some serious limitations: no serializable components, no support for Light, ParticleEmitters (probably others). By the way, I was contacted by another scripter/programmer who is working on his own version. Still, mine had bugs and I am hoping (as Warwick Allison above) that it will be an "out-of-the-box" feature soon! For what it's worth: If you use other's scripts, particularly when copying/pasting components I highly recommend you backup your scenes very frequently (not a bad idea when using Unity anyhow).
Sep 07 '10 at 10:42 PM
mcroswell
(comments are locked)
|

Can you please post your full working script?