【问题标题】:Question about AnimationEvent.Time in UnityUnity中关于AnimationEvent.Time的问题
【发布时间】:2021-07-12 18:16:57
【问题描述】:

我是 Unity 的新手,我有一个关于动画事件的简单问题。 在我的代码片段中,我有公共变量 - 我注意到我无法引用 AnimationEvent - 以及一些与它们相关的简单事情。

public GameObject completeLevelUI; // a panel
public Text endUI; // the text on the panel that show your success or fail
public AnimationEvent nextSceneEvent; // AnimatonEvent in the animation of the panel above

public void GameOver() { nextSceneEvent.time = 2; completeLevelUI.SetActive(true); endUI.text = "LEVEL\nFAILED"; }

public void GameWon() { nextSceneEvent.time = 6; completeLevelUI.SetActive(true); endUI.text = "LEVEL\nCOMPLETED"; }

public void LoadEnd() { SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1); }

AnimationEvent 的目标方法为LoadEnd()
默认触发时间为 1.5 秒,代码不会更改。

在共享代码中看不到我需要不同触发时间的原因 - 现在并不重要。我想出了一些其他的方法来解决它,我只是好奇为什么它不起作用。
我尝试更改激活和时间设置代码,但它是相同的。

我必须以某种方式引用 AnimationEvent?即使从没有 AnimationEvent 的地方多次调用此脚本是否也有问题 - 我在检查期间没有收到任何异常。

有什么想法吗?感谢您的帮助!

【问题讨论】:

  • 你想对动画事件做什么?它触发并触发您的代码。你想用它做什么?
  • 触发事件时会加载另一个场景(感谢您播放场景)。我希望下一个场景在 x 秒内加载,具体取决于胜利或死亡。
  • 但是这里的重点——你在已发布的代码中看不到它——它对音量进行了一些修改。也许我会用 Invoke 来做......
  • 那么我认为你的做法是错误的。在战斗结束。设置一些你可以测试的东西,这意味着你的事件代码只是读取 if (get fight result == win) load win rlse load lost

标签: c# unity3d animation events


【解决方案1】:

我猜你有一个动画正在播放,最后你想触发新场景。 您可以使用不同的方法。

首先,将动画事件直接添加到动画剪辑中,并在检查器中设置方法。该方法必须位于附加到与动画相同的游戏对象的组件上。

其次,你启动动画并等待它结束调用方法:

void EndProcess()
{
     StartCoroutine(EndProcessSequence());
}

IEnumerator EndProcessSequence()
{
    Animation anim = GetComponent<Animation>();
    anim.Play("animName");
    yield return new WaitWhile(()=> anim.isPlaying);
    LoadNewScene();
} 

这是一个可重复使用的版本

IEnumerator EndProcessSequence(string animName, Action onComplete)
    {
        Animation anim = GetComponent<Animation>();
        anim.Play(animName);
        yield return new WaitWhile(()=> anim.isPlaying);
        onComplete?.Invoke();
    } 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多