我如何在unity中制作一个暂停菜单,在unity 4.6中停止计时器



如何在unity中制作暂停菜单,在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");
    }
}
}

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

你使用了两个不同的类来创建计时器,一个是你在问题中提到的"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"));
}

现在请注意第一个类是用c#编写的,另一个类是用javascript编写的。

有两个解决方案来实现您的目标

1。

使用两个脚本是非常棘手的,你需要把你的第一个脚本在插件文件夹,因为unity的执行顺序和访问"时间"变量从javascript。或者

2。

你可以删除你的javascript "timer",用下面的代码更新你的c#类"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");
        }
    }
}

最新更新