簡體   English   中英

Unity-如果條件為false時也執行塊

[英]Unity - if block is executed even when condition is false

這是我的代碼:

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”變量的值。

我已經看到它是“假”的,按下按鈕仍會執行類似繞軌道運動和切換目標的操作!

通常,當您為按鈕添加onClick偵聽器時按下按鈕時,操作就會發生,該按鈕將始終執行您在偵聽器中指定的操作。

如果您希望它停止,則應在值為false時刪除偵聽器。 您可以使用RemoveAllListeners將其全部刪除

添加單擊偵聽器的要點是,只需指定一次按鈕單擊應該執行的操作,它將為每次按鈕單擊執行完全相同的操作。


我建議您僅在start函數中添加一次onClick偵聽器,並且當您需要按鈕停止工作時可以禁用該按鈕。 這樣,您不必總是在每個幀上都設置一個新的onClick偵聽器。

要禁用按鈕,可以將interactable屬性設置為false

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

然后,當您需要再次可交互時,可以隨時將其設置為true

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

您需要保存所有按鈕的動作(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的引用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM