如何在统一中制作暂停菜单,以便在单位中停止计时器4.6

时间:2015-07-26 11:37:33

标签: c# unity3d menu

如何制作统一暂停菜单,以便在单位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");
    }
}
}

1 个答案:

答案 0 :(得分:0)

编辑:检查实际项目后

<强>问题

您正在使用两个不同的类来创建计时器,一个是您在问题中提到的“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

将两个脚本放在一起非常棘手,你需要将你的第一个脚本保存在插件文件夹中,因为统一的执行顺序和访问来自javascript的“时间”变量。或

<强> 2

您可以删除您的javascript“计时器”,并使用以下代码更新您的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");
        }
    }
}