Unity - 面板被禁用并在之后立即重新启用(C#)

时间:2017-04-17 22:53:25

标签: c# unity3d

只需要一种方法来解决这个问题。我知道问题所在,我只需要一个可行的解决方案。

所以基本上我有一个名为MouseManager的脚本,可以检测你是否正在查看特定的游戏对象,如果按下" f"它告诉另一个名为UIManager的脚本打开GameObject的面板(我有一个游戏,您可以在控制台上按f,它可以启用带有拼图的UI)。

基本上,我希望用户能够按下" f"或" esc"并让它退出面板,但是如果你按下" f",它会禁用面板,但会立即重新启用它,因为你还在查看游戏对象,并且两个检查都在Update()中

我需要一个解决方法,如果有人知道请注意,因为这个问题让我感到紧张:P

由于

编辑:通过控制台,我的意思是GameObject看起来像一个控制台。

编辑2:这里是代码,我会在其中添加一些注释,以便您了解每件事情的作用。我很快就会重构它,因为它太乱了......

void Start()
{
    uiPuzzles = GameObject.Find("PuzzlesUI").GetComponentInChildren<UIManager>(); // Currently I have the UIManager set to a canvas called PuzzlesUI. I will soon be moving it to an empty GameObject.
}

void Update()
{
    if (!uiPuzzles.invActive && !uiPuzzles.puzzleActive) // invActive and puzzleActive are two public booleans that just show if the inventory or any puzzle panel is empty (UIManager handles opening and closing the inventory too)
    {
        if (Input.GetButtonDown("Use") && !uiPuzzles.invActive && !uiPuzzles.puzzleActive) // Use key is bound to "f". Currently Im just checking if puzzleActive and invActive are still false.
        {
            ray = GetComponentInChildren<Camera>().ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit, 7)) // Raycast where you're looking
            {
                uiPuzzles.openPuzzleOverlay(hit.collider.name); // function in UIManager which will check if the hit collider is in a list of all the puzzle colliders currently added and if it is, open that panel.
            }
        }
    }
}

至于UIManager:

public void openPuzzleOverlay(string collider) // Get a panel name
{
    foreach (Puzzle item in puzzleList)
    {
        item.panelGameObject.SetActive(false); // Disable all puzzle panels.
        if (item.consoleGameObject.name == collider) // unless its the given panel,
        {
            item.panelGameObject.SetActive(true); // then enable it.
            Cursor.lockState = CursorLockMode.None; // Unlock the mouse.
            Cursor.visible = true; // Show the mouse.
            DisablePlayerUI(); // Disable the UI (e.g. crosshair)
            puzzleActive = true; // Disable movement. (Because player movement requires puzzleActive and invActive to also be false
        }
    }
}

void Update ()
{
    if (Input.GetButtonDown("Use") && puzzleActive && !invActive && // If you want to use a console and the puzzle is already active
        !puzzleList.Where(p => p.panelGameObject.activeSelf).FirstOrDefault().panelGameObject.GetComponentInChildren<InputField>().isFocused) // Check if the currently active panel's input field is not focused.
    {
        ClosePuzzleOverlay(); // Close the puzzle.
        EnablePlayerUI(); // Enable the UI.
    }

}

public void ClosePuzzleOverlay()
{
    foreach (Puzzle item in puzzleList)
        item.panelGameObject.SetActive(false);
    Cursor.visible = false;
    Cursor.lockState = CursorLockMode.Locked;
    puzzleActive = false;
}

1 个答案:

答案 0 :(得分:1)

问题是由两个输入检查引起的。当按下F时两者都为真,因此UI正在关闭,然后在同一帧中打开。

将输入代码从UIManager中取出并保存在一个位置。然后你可以修改你的输入类是这样的:

//MouseManager Update 
if(Input.GetButtonDown("Use")){
     if(uiPuzzles.isActive)
         uiPuzzles.Hide();
    else
        uiPuzzles.Show(GetPuzzleName());
}

现在它只会在F被按下的帧上打开或关闭。