2015-06-19 92 views
0

我一直在處理暫停菜單的腳本,並且在開始執行下一行之前我不知道如何停止腳本 這是代碼,我問這是因爲多次執行「if」,因爲檢測到我仍在按取消按鈕。 (I使用C#和統一5工作) 由於如何在執行其餘命令之前停止一秒鐘

using UnityEngine; 
using System.Collections; 

public class MenuPausa : MonoBehaviour { 

public GameObject menuPausa; 

private bool pausaMenu = false; 

// Use this for initialization 
void Start() { 

} 

// Update is called once per frame 
void Update() { 
    if (Input.GetButtonDown("Cancel") || pausaMenu == false) { 
     menuPausa.SetActive (true); 
     Time.timeScale = 0; 
     WaitForSeconds(1); 
     pausaMenu = true; 
    } else { 
     menuPausa.SetActive (false); 
     Time.timeScale = 1; 
     Invoke("waiting",0.3f); 
    } 
} 

}

+0

http://answers.unity3d.com/questions/379440/a-simple-wait-function-without-coroutine-c.html – Benj

+0

也許Thread.Sleep()。雖然,我不確定這是什麼意思。你想要做某種類型的投票嗎? – PilotBob

+0

如果你想進行某種類型的輪詢,這可能會有所幫助。 http://sstut.com/csharpdotnet/javascript-timers-equivalent.php – PilotBob

回答

0

在該方法結束時,把

Thread.Sleep(1000); 

它暫停1000毫秒== 1秒執行。 這是一個簡單的解決方案嗎?或者你需要一個更復雜的計時器嗎?

+0

不工作,我想停止if方法,因此我可以保留暫停菜單 – Fran910

+0

現在我不明白你的目標。是否要保持取消按鈕被按下,但讓Update()方法只反應一次? – Isolin

+0

我想要做的是: 當我按esc設置爲主動遊戲objet,直到我可以做我的自我,但如果我使用if(Input.GetButtonDown(「取消」))'檢測就像我仍然握着esc按鈕一樣 – Fran910

1

我會有點用協同程序是這樣的:

bool paused = false; 

void Update() 
{ 
    if (paused) // exit method if paused 
     return; 

    if (Input.GetButtonDown("PauseButton")) 
     StartCoroutine(OnPause()); // pause the script here 
} 

// Loops until cancel button pressed 
// Sets pause flag 
IEnumerator OnPause() 
{ 
    paused = true; 

    while (paused == true) // infinite loop while paused 
    { 
     if (Input.GetButtonDown("Cancel")) // until the cancel button is pressed 
     { 
      paused = false; 
     } 
     yield return null; // continue processing on next frame, processing will resume in this loop until the "Cancel" button is pressed 
    } 
} 

這隻「暫停」,並恢復該單個腳本。所有其他腳本將繼續執行其更新方法。要暫停獨立於幀速率的方法(即物理),請在暫停時設置Time.timeScale = 0f,在未暫停時設置爲1f。如果需要暫停所有其他腳本,並且它們取決於幀速率(即更新方法),則使用全局暫停標誌而不是本示例中使用的本地暫停變量,並檢查每個更新方法中是否設置了該標誌,就像在這個例子中一樣。

1

如果我誤解了這一點,我表示歉意,但在我看來,您似乎在使用「取消」按鈕打開和關閉暫停菜單,並且您的腳本似乎在打開後立即關閉它,原因是仍然按下「取消」按鈕。在我看來,這個腳本也是打開和關閉菜單對象的一個​​組件。如果是這樣的話,我會建議如下:

有沒有這個腳本用於菜單本身以外的其他對象(如MenuManager對象或其他東西 - 我也將此腳本的名稱改爲MenuManger以避免混淆它與實際的菜單)將在現場保持活躍。在分配給此MenuManager的「menuPausa」屬性的場景中有一個PauseMenu對象。然後我會刪除pausaMenu變量。此外,請確保此腳本僅作爲組件添加到一個對象(MenuManager)中,否則第二個對象可能會在同一幀中更新並將菜單右移。

相關問題