Unity - 即使条件为false也会执行块

时间:2017-04-18 12:59:28

标签: c# android unity3d unity5

这是我的代码:

void Update () {
    //------------ CHECKING "IF RUNNING" FOR ALL ACTIONS FROM LOCAL PLAYER ---------------------- 
    if (MatchManager.GetMMInstance().Running)
    {
        Debug.Log("I Am Running");
        // ---------- orbiting and sending it -------------
        //if (Input.GetKeyDown(KeyCode.D))
        ClockwiseOrbitButton.GetComponent<Button>().onClick.AddListener(() =>
        {
            if (ClockwiseOrbitPressPermit)
            {
                ClockwiseOrbitPressPermit = false;
                if (MatchManager.GetMMInstance().NearCabin.Local)
                {
                    // we send this before we start our orbit that maybe it compensate possibly delay of internet. also we send it once again in the end of continousorbit with speed value of zero.
                    Network.GetNetInstance().SendOrbit(1, MatchManager.GetMMInstance().NearCabin.Center.transform.rotation.eulerAngles, 1, InitialOrbitSpeed);
                    Orbit(1, MatchManager.GetMMInstance().NearCabin.Center.transform.rotation.eulerAngles, 1, InitialOrbitSpeed, BaseDeceleratorAmount);
                }
                else if (MatchManager.GetMMInstance().MidCabin.Local)
                {
                    // we send this before we start our orbit that maybe it compensate possibly delay of internet. also we send it once again in the end of continousorbit with speed value of zero.
                    Network.GetNetInstance().SendOrbit(2, MatchManager.GetMMInstance().MidCabin.Center.transform.rotation.eulerAngles, 1, InitialOrbitSpeed);
                    Orbit(2, MatchManager.GetMMInstance().MidCabin.Center.transform.rotation.eulerAngles, 1, InitialOrbitSpeed, BaseDeceleratorAmount);
                }
                else if (MatchManager.GetMMInstance().FarCabin.Local)
                {
                    // we send this before we start our orbit that maybe it compensate possibly delay of internet. also we send it once again in the end of continousorbit with speed value of zero.
                    Network.GetNetInstance().SendOrbit(3, MatchManager.GetMMInstance().FarCabin.Center.transform.rotation.eulerAngles, 1, InitialOrbitSpeed);
                    Orbit(3, MatchManager.GetMMInstance().FarCabin.Center.transform.rotation.eulerAngles, 1, InitialOrbitSpeed, BaseDeceleratorAmount);
                }
                StartCoroutine(ReactiveOrbit(OrbitCoolDown));
            }
        });
        //if (Input.GetKeyDown(KeyCode.A))
        CoClockwiseOrbitButton.GetComponent<Button>().onClick.AddListener(() =>
        {
            if (CoClockwiseOrbitPressPermit)
            {
                CoClockwiseOrbitPressPermit = false;
                if (MatchManager.GetMMInstance().NearCabin.Local)
                {
                    // we send this before we start our orbit that maybe it compensate possibly delay of internet. also we send it once again in the end of continousorbit with speed value of zero.
                    Network.GetNetInstance().SendOrbit(1, MatchManager.GetMMInstance().NearCabin.Center.transform.rotation.eulerAngles, 2, InitialOrbitSpeed);
                    Orbit(1, MatchManager.GetMMInstance().NearCabin.Center.transform.rotation.eulerAngles, 2, InitialOrbitSpeed, BaseDeceleratorAmount);
                }
                else if (MatchManager.GetMMInstance().MidCabin.Local)
                {
                    // we send this before we start our orbit that maybe it compensate possibly delay of internet. also we send it once again in the end of continousorbit with speed value of zero.
                    Network.GetNetInstance().SendOrbit(2, MatchManager.GetMMInstance().MidCabin.Center.transform.rotation.eulerAngles, 2, InitialOrbitSpeed);
                    Orbit(2, MatchManager.GetMMInstance().MidCabin.Center.transform.rotation.eulerAngles, 2, InitialOrbitSpeed, BaseDeceleratorAmount);
                }
                else if (MatchManager.GetMMInstance().FarCabin.Local)
                {
                    // we send this before we start our orbit that maybe it compensate possibly delay of internet. also we send it once again in the end of continousorbit with speed value of zero.
                    Network.GetNetInstance().SendOrbit(3, MatchManager.GetMMInstance().FarCabin.Center.transform.rotation.eulerAngles, 2, InitialOrbitSpeed);
                    Orbit(3, MatchManager.GetMMInstance().FarCabin.Center.transform.rotation.eulerAngles, 2, InitialOrbitSpeed, BaseDeceleratorAmount);
                }
                StartCoroutine(ReactiveOrbit(OrbitCoolDown));
            }

        });


        // ---------- Shooting and sending it ------ - - - - - - -

        ShootButton.GetComponent<Button>().onClick.AddListener(() =>
        {
            if (ShootPressPermit)
            {
                ShootPressPermit = false;
                if (MatchManager.GetMMInstance().NearCabin.Local)
                {
                    // we send this before we Shoot, maybe it compensate possibly delay of internet.
                    Network.GetNetInstance().SendProduceBullet(1);
                    Shoot(1);
                }
                else if (MatchManager.GetMMInstance().MidCabin.Local)
                {
                    // we send this before we Shoot, maybe it compensate possibly delay of internet.
                    Network.GetNetInstance().SendProduceBullet(2);
                    Shoot(2);
                }
                else if (MatchManager.GetMMInstance().FarCabin.Local)
                {
                    // we send this before we Shoot, maybe it compensate possibly delay of internet.
                    Network.GetNetInstance().SendProduceBullet(3);
                    Shoot(3);
                }
                StartCoroutine(ReactiveShooting(MatchManager.GetMMInstance().LocalShootCoolDown));
            }
        });

        // ------------------ switching target and send it ----------------

        SwitchTargetButton.GetComponent<Button>().onClick.AddListener(() =>
        {
            if (SwitchTargetPressPermit)
            {
                SwitchTargetPressPermit = false;
                if (MatchManager.GetMMInstance().NearCabin.Local)
                {
                    // we send this before we Switch, maybe it compensate possibly delay of internet.
                    Network.GetNetInstance().SendSwitchTarget(1);
                    SwitchTarget(1);
                }
                else if (MatchManager.GetMMInstance().MidCabin.Local)
                {
                    // we send this before we Switch, maybe it compensate possibly delay of internet.
                    Network.GetNetInstance().SendSwitchTarget(2);
                    SwitchTarget(2);
                }
                else if (MatchManager.GetMMInstance().FarCabin.Local)
                {
                    // we send this before we Switch, maybe it compensate possibly delay of internet.
                    Network.GetNetInstance().SendSwitchTarget(3);
                    SwitchTarget(3);
                }
                StartCoroutine(ReactiveSwitchTarget(SwitchTargetCoolDown));
            }
        });

        //-----------------------  END OF ACTION SCOPE -----------------------------

        // ----------------- WAITING AND CHECKING SOME EVENT -----------------------
        // ** these events can happen only during running state

        WaitingForCrippling();

        // ---------------------- END OF WAIT AND CHECK ----------------------------
    }

    // --------------- SOME CONTINUOUS UPDATING METHODS ---------------------
    // these actions don't need to be in running block and don't depend on running state. can happen in any moments. 
    UpdateTargetPosition();
    Debug.Log(MatchManager.GetMMInstance().Running.ToString());
}

当场景运行时,“Running”值为“false”几秒钟,然后它将变为“true”。它将保持这样,直到玩家的健康状态变为0,此时它将再次变为“假”(这是一个有3名玩家的多人游戏)。

if-block包含一些在按下UI按钮时运行的代码。在开始的几秒钟内,“Running”为假,一切正常(按下按钮不起作用)。

当它变为true时,它再次正常工作(按下按钮将遵循正确的操作)。但是,当玩家的健康状况达到0,并且“正在运行”变量变为“假”时,我仍然可以按下按钮以及动作和事件 还是会发生!

我不知道问题是什么。我将Debug.Log("I Am Running");Debug.Log(MatchManager.GetMMInstance().Running.ToString());放在代码中,如您所见,并且每帧都会看到“Running”变量的值。

我看到它是“假的”,按下按钮仍然执行轨道和切换目标等动作!

2 个答案:

答案 0 :(得分:1)

当您为按钮添加onClick侦听器时,按下按钮时按钮将始终执行您在侦听器中指定的操作,这是正常的。

如果您希望它停止,则应在值为false时删除侦听器。您可以使用RemoveAllListeners.

删除所有内容

添加点击监听器的重点是,您只需指定按钮单击所要执行的操作,并且每次按钮单击都会执行完全相同的操作。

我建议您在启动功能中只添加一次onClick侦听器,当您需要按钮停止工作时,可以禁用该按钮。这样,您不必每个帧都设置一个新的onClick侦听器。

要禁用此按钮,您可以将interactable property设置为false

ClockwiseOrbitButton.GetComponent<Button>().interactable = false;

然后,当您需要再次进行交互时,您可以随时将其设置回true

ClockwiseOrbitButton.GetComponent<Button>().interactable = true;

答案 1 :(得分:1)

您需要保存所有按钮&#39;动作(Events.UnityAction)并删除它们,如果&#34;运行&#34;是假的,即:

void Update () {
//------------ CHECKING "IF RUNNING" FOR ALL ACTIONS FROM LOCAL PLAYER ---------------------- 
    if (MatchManager.GetMMInstance().Running)
    {
        //...your previous code
        //save references to Events.UnityActions
    } 
    else
    {
        ClockwiseOrbitButton.GetComponent<Button>().onClick.RemoveListener(referenseToTheAction1);
        CoClockwiseOrbitButton.GetComponent<Button>().onClick.RemoveListener(referenseToTheAction2);

        // etc.; remove all Events.UnityAction listeners from other buttons.
    }
}

或者您可以在else语句SomeButton.GetComponent<Button>().onClick.RemoveAllListeners();中为每个按钮调用,而不保存对Events.UnityActions的引用。