Play Animation File

Hi all. Hope someone can help. I'm relatively new to Unity.

A minimal scene I have is that I create a Cube from the GameObject menu. Then I create a test animation clip in Unity for the cube and saved it as "CubeAnimation". Then I created a script that I dragged onto the cube in the scene. My script is:

public class Foo {

// Drag my CubeAnimation asset to this variable
public AnimationClip clip;

private void Awake()
{
    gameObject.AddComponent("Animation");
    gameObject.animation.AddClip(clip, "CubeAnimation");
    gameObject.animation.Play("CubeAnimation");
}

}

All is fine as expected, and the animation plays. However, what I wish to do is to be able to do something like:

public class Foo {

private AnimationClip clip;

private void Awake()
{
    gameObject.AddComponent("Animation");
    clip = new CubeAnimation();  // not possible
    gameObject.animation.AddClip(clip, "CubeAnimation");
    gameObject.animation.Play("CubeAnimation");
}

}

In other words, I wish to be able to add my clip by way of a script rather than dragging it over. I know there is a way to do:

clip = AnimationClip();

but obviously, that alone doesn't help. There's SetCurve, but I don't know how that applies here.

Thank you kindly.

You want to create curves from code?

This code might give you some idea:

AnimationClip clip = new AnimationClip();
clip.name = animName;
animation.AddClip(clip, clip.name);

AnimationCurve curve = new AnimationCurve();
curve.AddKey(0, 2);
curve.AddKey(animationLengh, 4);
clip.SetCurve("", typeof(Transform), "localPosition.x", curve);     

Maybe you just want to do this:

foreach (AnimationState state in animation)
{
    AnimationClip clip = state.clip;
    // register each clip on some list ?
}

My second guess based on this comment:

I can live with having a publicly-declared AnimationClip then dragging-n-dropping to it, but I was hoping to assign the reference, via a line of code instead, just because it would be consistent with what I'm doing. My motive has to do with a desire to pair up a unique integer with Animation and AnimationClip, and use that in a hashtable, and store the information to disk.

You can't just get AnimationClip reference in runtime based on name (or anything like that) in runtime. Everything has to referenced somewhere in your scene (that's how Unity know which assets have to be included into scene). But you can still generate these references at import time. Look at http://unity3d.com/support/documentation/ScriptReference/AssetPostprocessor.OnPostprocessModel.html

You could collect references of all imported animations into some list/hashtable at import-time and then use it at run-time.

Very quick'n'dirty piece of code:

class MyModelPostprocessor : AssetPostprocessor {
    void OnPostprocessModel (GameObject go) {
        if (go.animation && go.animation.clip)
        {
            AnimationClip c = g.animation.clip;
            go.AddComponent(typeof(MySriptName));
            MySriptName s = go.GetComponent<MySriptName>();
            s.AddClip(c);
        }
    }
}

Yes, you did answer my question unambiguously regarding whether or not I could obtain a reference to an animation clip at run-time in code. I'm not sure how to mark my question as having been answered, so funnily, I'm answering my own question and marking it that way as having been answered :). Thank you again.