簡體   English   中英

Unity-面板被禁用,之后立即重新啟用(C#)

[英]Unity - Panel is disabled and re-enabled immediately afterwards (C#)

只需要一種方法來解決此問題。 我知道問題所在,我只需要一個可行的解決方案。

因此,基本上,我有一個名為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;
}

該問題是由兩個輸入檢查引起的。 按下F都為true,因此將UI關閉,然后在同一框架中打開。

將輸入代碼從UIManager中取出,並將其放在一個位置。 然后,您可以將輸入類重做為如下所示:

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

現在,它將僅在按下F的框架上調用打開或關閉。

暫無
暫無

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

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