GetKeyDown似乎无法正常工作。 Unity C#

时间:2018-01-10 18:00:09

标签: c# unity3d

在工作中制作一个小原型并且已经关注如何做UI的一些教程。一切似乎都在起作用,除了击中" space"在玩游戏时不会暂停游戏。我添加了一些调试日志来尝试捕获它破坏的位置,但是当按下空格键时我没有得到日志输出。我认为这意味着在它应该寻找GetKeyDown之前或之处。

在编辑器中,我有“恢复”按钮和“停顿”#34;所有使用标记" ShowOnPause"设置的文本。如果我在启动功能中注释掉hidePaused,它们会正确显示,所以我不认为它在编辑器中出错。

如果有人看到其他任何东西,我可能会做得更好,请分享。还在努力学习这些东西。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class UIManager : MonoBehaviour 
{
// Array of gameObjects for the Pause Menu
GameObject[] pauseObjects;

void Start () 
{
    //Game is running at start
    Time.timeScale = 1;
    // Assign all objects with the ShowOnPause to pauseObjects
    pauseObjects = GameObject.FindGameObjectsWithTag("ShowOnPause");            
    //Hide all pause UI at start
    hidePaused();
    Debug.Log("UI Hidden");

}

void Update () 
{
    // If the space key is pressed 
    if(Input.GetKeyDown(KeyCode.Space))
    {
        Debug.Log("Key Pressed");   

        // and if the game is running
        if(Time.timeScale == 1)
        {
            // Pause the game and show Pause Menu
            Time.timeScale = 0;
            showPaused();
            Debug.Log("Paused");
        } 
        // or if the game is already paused
        else if (Time.timeScale == 0)
        {
            // Resume the game and hide the Pause Menu
            Time.timeScale = 1;
            hidePaused();
            Debug.Log("UnPaused");
        }
    }
}

// The function for activating gameobjects in the Pause Menu array
public void showPaused()
{
    foreach(GameObject g in pauseObjects)
    {
        g.SetActive(true);
        Debug.Log("Showing UI");
    }
}

// The Function for deactivating the gameobjects in the Pause Menu array
public void hidePaused()
{
    foreach(GameObject g in pauseObjects)
    {
        g.SetActive(false);
        Debug.Log("Hiding UI");
    }
}

// Function for the Resume button in the Pause Menu
public void Resume()
{
    if(Time.timeScale == 1)
    {
        Time.timeScale = 0;
        showPaused();
    } 
    else if (Time.timeScale == 0)
    {
        Time.timeScale = 1;
        hidePaused();
    }
}           

}

1 个答案:

答案 0 :(得分:0)

试试这个

if(Input.GetKeyDown("space"))