【问题标题】:How to create and run animation at runtime?如何在运行时创建和运行动画?
【发布时间】:2021-09-03 06:33:07
【问题描述】:

我目前正在制作游戏,并希望在运行时创建和运行动画。 这是我到目前为止得到的:

void Start()
{
    Sprite[] sprites = upgrades.GetComponent<uprgades>().sprites;
    AnimationClip animClip = new AnimationClip();
    animClip.frameRate = 60;

    EditorCurveBinding spriteBinding = new EditorCurveBinding();
    spriteBinding.type = typeof(SpriteRenderer);
    spriteBinding.path = "";
    spriteBinding.propertyName = "m_Sprite"; 

    ObjectReferenceKeyframe[] spriteKeyFrames = new ObjectReferenceKeyframe[sprites.Length];
    for(int j = 0; j < (sprites.Length); j++)
    {
        spriteKeyFrames[j] = new ObjectReferenceKeyframe();
        spriteKeyFrames[j].time = j;
        spriteKeyFrames[j].value = sprites[j];
    }

    AnimationUtility.SetObjectReferenceCurve(animClip, spriteBinding, spriteKeyFrames);
    AnimationClipSettings animClipSett = new AnimationClipSettings();
    animClipSett.loopTime = true;

    animClip.legacy = true; //added
 
    AnimationUtility.SetAnimationClipSettings(animClip, animClipSett);

我不完全了解所有内容,因为我在互联网上找到了这个。但现在我需要开始动画。所以我这样做了:

    Animation anim = someGameObject.GetComponent<Animation>();
    anim.clip = animClip;
    anim.Play();
}

但这不起作用。我认为这与我没有任何动画师/或控制器有关,但我不知道是否必须添加它们,我应该如何以及在哪里执行此操作

【问题讨论】:

  • 你试过制作AnimationCliplegacy吗?
  • 我已经添加了它(你可以在上面的代码中看到),但它似乎没有改变任何东西

标签: c# unity3d animation runtime


【解决方案1】:

你在哪里运行动画?是在更新功能里吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-13
    • 2013-05-06
    • 1970-01-01
    • 2019-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多