Unity3D——游戏开始与暂停

暂停:
Time . timeScale   =   0 ;
开始:
Time . timeScale   =  1 ;

例如:
using   UnityEngine ;
using   System . Collections ;

public   class   Script_Menu   :   MonoBehaviour   {
   
    public   bool   IsGamePaused ;
   
    void   Start ()
    {
        PauseGame ();
    }
   
    void   Update ()
    {
        if  ( Input . GetKey ( KeyCode . Escape ))
        {
            PauseGame ();
        }
    }
    void   OnGUI ()
    {
        if  ( ! IsGamePaused )
            return ;
        ///自动布局,按照区域
        GUILayout . BeginArea ( new    Rect (( Screen . width   -   100 ) / 2 ,  ( Screen . height   -   200 ) / 2 ,   100 ,   200 ));
        ///横向
        GUILayout . BeginVertical ();
        if  ( IsGamePaused )
        {
            if ( GUILayout . Button ( "开始游戏" ,   GUILayout . Height ( 50 )))
            {
                StartGame ();
                //enabled = false;
            }
        }       
        if ( GUILayout . Button ( "退出游戏" ,   GUILayout . Height ( 50 )))
        {
            //在Android上可以,IOS上没试
            //Debug.Log("Exit");
            Application . Quit ();
        }
        GUILayout . Button ( "关于游戏" ,   GUILayout . Height ( 50 ));
        GUILayout . EndVertical ();
        GUILayout . EndArea ();
    }
   
    void   StartGame ()
    {
        IsGamePaused   =   false ;
        Time . timeScale   =   1 ;
        //Debug.Log("Start Game" + Time.fixedTime);
    }
   
    void   PauseGame ()
    {
        IsGamePaused   =   true ;
        Time . timeScale   =   0 ;
        //Debug.Log("Pause Game");
    }
}

http://blog.sina.com.cn/s/blog_c3a4360501019wnl.html


   一提到游戏暂停,很多人会想到 Time.timeScale = 0; 这种方法,但 Time.timeScale 只是能暂停部分东西。如果在 update 函数中持续改变一个物体的位置,这种位置改变貌似是不会受到暂停影响的。比如 transform.position = transform.position+transform.TransformDirection(Vector3(0,0,throwForce));
Time.timeScale = 0 的时候这个东西仍然在动。

    CocoaChina 会员“123探花”的解决方法是:把使用Time.timeScale = 0; 功能的函数写在 FixedUpdate() , 当使用 Time.timeScale = 0 时项目中所有 FixedUpdate() 将不被调用,以及所有与时间有关的函数。  在 update 通过一个布尔值去控制暂停和恢复。
如果您设置 Time.timeScale 为 0,但你仍然需要做一些处理(也就是说动画暂停菜单的飞入),可以使用 Time.realtimeSinceStartup 不受 Time.timeScale 影响。

    另外您也可以参考一下 Unity Answer 上提到的方法

要暂停游戏时,为所有对象调用 OnPauseGame 函数:
 Object[] objects = FindObjectsOfType (typeof(GameObject));
foreach (GameObject go in objects) {
        go.SendMessage ("OnPauseGame", SendMessageOptions.DontRequireReceiver);
}

从暂停状态恢复时,
A basic script with movement in the Update() could have something like this:
protected bool paused;

void OnPauseGame ()
{
        paused = true;
}

void OnResumeGame ()
{
        paused = false;
}

void Update ()
{
        if (!paused) {
                // do movement
        }
}


http://blog.csdn.net/tammy520/article/details/8959394
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值