Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将动画与声音同步_C#_Unity3d - Fatal编程技术网

C# 如何将动画与声音同步

C# 如何将动画与声音同步,c#,unity3d,C#,Unity3d,我有一个145 BPM的声音和137毫秒的偏移量,我正试图用它同步一个非常快的动画。。。我试着这样做: public class ActiveEpilepsy : MonoBehaviour { public GameObject Epilepsy; //GameObject that carries the animation float delay = 0.2f; //a fair time to disable the Epilepsy after the animati

我有一个145 BPM的声音和137毫秒的偏移量,我正试图用它同步一个非常快的动画。。。我试着这样做:

public class ActiveEpilepsy : MonoBehaviour {

    public GameObject Epilepsy; //GameObject that carries the animation
    float delay = 0.2f; //a fair time to disable the Epilepsy after the animation runs before active again

    void Start()
    {       
        InvokeRepeating("activeEpilepsy", 0.137f, 2.416f);  //second parameter is the offset of the music and the third is the bpm/60;
    }

    void activeEpilepsy()
    {
         while(delay >= 0)
         {
            Epilepsy.SetActive(true);
            delay -= Time.deltaTime;
         }

         Epilepsy.SetActive(false);

    }
}
但根本不激活游戏对象,并在我的统一中返回一个美丽的崩溃。。。另外,人们告诉我不应该使用字符串来调用方法(调用示例),那么我的代码有什么问题,如何在不使用调用的情况下使其工作?

问题发生在这里:

void activeEpilepsy()
{
     while(delay <= 0)
     {
        Epilepsy.SetActive(true);
        delay -= Time.deltaTime;
     }

     Epilepsy.SetActive(false);

}
这就是非活动对象问题。事故可能发生在别处,我看不出有什么地方出了问题


对于作为字符串的方法,它不是关于使用字符串调用,而是关于如何处理它。我认为(待确认)Invoke使用反射,因此它调用程序集清单以确定该方法是否存在。这会很慢。如果Unity足够聪明,可以将该方法添加到字典中,那么通过字符串调用几乎没有影响。

是的,我看到了“通过编辑,您需要在每次使用后重置延迟。
void activeEpilepsy()
{
     Epilepsy.SetActive(true);
     Invoke("Reset", this.delay);
}
void Reset(){Epilepsy.SetActive(false);}