- Home /
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.
Answer by Paulius-Liekis · Dec 10, 2010 at 12:25 PM
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);
Actually no, I do not wish to create curves from code. I was just thinking that SetCurve was the only thing close to what I wish to accomplish. Basically, I want to be able to play an AnimationClip, via a script, without having to drag-n-drop the AnimationClip to a publically declared variable within my script. Partly based upon your snippet, I was thinking I could do:
clip = new AnimationClip();
clip.name = "CubeAnimation";
gameObject.AddComponent("Animation");
gameObject.animation.AddClip(clip, clip.name);
gameObject.animation.Play("CubeAnimation");
but no luck.
Then the answer is kind of "no". You can't really tell Unity to find animation clip by name and add it you your component. You have to get AnimationClip reference from somewhere and add it to Animation component. You can get it either from variable, or from other Animation component or something like that.
Anyway, I feel like we're looking at your problem at too small scale. Could you explain why you can't/don't want just pass animation clip through variable and we might be able to help you with it.
Hi and thanks again for the reply! You're very intuitive about my real motive. 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 ins$$anonymous$$d, just because it would be consistent with what I'm doing. $$anonymous$$y 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. I'm okay with things though. Just wanted to make sure before I went any further. Unity is truly awesome and a blast to work with!
Answer by Paulius-Liekis · Dec 12, 2010 at 04:17 PM
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);
}
}
}
I still feel like I' trying to solve some problem which I do not fully understand :)
Hi thank you again for the awesome reply. Philosophically, you could say that I'm searching for a general, "typical" work-flow for how to do "everything". I'm just finding that as powerful as Unity is, it allows multiple ways to do the same thing for many tasks, so if I did things differently many times, I wind up forgetting how I did things. So I'm sort of trying to "standardize" the way I do things. This is a salient feature to me because I'm currently creating a big project with dummy placeholders for all assets, and when I go to switch them out, it could prove very difficult.
Sounds like there is no absolute answer to your question :) but if you find one then either mark one of the answers as correct, or post your own answer and mark it as correct.
Answer by Coder · Dec 16, 2010 at 05:49 AM
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.
Your answer
Follow this Question
Related Questions
Problem with Animator.Play() 0 Answers
Streaming music from a folder or file 0 Answers
Reading anim files at runtime 0 Answers
Stop play procedure from within a script 0 Answers
play animation clip 0 Answers