1、新建兩個場景,一個命名為start,另一個命名為game,如下圖
2、打開start,創(chuàng)建一個平面和一個按鈕(右鍵,UI,Button),平面繞X軸旋轉(zhuǎn)-90度,按鈕的text設(shè)置為開始游戲,把按鈕拖到場景中合適的位置,如下圖
3、編寫代碼start,把它賦給Canvas,注意是賦給Canvas,不是給按鈕,代碼如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class start : MonoBehaviour
{
// Start is called before the first frame update
public void OnLoginButtonClick()
{
SceneManager.LoadScene(1);
}
}
4、點擊button往下滑找到OnClick,點擊加號,把canvas拖到左邊,另一個參數(shù)選擇start.OnLoinnButtonClick,如下圖
5、打開場景game,場景也任意設(shè)置,這里以創(chuàng)建了一個地形為例,再創(chuàng)建兩個按鈕,分別命名為重新開始和退出游戲,如下圖
6、寫代碼restart和quit,并把他們賦給canvas, 代碼如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class restart : MonoBehaviour
{
// Start is called before the first frame update
public void OnLoginButtonClick()
{
SceneManager.LoadScene(0);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class quit : MonoBehaviour
{
// Start is called before the first frame update
public void OnLoginButtonClick()
{
Application.Quit();
}
}
7、對按鈕的OnClick進(jìn)行設(shè)置
?文章來源:http://www.zghlxwxcb.cn/news/detail-504462.html
8,場景添加,file,build?settings,Add open scenes?把兩個場景都選上,注意前后順序,然后關(guān)閉頁面,點擊運行就可以實現(xiàn)頁面跳轉(zhuǎn)了。文章來源地址http://www.zghlxwxcb.cn/news/detail-504462.html
到了這里,關(guān)于unity場景轉(zhuǎn)換(實現(xiàn)開始游戲和返回游戲)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!