Hi everyone,
I’m trying to create my own solution to the nested prefabs problem and I’ve hit a snag. First some info:
I have a GameObject that represents a block in a level, the block has two components - a skin, which is the visual look for the block and comprises a mesh filter and renderer, and a collection of behaviours that define how the block acts in the level. Visually the block looks like this:
Block (GameObject)
Skin (Prefab)
MeshRenderer
MeshFilter
Behaviours (Prefab)
RigidBody
Collider
BlockScript
Now immediately you’ll realise that the rigidbody will not work properly when it is parented to a child instead of the base game object. However the rigidbody is part of the block’s behaviour definition - if I were to change the mass of that rigidbody, then the mass for ALL blocks using that particular block behaviour would also have to change. This isn’t possible without the use of nested prefabs, a problem that I’ve almost solved.
The Behaviours GameObject includes a script that clones all the behaviours attached to it onto the parent object at runtime. This means that the block script, collider and rigidbody would all appear on the parent object in game and function correctly. This is easy to do with my own scripts, as I can implement a clone function. However on unity’s base behaviours like rigidbody and collider things get tricky, and this is where I’ve hit the snag I mentioned earlier. Simply put, there doesn’t seem to be an easy way for me to clone a built in unity behaviour. This is especially difficult with colliders, as they appear to be the same script when using the IDE, but are actually different classes depending on the collider type. So for example, the way I clone a rigidbody (shown below) would need to be repeated for every collider type, which seems extremely inefficient:
public static void cloneRigidBody( GameObject source, GameObject destination )
{
Rigidbody behaviour = destination.AddComponent();
behaviour.mass = source.rigidbody.mass;
behaviour.drag = source.rigidbody.drag;
behaviour.angularDrag = source.rigidbody.angularDrag;
behaviour.useGravity = source.rigidbody.useGravity;
behaviour.isKinematic = source.rigidbody.isKinematic;
behaviour.interpolation = source.rigidbody.interpolation;
behaviour.collisionDetectionMode = source.rigidbody.collisionDetectionMode;
behaviour.constraints = source.rigidbody.constraints;
}
Initially I looked at using Instantiate to clone my scripts, but upon closer inspection of the documentation, I realised that it would also clone the entire object hierarchy as well which is useless for my needs.
Is there an easy way to clone a script given its type? Could I somehow iterate through every public member on one object and copy them to another? For example, this problem is trivial in Actionscript with a closed source class:
//assuming that data types copied are not passed by reference
for (var public_member:* in source)
{
destination[public_member] = source[public_member];
}
Is there a C# or built in Unity equivalent? It seems criminal that Unity behaviours have no clone function to extend, so perhaps I’ve missed something?