1、作用
切换一个布尔值(bool)的状态
简化代码并减少冗余
2、解释
bool isPlaying = true; // 假设游戏当前正在进行中
// 玩家点击了暂停按钮
isPlaying = !isPlaying; // 通过逻辑取反操作切换isPlaying的状态
// 现在isPlaying的值是false,表示游戏已暂停
3、实例
public void Pause()
{
isPause = !isPause;//改变当前isPause的值(与当前的值相反)
if (isPause)
{
Time.timeScale = 0;//时间冻结(游戏暂停)
pauseImage.sprite = pauseSprites[1];//调用图片1(索引值为1的图片)
}
else
{
Time.timeScale = 1;//游戏恢复正常速度
pauseImage.sprite = pauseSprites[0];//调用图片0
}
}
实例解释:
(1) 游戏当前是运行状态时,isPause的值为false
(2) 调用Pause方法时,isPause = !isPause;使isPause的值变为true——游戏暂停
(3) 再次调用Pause方法,游戏恢复运行