C# Unity-面板被禁用,然后立即重新启用(C)
只是需要一个方法来解决这个问题。我知道问题是什么,我只需要一个可行的解决方案 所以基本上我有一个叫做MouseManager的脚本,它检测你是否在看一个特定的游戏对象,如果你按f键,它会告诉另一个叫做UIManager的脚本,打开游戏对象的面板。我有一个游戏,你可以在控制台上按f键,它会启用一个带有谜题的UI 基本上,我希望用户能够按f或esc并让它退出面板,但是如果你按f,它会禁用面板,但当你仍在观看游戏对象时,会立即重新启用面板,并且两个检查都在更新中 我需要一个解决办法,如果有人知道,请评论,因为这个问题是我的神经:p 谢谢 编辑:我说的控制台是指有一个看起来像控制台的游戏对象 编辑2:这是代码,我会在其中添加一些注释,这样你就可以理解每件事的作用。我将很快重构它,因为它很凌乱C# Unity-面板被禁用,然后立即重新启用(C),c#,unity3d,C#,Unity3d,只是需要一个方法来解决这个问题。我知道问题是什么,我只需要一个可行的解决方案 所以基本上我有一个叫做MouseManager的脚本,它检测你是否在看一个特定的游戏对象,如果你按f键,它会告诉另一个叫做UIManager的脚本,打开游戏对象的面板。我有一个游戏,你可以在控制台上按f键,它会启用一个带有谜题的UI 基本上,我希望用户能够按f或esc并让它退出面板,但是如果你按f,它会禁用面板,但当你仍在观看游戏对象时,会立即重新启用面板,并且两个检查都在更新中 我需要一个解决办法,如果有人知道,请评
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时,这两个值都为真,因此UI将在同一帧中关闭然后打开 将输入代码从UIManager中取出并保存在一个地方。然后,您可以将输入类修改为如下所示:
//MouseManager Update
if(Input.GetButtonDown("Use")){
if(uiPuzzles.isActive)
uiPuzzles.Hide();
else
uiPuzzles.Show(GetPuzzleName());
}
现在,它只会在按下F的框架上调用open或close。请发布您的代码完成。抱歉花了这么长时间,不得不添加一些澄清意见