2011-07-22 38 views
1

我在Unity3D中做了很多場景的項目。 在第一個場景中有按鈕,每個按鈕都會在點擊時播放一個場景。例如,如果玩家點擊「顯示氣球」按鈕,則會打開名爲氣球(包含氣球對象及其動畫)的場景。在Unity3d中打開並播放一個場景

我該如何使用JavaScript代碼來做到這一點?

回答

2

請參閱Application.LoadLevel(...)

從文檔:

[...]。在加載關卡之前,您必須將其添加到遊戲中使用的關卡 的列表中。 [...]

// Loads the level with index 0 
Application.LoadLevel (0); 

// Load the level named "HighScore". 
Application.LoadLevel ("HighScore"); 
1

LoadLevel的INT版本需要從構建設置的ID。 你也可以調用字符串版本,它從項目視圖中獲取場景名稱(儘管它仍然需要在構建設置中才能找到)

轉到文件 - >構建設置並將場景拖到那裏然後使用以下。

Application.LoadLevel("Ballon"); 

如果要重新加載當前水平,則可以使用

Application.LoadLevel (Application.loadedLevel); 

你會的,當然,需要確保手動復位或銷燬由場景中創建任何東西,標記爲DontDestroyOnLoad。

是的,在構建中以任意順序放置關卡的唯一方法是從構建設置菜單。您可以通過指定其名稱或構建順序來跳轉到哪個級別,但是如果您想在構建順序中按順序插入級別,並且只想從一個級別跳到下一個級別(即Menu-> Level1- > Level2->殘局),你可以使用你所要做的

Application.LoadLevel (Application.loadedLevel + 1); 
2

的第一件事是所有你想要的水平加入到構建設置屬性。 轉到Unity或文件菜單,構建設置,然後將您的場景拖放到滾動窗口中。然後它會爲它們分配一個邏輯號碼(或者您可以通過名稱引用)。

Application.LoadLevel(0); // this loads the first level in the list 
Application.LoadLevel("nameoflevel"); //does the same thing numerically except by name 
Application.LoadLevel(Application.loadedLevel); //reloads current level 
Application.LoadLevel(Application.loadedLevel + 1); //loads the next level in order 
Application.LoadLevel(Application.loadedLevel - 1); //loads the prior level in order 
Application.LoadLevel(Application.levelCount - 1); //loads the last level in list 
相關問題