Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Build Animation from scratch! AssetPostrocessor.

Discussion in 'Animation' started by luskos, Mar 22, 2019.

  1. luskos

    luskos

    Joined:
    Mar 4, 2013
    Posts:
    48
    I have custom animation format that i am able to read and write, but i don't know how to build Animation Clip file from the data. I don't know what is the structure of Unity's .anim file. Somewhere in the forums it was stated that it's readable by text editors, and part of it is plain text while most of it is Binary, the thread is old so things could of changed. How i can find the file structure of this format?

    I am trying to import assets and convert them with AssetPostprocessor like that:

    Code (CSharp):
    1.                         ani aniFile = new ani(asset);
    2.                         fileName = fileName.Replace(".ani", string.Empty);
    3.                         AnimationClip clip = aniFile.GetAssetObject();
    4.                         clip.name = fileName;
    5.                         AssetDatabase.DeleteAsset(aniOutputPath + fileName + ".anim");
    6.                         AssetDatabase.CreateAsset(clip, aniOutputPath + fileName + ".anim");
    Problematic line of code i can't figure out is "AnimationClip clip = aniFile.GetAssetObject();", it's the GetAssetObject() method that i am not sure what to do with. In it's body i should build the Animation Clip.
    Creating Mesh in GetAssetObject() template i've got is done like that:
    Code (CSharp):
    1.         public override Mesh GetAssetObject()
    2.         {
    3.             Mesh mesh = new Mesh();
    4.             if (this.wasLoaded)
    5.             {
    6.                 LODMesh lod = lods[0];
    7.                 List<Vector3> verts = new List<Vector3>();
    8.                 List<Vector3> norms = new List<Vector3>();
    9.                 List<Vector2> uvs = new List<Vector2>();
    10.                 List<BoneWeight> weights = new List<BoneWeight>();
    11.  
    12.                 for(int i = 0; i < lod.verticies.Length; i++)
    13.                 {
    14.                     verts.Add(lod.verticies[i].position);
    15.                     uvs.Add(lod.verticies[i].UV);
    16.                     norms.Add(lod.verticies[i].normal);
    17.  
    18.                     if (lod.hasBones == 1)
    19.                     {
    20.                         BoneWeight weight = new BoneWeight();
    21.                         weight.boneIndex0 = lod.verticies[i].bone1;
    22.                         weight.boneIndex1 = lod.verticies[i].bone2;
    23.                         weight.weight0 = lod.verticies[i].weight1;
    24.                         weight.weight1 = lod.verticies[i].weight2;
    25.                         weights.Add(weight);
    26.                     }
    27.                 }
    28.  
    29.                 mesh.SetVertices(verts);
    30.                 mesh.SetNormals(norms);
    31.                 mesh.uv = uvs.ToArray();
    32.  
    33.                 if (lod.hasBones == 1)
    34.                     mesh.boneWeights = weights.ToArray();
    35.  
    36.                 int numTris = lod.indexCount;
    37.                 int[] triangles = new int[numTris];
    38.                 for(int j = 0; j < numTris; j++)
    39.                 {
    40.                     triangles[j] = (int)lod.faces[j];
    41.                 }
    42.  
    43.                 mesh.SetTriangles(triangles, 0);
    44.                 //mesh.RecalculateNormals();
    45.  
    46.                 mesh.RecalculateTangents();
    47.                 mesh.RecalculateBounds();
    48.             }
    49.             return mesh;
    50.         }
    The other thing is that i found is Maya's .anim file that was stated could be used right away from Unity. Got the file structure here: https://knowledge.autodesk.com/supp...87541258-2463-497A-A3D7-3DEA4C852644-htm.html

    Should i go this path and instead make Maya's .anim file instead? Are they actually the same thing or not?
    My problem here is that naming conventions of my animation format does not align with Maya's. Still need to figure out what is what and where it goes.