Import/Export skeletons in Unity games during runtime using motion capture data saved in the Biovision Hierarchy format. · By
Yes you can! If the imported model has exactly the same skeleton structure and joint rotation as the BVH it should work just fine.
In BVH all joints are always Quaternion.identity in the resting pose, in other words, not rotated. If the model imported by Unity has a skeleton with rotated joints the animation will appear incorrect since BVH base all rotations in the AnimationClip from Quaternion.identity. I tried to make a method that adapted a BVH to an already existing skeleton but it was tricky to make it work without some rotations being flipped the wrong way so I couldn't include it in the final version of the product.
I'm currently working on a different product that skins a mesh from a skeleton during runtime, it'll ensure that the joints have the correct rotation. I'm making good progress but I don't know when it's finished. I recommend that you download the demo of BvhImporterExporter and see if you can achieve what you need. The most important thing is that the model's mesh was skinned to the skeleton when the joints wasn't rotated, then the AnimationClip created from BVH will move the skeleton correctly!
Check your model in Unity (make sure it's in its rest pose - you can uncheck "import animation" for the model in the inspector). If all the GameObjects that make up the skeleton have Transform rotation [0,0,0] you're good to go! Assuming that the skeleton has the same structure as the BVH of course (matching bone names).
1) There might be a way but right now I haven't implemented such a feature. I can see how it would be useful though and will be looking into it if I make an update! It's likely it will be an editor-only feature though and not not available in the demo.
2) Using the Mecanim system is possible but not simple. You have to create an Animator Controller in Unity and set it up with dummy animations, which means no previews (make blank states, set their motion to any AnimationClip you've got in your project). You also prepare an Animator Override Controller (AOC). Then during runtime you replace the original dummy clips in the AOC with the real ones created using BvhImporterExporter ("bvh.makeAnimationClip(pathToSkeletonGO:"MySkeletonGOName", legacy:false);"). Finally you instruct the Animator to use the override ("myAnimator.runtimeAnimatorController = AOC;").
Sticking with legacy might be just as simple or even simpler unless you intend to do some advanced stuff. The legacy Animation system is not deprecated even though Mecanim has been around since 2012 when Unity 4.0 was released. After trying to work with Mecanim from code I understand why, the Animation system has better access from code so Mecanim isn't a replacement (yet at least). I haven't for example found a way to create an Animator Controller from scratch via code during runtime, it seems to be an impossible task outside the editor.
Did a little test just now to make sure it works. Here's the code I used:
BVH bvh = new BVH("dance.bvh");
BVH.animateSkeleton(GameObject.Find("MySkeletonGOName"), bvh.makeAnimationClip());
1) I used MakeHuman ( http://www.makehuman.org/ ) to make a model. I exported in mesh format "Collada (dae)" and made sure to select the Bone Orientation Option "Local = Global" (important/required since it makes sure no joints are rotated).
2) I imported the .dae file into Blender ( https://www.blender.org/ ) and made a little animation. I exported it to format "Motion Capture (.bvh)" and checked the "Root Transform Only" option (it isn't required but I really recommend it since it makes the BVH file smaller by removing positional data usually not used).
3) Made a new scene in Unity, it imported the .dae file automatically. I dragged it into the scene, opened up the model GameObject in the Hierarchy and renamed the skeleton GameObject to "MySkeletonGOName" (just so I can use GameObject.Find()). The skeleton is the one that only contains a bunch of transforms.
4) Made a new MonoBehaviour script and put my two lines of code in its Start() method. Could have been one line of code btw, the bvh variable isn't really needed in this case.
5) Pressed play and the model made by MakeHuman danced via the BVH animation made in Blender. It works!
Blender Tip: If you go "Export > Collada (Default) (.dae)" you need to check "Export for OpenSim" under Armature Options. It removes bone rotations so that the skeleton joints become compatible with BVH. So you can create models from scratch or modify the mesh of existing ones if you want to.