这是我的代码:

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为止,此时它将再次变为“ false”(这是一个具有3个玩家的多人游戏)。

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

更改为true后,它将再次正常工作(按按钮将执行正确的操作)。但是,当玩家的生命值达到0,并且“跑步”变量更改为“假”时,我仍然可以按按钮以及动作和事件
仍然会发生!

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

我已经看到它是“假”的,按下按钮仍会执行类似绕轨道运动和切换目标的操作!

最佳答案

您需要保存所有按钮的动作(Events.UnityAction),如果“ Running”为false,则将其删除,即:

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的引用。

关于c# - Unity-如果条件为false时也执行块,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43472908/

10-10 06:19