统一 - 如果即使条件为假也执行块
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 并且 "Running" 变量变为 "false" 时,我仍然可以按下按钮以及动作和事件
也照样发生!
我不知道是什么问题。我将 Debug.Log("I Am Running");
和 Debug.Log(MatchManager.GetMMInstance().Running.ToString());
放在代码中,如您所见并观察了每一帧的 "Running" 变量的值。
我看到它是 "false" 并且按下按钮仍然执行轨道和切换目标等操作!
在为按钮添加 onClick 侦听器的那一刻按下按钮时发生操作是正常的,该按钮将始终执行您在侦听器中指定的操作。
如果你想让它停止,你应该在你的值为 false 时移除监听器。您可以使用 RemoveAllListeners.
将它们全部删除
添加点击侦听器的要点是,您只需指定一次按钮点击应该做什么,每次按钮点击都会做完全相同的事情。
我建议您只在启动函数中添加一次 onClick 侦听器,当您需要按钮停止工作时,您可以禁用该按钮。这样你就不会总是在每一帧都设置一个新的 onClick 侦听器。
要禁用该按钮,您可以将 interactable
property 设置为 false
ClockwiseOrbitButton.GetComponent<Button>().interactable = false;
然后您可以随时将其设置回 true
当您需要它再次可交互时
ClockwiseOrbitButton.GetComponent<Button>().interactable = true;
您需要保存所有按钮的操作 (Events.UnityAction),如果 "Running" 为假,则将其删除,即:
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.UnityAction 的引用。
这是我的代码:
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 并且 "Running" 变量变为 "false" 时,我仍然可以按下按钮以及动作和事件 也照样发生!
我不知道是什么问题。我将 Debug.Log("I Am Running");
和 Debug.Log(MatchManager.GetMMInstance().Running.ToString());
放在代码中,如您所见并观察了每一帧的 "Running" 变量的值。
我看到它是 "false" 并且按下按钮仍然执行轨道和切换目标等操作!
在为按钮添加 onClick 侦听器的那一刻按下按钮时发生操作是正常的,该按钮将始终执行您在侦听器中指定的操作。
如果你想让它停止,你应该在你的值为 false 时移除监听器。您可以使用 RemoveAllListeners.
将它们全部删除添加点击侦听器的要点是,您只需指定一次按钮点击应该做什么,每次按钮点击都会做完全相同的事情。
我建议您只在启动函数中添加一次 onClick 侦听器,当您需要按钮停止工作时,您可以禁用该按钮。这样你就不会总是在每一帧都设置一个新的 onClick 侦听器。
要禁用该按钮,您可以将 interactable
property 设置为 false
ClockwiseOrbitButton.GetComponent<Button>().interactable = false;
然后您可以随时将其设置回 true
当您需要它再次可交互时
ClockwiseOrbitButton.GetComponent<Button>().interactable = true;
您需要保存所有按钮的操作 (Events.UnityAction),如果 "Running" 为假,则将其删除,即:
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.UnityAction 的引用。