- Home /
Replace a Skinned Mesh in a character with a new Skinned Mesh
To help illustrate my problem, I have the following PDF that I created: 1
Basically, CharacterPrefab is based off of CharacterFBX and is what is in the scene of the game. ReplacementPrefab is based off of ReplacementFBX. I want to take MeshObj1 of the character, and replace it with the MeshObj of the replacement, as I do not need to duplicate the skeleton already on the character. I need to know the best way to do so.
I tried to use Resources.Load() to get a GameObject of the FBX with it's hierarchy, and then using Find() on it's transform to get just the mesh and then using it's gameObject property with Instantiate() to create a new copy. I then tried to set it's parent transform to the transform of the character, but that seemed to do nothing, the duplicate just floats where it was copied, and the copy does not get placed with the character even when I use transform.position and transform.rotation in the Instantiate() call.
I also tried earlier to not bother with trying to find the mesh's object and just set the parent transform of the base GameObject to the character's transform. This made it follow the character, but it was not placed correctly (just a little higher than it should've been) and ignored the skeleton when moving. Looking in the hierarchy during gameplay shows that the full object was placed on the character, which is why I think it's not following the character's bones.
If there is some other way I should be going about this, I would like to know. What I really need is an easy and/or sure-fire way to swap out a single mesh out of X number of meshes from a character. I can't place a list of GameObject transforms on a script as we have no idea how many replacement meshes we will eventually have.
So in the end, my question would be this: What is the best (and probably easiest) way to replace a single mesh in a character that has multiple meshes?
Just wanted to give you some kudos on a very well detailed question. Cheers!
Answer by Ony · Mar 14, 2010 at 11:38 PM
Try something like this:
Assuming your replacement mesh is in the Resources folder...
theMainMesh = GameObject.FindWithTag("sometag");
var Swapper : SkinnedMeshRenderer = theMainMesh.GetComponent(SkinnedMeshRenderer);
SwapMesh = Resources.Load("the_Replacement_Mesh_filename", Mesh); Swapper.sharedMesh = SwapMesh;
I did try this, and what happens is that the replaced mesh looks like the original but with pieces missing. Even trying to store a copy of the original mesh back into the shared$$anonymous$$esh didn't help.
you should take care of the shared mesh and the material/materials actually
I'm also experiencing the same problem. I tried various methods, I have asked around the net for help, but this seems to be a problem that many others have had, but nobody knows what to do. I'm only writing here to "bump" the question and to also get some information from somebody who knows this.
Also, I want to add that yes, you can move the verts in the mesh via the update method, but you can NOT give the shared mesh a new mesh to render, even if it will work as a normal mesh. It will have the same problems as the OP has listed. Also, giving a mesh that has more verts than the original will crash unity.
Answer by MichiealO · Sep 21, 2013 at 06:34 PM
Well, one of the Ideas that I had, was to create added objects (clothing etc.,) and just add them to the character. That makes it to where one doesn't need to swap out the mesh, if the main mesh is the basis of all the others. Then, I changed out the material on the mesh... This seemed to work well for the primary material, but things start to go a bit hinky when doing secondary materials. My main character has two materials in its mesh, and I can change the primary just fine, add in any number of clothing/props/etc., but if I swap the secondary material out, it fails, and swaps the primary instead. The last resort option that I have is to just swap the player out with another prefab, that has been set up with the secondary material preset, and just use the material swap and prop swap to do the rest. This might work well, for answering this question, as the prefabs can be instantiated in code, and as long as you find the player (usually named something like "Player(Clone)" if you've instantiated a prefab named Player at runtime). those are the options & ideas that I came up with. Hope that it helps.