[unity]在退出时添加提示

该功能说明:在游戏中点击游戏关闭按钮(或者退出按钮)时,往往会有需求期望在此时,添加退出提示(比如是否确认要退出)。

代码环境:

  unity:version5.3.4.f1

  IDE:Microsoft visual studio Community 2015

 

相关代码说明:

MonoBehaviour的回调一定程度上反映着运行流程。
OnApplicationQuit() ,下述引用下官方文档说明:
Sent to all game objects before the application is quit.

In the editor this is called when the user stops playmode. In the web player it is called when the web view is closed.

 
功能实现:
    bool m_AppQuit = false;
    void OnApplicationQuit()
    {
        if (m_AppQuit  == false)
        {
            //do something                    
            Application.CancelQuit();        //此时需要展开提示框如此之类,所以需要暂时取消退出流程。
 
            m_AppQuit  = true;
        }
    }
额外说明:上述引用的官方文档也提出了,OnApplicationQuit()在编辑模式时,是在停止播放时调用的。所以该效果在编辑模式并不能完全展现,因此建议实际测试时还是需要build出来再测试。
posted @ 2016-06-28 11:53  lanyuan  阅读(780)  评论(0编辑  收藏  举报