游戏的退出,重开,暂停,

游戏的退出和重开

退出和重开:建一个按钮,并在按钮上写一个脚本
在这里插入图片描述
同时在Inspector视图中,在右侧如图所示

退出:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class an : MonoBehaviour
{
    
    
    // Start is called before the first frame update
    void Start()
    {
    
    

    }

    // Update is called once per frame
    void Update()
    {
    
    

    }
    public void Click()
    {
    
    
        EditorApplication.isPlaying = false;//编辑器是否在播放模式
    }

}

重开:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor.SceneManagement;

public class an2 : MonoBehaviour
{
    
    
    // Start is called before the first frame update
    void Start()
    {
    
    
        
    }

    // Update is called once per frame
    void Update()
    {
    
    
        
    }
    public  void Click ()
    {
    
    
        EditorSceneManager.LoadScene("SampleScene");//引号里面写自己场景的名字
    }
}

专业解释:
首先要添加 using UnityEditor.SceneManagement;

using System.Collections;
using UnityEngine;
using UnityEditor.SceneManagement;

//两种情况:
void Restart_1(string SceneName)// 重新打开场景名为“SceneName”的场景
{
if (Input.GetKeyDown(KeyCode.R))//如果按下“R”经重启场景
{
EditorSceneManager.LoadScene(SceneName);
}
}

void Restart_2()
{
EditorSceneManager.LoadScene(“SceneName”);//重新打开ScnenName 场景
}

上面的仅仅在编译时可以用,但是打包时就会出现错误,因为你使用了Editor文件

下面的可以不使用Editor

using UnityEngine.SceneManagement;

SceneManager.LoadScene (0);

暂停:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class an3 : MonoBehaviour
{
    
    
    // Start is called before the first frame update
    void Start()
    {
    
    
        
    }

    // Update is called once per frame
    void Update()
    {
    
    

    }
        public void Click ()
    {
    
    
        EditorApplication.isPaused = true ;
       
    }
    
}

猜你喜欢

转载自blog.csdn.net/qq_46289420/article/details/108796966
今日推荐