简体   繁体   English

如何在Unity中制作暂停菜单以在Unity 4.6中停止计时器

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

How can i make a pause menu in unity which stops a timer in unity 4.6 ? 如何在unity中创建一个暂停菜单,以在unity 4.6中停止计时器? Here is the script : 这是脚本:

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");
    }
}
}

Edit : After checking your actual project 编辑:检查您的实际项目后

Problem 问题

You are using two different classes for creating timer, one is "pauseMenu" which you have mentioned in your question and other one is "Timer" which is this one 您正在使用两种不同的类来创建计时器,一种是您在问题中提到的“ 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"));
}

Now please note first class is written in C# and the other class is in javascript. 现在请注意,第一堂课是用C#编写的,另一堂课是用javascript编写的。

There are two solution to achieve you goal 有两种解决方案可以实现您的目标

1. 1。

Using both script together is very tricky, you need to keep your first script in plugin folder, because of unity's execution order and access "time" variable from javascript. 同时使用这两个脚本非常棘手,由于统一的执行顺序,您需要将第一个脚本保存在plugin文件夹中,并从javascript访问“ time”变量。 or 要么

2. 2。

You can delete your javascript "timer" and update your C# class "pauseMenu" with following code. 您可以使用以下代码删除javascript“计时器”并更新C#类“ pauseMenu”。

*************************** New Code *********************** ***************************新代码********************** **

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");
        }
    }
}

------------------------ Previous code ----------------------------- ------------------------先前的代码------------------------ -----

Like this 像这样

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");
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM