设置暂停ui

新建两个sprite renderer附上开始和暂停的按键

然后挂上脚本

using UnityEngine;
using System.Collections;

public class GamePause : MonoBehaviour {

    
    void OnMouseUpAsButton()
    {
        //用GameManager里的TransformGameState方法去调用按钮功能
        GameManager._instance.TransformGameState();
    }
}

在gamemanager脚本里调用

public enum GameState//枚举两个游戏状态
{
    Running,
    Pause
}
public class GameManager : MonoBehaviour
{
    public GameState gameState=GameState.Running;//声明一个共有变量存储游戏状态,默认为运行
    public GameObject gamePuase;
    public GameObject gameContinue;

 void Start()
    {
        gameContinue.SetActive(false);
        gamePuase.SetActive(true);
    }

 public void TransformGameState()
    {
        if (gameState==GameState.Running)
        {PuaseGame();
        gameContinue.SetActive(true);
        gamePuase.SetActive(false);
        }
        else if (gameState==GameState.Pause)
        {
            ContinueGame();
            gameContinue.SetActive(false);
            gamePuase.SetActive(true);
        }
    }
    public void PuaseGame()
    {
        
        Time.timeScale = 0;//将时间暂停
        gameState=GameState.Pause;
    }

    public void ContinueGame()
    {
       
        Time.timeScale = 1;//开始时间
        gameState=GameState.Running;
    }
}

这样就完成按键切换与控制时间,特别需要注意在暂停的时候不让角色移动需要在主角移动的方法内加上

 if (isMouseDown&&GameManager._instance.gameState==GameState.Running)

 

转载于:https://www.cnblogs.com/fuperfun/p/5352324.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值