Search Unity

Simple AssetPostprocessor example

Discussion in 'Scripting' started by goodhustle, Jul 2, 2010.

  1. goodhustle

    goodhustle

    Joined:
    Jun 4, 2009
    Posts:
    310
    I was just working on a dead simple AssetPostprocessor script to learn the ropes, and I thought I'd post it here for others new to Editor scripting.

    This script is mostly useful if you are working with a 3d artist that gives you separate FBX files for each animation for a character. It will print a debug message to the console stating how many frames each animation has. This is handy if you need to recombine them or keep track of the frame count and don't have Maya to drop into.

    Just place this in Editor\CheckAnimationFrames.cs.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4. using System.Collections;
    5.  
    6. public class CheckAnimationFrames : AssetPostprocessor {
    7.  
    8.     void OnPostprocessModel (GameObject g) {
    9.         // Only operate on FBX files
    10.         if (assetPath.IndexOf(".fbx") == -1) {
    11.             return;
    12.         }
    13.                
    14.         if (EditorUtility.DisplayDialog("FrameCount", "Show number of frames?", "Yes", "No")) {
    15.             ShowFrames(g);
    16.         }
    17.     }
    18.  
    19.     void ShowFrames(GameObject g) {
    20.  
    21.         Animation anim = (Animation)g.GetComponent(typeof(Animation));
    22.         foreach (AnimationState state in anim) {
    23.             AnimationClip clip = state.clip;
    24.             Debug.Log(g.name + " animation data is " + (clip.length * clip.frameRate) + " frames long");   
    25.         }
    26.        
    27.        
    28.     }
    29. }
    30.  
    31.  
     
  2. kenshin

    kenshin

    Joined:
    Apr 21, 2010
    Posts:
    940
    Thanks for sharing!
     
    marc_unity692 likes this.
  3. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,689
    Fantastic sample and just for that you got a reference in my upcoming book titled "Mastering Unity2D game development" in Chapter 11 :D
     
  4. Boulou-be

    Boulou-be

    Joined:
    Jul 30, 2014
    Posts:
    3
    Thanks !