我怎样才能在 unity 4.6 中制作一个停止计时器的暂停菜单

How can i make a pause menu in unity which stops a timer in unity 4.6

如何在 unity 4.6 中制作一个停止计时器的暂停菜单? 这是脚本:

using UnityEngine;
using System.Collections;

public class pauseMenu : MonoBehaviour 
{
    public GUISkin myskin;

private Rect windowRect;
private bool paused = false , waited = true;

private void Start()
{
    windowRect = new Rect(Screen.width / 2 - 100, Screen.height / 2 - 100, 200, 200);
}

private void waiting()
{
    waited = true;
}

private void Update()
{
    if (waited)
        if (Input.GetKey(KeyCode.Escape) || Input.GetKey(KeyCode.P))
        {
            if (paused)
                paused = false;
            else
                paused = true;

            waited = false;
            Invoke("waiting",0.3f);
        }
}

private void OnGUI()
{
    if (paused)
        windowRect = GUI.Window(0, windowRect, windowFunc, "Pause Menu");
}

private void windowFunc(int id)
{
    if (GUILayout.Button("Resume"))
    {
        paused = false;
    }
    if (GUILayout.Button ("Restart")) 
    {
        Application.LoadLevel("LEVEL 1");
    }
    if (GUILayout.Button("Quit"))
    {
        Application.LoadLevel("Main Menu");
    }
}
}

编辑:检查您的实际项目后

问题

您正在使用两种不同的 class 来创建计时器,一种是您在问题中提到的 "pauseMenu",另一种是 "Timer",就是这个

#pragma strict

var timer : float = 10.0;

function Update()
{
    timer -= Time.deltaTime;

    if(timer <= 0)
    {
        timer = 0;
        //DO SOMETHING EPIC!
    }
}


function OnGUI()
{
    GUI.Box(new Rect(1, 2, 70, 30), "Time: " + timer.ToString("0"));
}

现在请注意第一个 class 是用 C# 编写的,另一个 class 在 javascript 中。

有两种解决方案可以实现您的目标

1.

同时使用这两个脚本非常棘手,您需要将第一个脚本保存在插件文件夹中,因为 unity 的执行顺序和从 javascript 访问 "time" 变量。或

2.

您可以删除您的 javascript "timer" 并使用以下代码更新您的 C# class "pauseMenu"。

*************************** 新代码 **************** *******

using UnityEngine;
using System.Collections;

public class pauseMenu : MonoBehaviour 
{
    public GUISkin myskin;

    private Rect windowRect;
    private bool paused = false , waited = true;
    private float time = 60f;

    private void Start()
    {
        windowRect = new Rect(Screen.width / 2 - 100, Screen.height / 2 - 100, 200, 200);
    }

    private void waiting()
    {
        waited = true;
    }

    private void Update()
    {
        if (waited)
            if (Input.GetKey(KeyCode.Escape) || Input.GetKey(KeyCode.P))
        {
            if (paused)
                paused = false;
            else
                paused = true;

            waited = false;
            Invoke("waiting",0.3f);
        }
        if(!paused)
            if(time>0)
                time -= Time.deltaTime;
    }

    private void OnGUI()
    {
        if (paused)
            windowRect = GUI.Window(0, windowRect, windowFunc, "Pause Menu");
        GUI.Box(new Rect(1, 2, 70, 30), "Time: " + time.ToString("0"));
    }

    private void windowFunc(int id)
    {
        if (GUILayout.Button("Resume"))
        {
            paused = false;
        }
        if (GUILayout.Button ("Restart")) 
        {
            Application.LoadLevel("LEVEL 1");
        }
        if (GUILayout.Button("Quit"))
        {
            Application.LoadLevel("Main Menu");
        }
    }
}

------------------------ 之前的代码-------------------- ----------

像这样

using UnityEngine;
using System.Collections;

public class pauseMenu : MonoBehaviour 
{
    public GUISkin myskin;

    private Rect windowRect;
    private bool paused = true , waited = true;
    private float time = 0;

    private void Start()
    {
        windowRect = new Rect(Screen.width / 2 - 100, Screen.height / 2 - 100, 200, 200);
    }

    private void waiting()
    {
        waited = true;
    }

    private void Update()
    {
        if (waited)
            if (Input.GetKey(KeyCode.Escape) || Input.GetKey(KeyCode.P))
        {
            if (paused)
                paused = false;
            else
                paused = true;

            waited = false;
            Invoke("waiting",0.3f);
        }
        if(paused)time += Time.deltaTime;
        Debug.Log (time);
    }

    private void OnGUI()
    {
        if (paused)
            windowRect = GUI.Window(0, windowRect, windowFunc, "Pause Menu");
    }

    private void windowFunc(int id)
    {
        if (GUILayout.Button("Resume"))
        {
            paused = false;
        }
        if (GUILayout.Button ("Restart")) 
        {
            Application.LoadLevel("LEVEL 1");
        }
        if (GUILayout.Button("Quit"))
        {
            Application.LoadLevel("Main Menu");
        }
    }
}