【问题标题】:How to start more than one animation with unity animator?如何用统一动画师启动多个动画?
【发布时间】:2016-04-16 08:46:03
【问题描述】:

我有一个 animatormanager 能够从 1 个动画创建运行时动画(开始帧、结束帧和延迟,然后计算速度,然后进行变形等等)。我的统一版本是5.3.1f1

一切正常,有些事情没有完成,但是是可选的。动画师可以对请求的动画进行排队,以防止打断当前的动画。

问题是,我只能用一个对象启动 1 次动画。启动下一个动画的代码是完全一样的,只是没什么可做的。

事件,如果它的 2 次相同的动画具有相同的动画数据 所有的配方都很好并且经过测试。

我使用断点进行了密集调试,以确保在任何时候一切正常

有什么东西可以阻止我在一个对象上一个接一个地启动动画 2 次吗?我根本没有错误或警告。无论我在 AnimData 结构中设置什么设置,第一个动画都可以正常工作,但第二次没有任何反应。

这是最重要的:

public int? startAnim(int index)
{
    if(index < animIndex.Count)
    {
        startAnim(animIndex[index]);
    }
    return null;
}

//private because the struct is internal, this make sure the animator keep control of the list.
private int? startAnim(AnimData animD)
{
    if(locked)
    {
        #if UNITY_EDITOR
        Debug.Log("anim locked");
        #endif

        return null;
    }

    //current anim (queue anim) not finished
    if(endTime > Time.time)
    {
        if(canQueue)
        {
            addToQueue(animD);
            return animDataQ.Count;
        }
        else
        {
            return null;
        }
    }
    else
    {
        endTime = Time.time + animD.TotalTime;
        StartCoroutine(animManager(animD));
        return 0;
    }

    return null;
}

#endregion anim starters

private IEnumerator animManager(AnimData animData)
{
        animator.speed = Mathf.Abs(animData.calculateSpeed(animLength, AnimType.main, currentKey).Value);

        //animator.Play(0,0, animData.StartKey/animLength);
        if(animData.AnimSpeed > 0)
        {
            animator.Play(0,0, animData.StartKey/animLength/2);
        }
        else
        {
            //animator.Play(0,0, (animLength * 2) - (animData.StartKey/animLength));
            animator.Play(0,0, (((animLength*2) - animData.StartKey)/(animLength * 2)));
        }

            //animator.Play(0,0, (animData.AnimSpeed > 0) ?  animData.StartKey/animLength : ((animLength * 2) - (animData.StartKey/animLength)));

        yield return new WaitForSeconds(animData.Delay);
        animator.Stop();

        yield return null;

    endAnim();

}


private void addToQueue(AnimData animD)
{
    animDataQ.Enqueue(animD);
    endTime += animD.TotalTime;
    queueTime += animD.TotalTime;
}

private void endAnim()
{
    if(canQueue && animDataQ.Count > 0)
    {
        StartCoroutine(animManager(animDataQ.Dequeue()));
    }
}

感谢您的宝贵时间。

【问题讨论】:

    标签: c# unity3d coroutine


    【解决方案1】:

    我找到了解决方法。 我不想将其标记为可接受的解决方案,因为我认为它不“干净”

    对于像我这样的特定情况,动画师肯定存在某种问题。

    我的解决方案是使用 animator.speed = 0 而不是 animator.stop(); 有了这个小小的改变,一切都立即奏效了

    我会向统一论坛提出我的问题,因为肯定有一些奇怪的事情

    【讨论】:

      猜你喜欢
      • 2017-10-20
      • 2014-09-03
      • 1970-01-01
      • 1970-01-01
      • 2019-04-03
      • 2019-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多