I’m quite familiar with c#'s serialization mechanism and I’m looking for information on how to properly serialize/deserialize a skinned mesh. I looked at the MeshSerializer2 class but that doesn’t seem to work on skinned meshes. I presume that saving/restoring bone data will work, but I’m not sure what’s involved. Does anyone have experience in this area?
Do you need to serialize a mesh at runtime? If not, then using AssetDatabase.CreateAsset to write the mesh to disk is by far the most efficient way to do it, since it works directly on the internal representation of mesh data and does not need to go through the C# properties.
,Hi, thanks for your answer. I know how to use AssetDatabase.CreateAsset, and I wanted to use AssetBundles, but I don’t have the choice, my client don’t want to use Unity Pro. In fact I have to code some kind of framework that can read 3D files and display them. So I wrote a custom editor script that serialize a scene into and xml, and serialize all the meshes and animations into binary files.
In fact, today I managed to serialize skinned mesh by serializing boneWeights and bindposes informations (by adding some functions in the original MeshSerializer2 class), so I didn’t have this problem anymore Thanks anyway, I know it’s a bit stupid and it will be more efficient to use Unity’s asset management, but like I said, I don’t have the choice. My client have to understand that the better way to keep graphics optimized in his application is to build directly into Unity, and not by parsing some xml files… But it’s not the subject of this topic !
(sorry for my poor english, I’m french…)
Hi, thanks you for your answer.
In fact I have to load serialized meshes and animations for several platforms (iPad, iOs, Web Player…) and I know it will be better to use AssetDatabase.CreateAsset and AssetBundles, but I don’t have the choice, my client doesn’t want to use Unity Pro.
Today I managed to serialize skinned mesh, so I don’t have this problem anymore I use a csharp version of MeshSerializer2 and I add some functions to serialize boneWeights and bindposes info. I paste an exemple below for those who are interested.
static void ReadBoneWeightArrayBytes (BoneWeight [] arr , BinaryReader buf )
{
int n = arr.Length;
for (int i = 0; i < n; ++i)
{
float weight0 = buf.ReadSingle();
float weight1 = buf.ReadSingle();
float weight2 = buf.ReadSingle();
float weight3 = buf.ReadSingle();
System.UInt16 boneIndex0 = buf.ReadUInt16();
System.UInt16 boneIndex1 = buf.ReadUInt16();
System.UInt16 boneIndex2 = buf.ReadUInt16();
System.UInt16 boneIndex3 = buf.ReadUInt16();
arr*.weight0 = weight0;*
_ arr*.weight1 = weight1;_
_ arr.weight2 = weight2;
arr.weight3 = weight3;
arr.boneIndex0 = (int)boneIndex0;
arr.boneIndex1 = (int)boneIndex1;
arr.boneIndex2 = (int)boneIndex2;
arr.boneIndex3 = (int)boneIndex3;
}
}
static void WriteBoneWeightArrayBytes (BoneWeight [] arr , BinaryWriter buf )
{
foreach (BoneWeight bone in arr)
{
float weight0 = bone.weight0;
float weight1 = bone.weight1;
float weight2 = bone.weight2;
float weight3 = bone.weight3;*
System.UInt16 boneIndex0 = (System.UInt16)bone.boneIndex0;
System.UInt16 boneIndex1 = (System.UInt16)bone.boneIndex1;
System.UInt16 boneIndex2 = (System.UInt16)bone.boneIndex2;
System.UInt16 boneIndex3 = (System.UInt16)bone.boneIndex3;_
buf.Write (weight0);
buf.Write (weight1);
buf.Write (weight2);
* buf.Write (weight3);*
* buf.Write (boneIndex0);*
buf.Write (boneIndex1);
buf.Write (boneIndex2);
* buf.Write (boneIndex3);*
}
}
(sorry for my poor english, I’m french…)
Unity Serializer will serialize meshes as part of a level serialisation or for an individual object. It will also save materials.