Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Coder · Dec 10, 2010 at 06:55 AM · fileplayanimationclip

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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);

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Coder · Dec 11, 2010 at 05:01 AM 0
Share

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.

avatar image Paulius-Liekis · Dec 11, 2010 at 12:02 PM 0
Share

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.

avatar image Paulius-Liekis · Dec 11, 2010 at 12:04 PM 0
Share

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.

avatar image Coder · Dec 11, 2010 at 10:30 PM 0
Share

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!

avatar image
0

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);
        }
    }
}
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Paulius-Liekis · Dec 12, 2010 at 04:18 PM 0
Share

I still feel like I' trying to solve some problem which I do not fully understand :)

avatar image Coder · Dec 14, 2010 at 05:44 AM 0
Share

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.

avatar image Paulius-Liekis · Dec 14, 2010 at 09:17 AM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges