get animaton clip lenght script unity code example

Example: how to get clip length of animation unity

public float attackTime;
    public float damageTime;
    public float deathTime;
    public float idleTime;
 
    private Animator anim;
    private AnimationClip clip;
 
    // Use this for initialization
    void Start () {
        anim = GetComponent<Animator>();
        if(anim == null)
        {
            Debug.Log("Error: Did not find anim!");
        } else
        {
            //Debug.Log("Got anim");
        }
 
   		UpdateAnimClipTimes();
   	}
    public void UpdateAnimClipTimes()
    {
        AnimationClip[] clips = anim.runtimeAnimatorController.animationClips;
        foreach(AnimationClip clip in clips)
        {
            switch(clip.name)
            {
                case "Attacking":
                    attackTime = clip.length;
                    break;
                case "Damage":
                    damageTime = clip.length;
                    break;
                case "Dead":
                    deathTime = clip.length;
                    break;
                case "Idle":
                    idleTime = clip.length;
                    break;
            }
        }
 	}