Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.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# 如何防止对协同程序的中断导致其在Unity中重新启动/重复自身?_C#_Unity3d - Fatal编程技术网

C# 如何防止对协同程序的中断导致其在Unity中重新启动/重复自身?

C# 如何防止对协同程序的中断导致其在Unity中重新启动/重复自身?,c#,unity3d,C#,Unity3d,在我的项目中,我有一个对象和一些按钮。当我按下其中一个按钮时,对象将在协同程序中滑动到新的旋转。当用户在协同程序有机会第一次完成方法之前按下按钮时,就会发生错误。在这种情况下,它基本上重新启动从开始旋转到新旋转的slerp运动。有没有办法防止这种情况发生?我可以使用一个布尔值来检查是否协同程序仍然处于倾斜的中间,但是一个理想的解决方案是一个停止SLERP的地方,然后转向新的旋转。下面是我在按钮上使用的协同程序: public IEnumerator SlerpRotation(Transform

在我的项目中,我有一个对象和一些按钮。当我按下其中一个按钮时,对象将在协同程序中滑动到新的旋转。当用户在协同程序有机会第一次完成方法之前按下按钮时,就会发生错误。在这种情况下,它基本上重新启动从开始旋转到新旋转的slerp运动。有没有办法防止这种情况发生?我可以使用一个布尔值来检查是否协同程序仍然处于倾斜的中间,但是一个理想的解决方案是一个停止SLERP的地方,然后转向新的旋转。下面是我在按钮上使用的协同程序:

public IEnumerator SlerpRotation(Transform trans, Quaternion slerpTo)
{
        timer = 0;
        Quaternion rotation = trans.localRotation;
        while (timer < 2)
        {
            trans.localRotation = Quaternion.Slerp(rotation, slerpTo, timer/2f);
            timer += Time.deltaTime;
            yield return null;
        }
}
public IEnumerator SLERPOtation(变换变换、四元数SLERPOT)
{
定时器=0;
四元数旋转=trans.localRotation;
同时(计时器<2)
{
trans.localRotation=Quaternion.Slerp(旋转,slerpTo,定时器/2f);
timer+=Time.deltaTime;
收益返回空;
}
}

在使用此方法之前,我有另一种方法,它基本上只是检查它是否等于其他两个角度,并在这两个角度之间切换。

您可以将
协程
保存到
变量
。无论何时需要启动
协同程序
,都可以先停止
协同程序
,然后重新启动。像这样:

float timer;
Coroutine theCoroutine;

public void SlerpRot(){
    if (theCoroutine != null)
        StopCoroutine (theCoroutine);
    theCoroutine = StartCoroutine (SlerpRotation(this.transform,Quaternion.identity));
}
public IEnumerator SlerpRotation(Transform trans, Quaternion slerpTo)
{
    timer = 0;
    Quaternion rotation = trans.localRotation;
    while (timer < 2)
    {
        trans.localRotation = Quaternion.Slerp(rotation, slerpTo, timer/2f);
        timer += Time.deltaTime;
        yield return null;
    }
}
浮点定时器;
协同常规检查;
公共无效SlerpRot(){
if(计算例程!=null)
StopCorroutine(客户端);
coroutine=start例程(SlerpRotation(this.transform,Quaternion.identity));
}
公共IEnumerator SLERPOtation(变换变换变换,四元数SLERPOT)
{
定时器=0;
四元数旋转=trans.localRotation;
同时(计时器<2)
{
trans.localRotation=Quaternion.Slerp(旋转,slerpTo,定时器/2f);
timer+=Time.deltaTime;
收益返回空;
}
}

您可以将
协同程序
保存到
变量
。无论何时需要启动
协同程序
,都可以先停止
协同程序
,然后重新启动。像这样:

float timer;
Coroutine theCoroutine;

public void SlerpRot(){
    if (theCoroutine != null)
        StopCoroutine (theCoroutine);
    theCoroutine = StartCoroutine (SlerpRotation(this.transform,Quaternion.identity));
}
public IEnumerator SlerpRotation(Transform trans, Quaternion slerpTo)
{
    timer = 0;
    Quaternion rotation = trans.localRotation;
    while (timer < 2)
    {
        trans.localRotation = Quaternion.Slerp(rotation, slerpTo, timer/2f);
        timer += Time.deltaTime;
        yield return null;
    }
}
浮点定时器;
协同常规检查;
公共无效SlerpRot(){
if(计算例程!=null)
StopCorroutine(客户端);
coroutine=start例程(SlerpRotation(this.transform,Quaternion.identity));
}
公共IEnumerator SLERPOtation(变换变换变换,四元数SLERPOT)
{
定时器=0;
四元数旋转=trans.localRotation;
同时(计时器<2)
{
trans.localRotation=Quaternion.Slerp(旋转,slerpTo,定时器/2f);
timer+=Time.deltaTime;
收益返回空;
}
}

如果当前正在运行协同路由,您可以设置一个标志,然后取消该协同路由,或者在第一个协同路由完成时将要启动的新协同路由排队……如果当前正在运行协同路由,您可以设置一个标志,然后取消该协同路由,或者在第一个协同路由完成时将要启动的新协同路由排队……就是这样正是我需要的。我真的不知道你可以保存一个到变量的协程,这正是我所需要的。我真的不知道你能把一个协同程序保存到一个变量上哈哈