提问人:Poka 提问时间:8/16/2023 更新时间:8/16/2023 访问量:39
所有代码都可以立即工作,但我需要逐渐
All code works instantly, but I need to gradually
问:
我有一个对象,应该用 ESCAPE 键打开,也可以用 ESCAPE 键关闭。我写了代码,但由于某种原因,当我按 ESCAPE 时,一切都会立即起作用,一旦更改,第二个代码也会起作用。boolean
using UnityEngine;
using System.Collections;
public class MouseCursor : MonoBehaviour {
public GameObject pause;
bool bool_determine = false;
// Use this for initialization
void Start () {
Cursor.lockState = CursorLockMode.Locked;
}
void OnGUI()
{
if(Input.GetKeyDown(KeyCode.Escape)){
if(bool_determine){
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = true;
pause.SetActive(true);
bool_determine = true;
Debug.Log("hey1" + bool_determine);
}else{
if(!bool_determine){
Cursor.lockState = CursorLockMode.None;
Cursor.visible = false;
pause.SetActive(false);
bool_determine = false;
Debug.Log("hey2" + bool_determine);
}
}
}
}
// Update is called once per frame
void Update () {
}
}
答:
2赞
Mr E Coder
8/16/2023
#1
我不太确定。但我发现了一些问题。 当bool_determine已经为 false 时,您将其设置为 false,当它已经为 true 时,您将其设置为 true。
我想你可以稍微优化一下代码:
if(Input.GetKeyDown(KeyCode.Escape)){
bool_determine = !bool_determine;
Cursor.visible = bool_determine;
pause.SetActive(bool_determine);
Debug.Log("Helo " + bool_determine);
}
如果我能帮忙就好了
评论
0赞
Poka
8/16/2023
谢谢。我什至没有想到我可以放一个布尔setActive
0赞
Mr E Coder
8/16/2023
欣赏它:)
0赞
derHugo
8/16/2023
@Poka ..您如何看待什么类型和是什么?;)I didn't even think that I could put a bool in setActive
true
false
评论
if (!bool_determine)
else
if(bool_determine)
bool_determine