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.
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.
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.
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.
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.
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.
– Paulius-LiekisAnyway, 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.
– Paulius-Liekis