unity3D 计时器

using UnityEngine;
using UnityEngine.UI;

public class Timer : MonoBehaviour
{
    [SerializeField] private float totalTime;//每回合可用的总时间
    [SerializeField] private float burningTime;//燃烧时间
    [SerializeField] private float playerTime;//玩家剩余的时间,游戏一开始时候,玩家的剩余时间将会等于每回合可用的总时间

    [SerializeField] private GameObject ropeGO;//用于显示或者隐藏这个绳子
    [SerializeField] private Slider ropeSlider;//获取Slider组件,方便我们通过Slider组件中的Value属性来控制绳子的长度/进度

    [SerializeField] private int playerID;//0 or 1 区分玩家0和玩家1

    [SerializeField] private Image player00Image, player01Image;//用于显示隐藏两个玩家头像

    [SerializeField] private Text timerText;//计时器的显示

    private void Awake()
    {
        playerID = 0;//游戏一开始玩家0是先手,玩家1是可敬的对手
        player00Image.color = Color.white;//玩家0的头像显示
        player01Image.color = Color.black;//玩家1的头像呈现黑色

        ropeSlider.maxValue = burningTime;//绳子,也就是我们的进度条、滑动条,它的最大值设置为burningTime燃烧时间(最大值默认是1)
        ropeGO.gameObject.SetActive(false);//一开始绳子是不可见,隐藏的
    }

    private void Start()
    {
        playerTime = totalTime;//玩家剩余时间一开始就等于一回合的总时间
    }

    private void Update()
    {
        UpdateTimeUI();//更新计时器

        playerTime -= Time.deltaTime;//剩余时间递减
        if(playerTime <= burningTime)//当玩家时间快不够的时候
        {
            ropeGO.gameObject.SetActive(true);//绳子显示
            ropeSlider.value = playerTime;//Slide组件中Value数值设置为玩家剩余时间
        }

        if(playerTime <= 0)//如果玩家剩余时间为0以后
        {
            //Debug.Log("Time Expired. Next Turn!");
            ManualStop();//我们也可以手动主动按下,结束该回合
        }
    }

    //这个方法会在上面,和Button组件中调用
    public void ManualStop()
    {
        NextTurn();//玩家1和玩家0的切换
        ropeGO.gameObject.SetActive(false);//隐藏绳子,因为一开始并不需要
        playerTime = totalTime;//Reset Each Player Timer 重置每个玩家的剩余时间
    }

    private void NextTurn()
    {
        if (playerID == 0)//当玩家ID等于0,那么就切换为1,并且进行相应的变色
        {
            playerID = 1;
            player01Image.color = Color.white;
            player00Image.color = Color.black;
        }
        else if (playerID == 1)//当玩家ID等于1,那么就切换为0,并且进行相应的变色
        {
            playerID = 0;
            player00Image.color = Color.white;
            player01Image.color = Color.black;
        }
    }

    private void UpdateTimeUI()
    {
        //timerText.text = playerTime.ToString();//简陋版
        //timerText.text = ((int)playerTime).ToString();//强制转换Cast试试
        //timerText.text = Mathf.CeilToInt(playerTime).ToString();转换为Int类型

        //MARKER FORMAT: 00:05

        int totalSeconds = Mathf.CeilToInt(playerTime);//将玩家剩余时间,转换为Int类型

        #region
        //int secondsPart = totalSeconds % 60;//取【秒数部分】
        //int minsPart = Mathf.RoundToInt(totalSeconds / 60);//取【分钟部分】
        //timerText.text = string.Format("{0}:{1}", minsPart, secondsPart);//以这样的文字格式就行输出,花括号中的0和1代表了变量在方法中的顺序
        #endregion

        string secondsPart = (totalSeconds % 60).ToString();//int -> string //取【秒数部分】直接先转换为String类型
        string minsPart = Mathf.RoundToInt(totalSeconds / 60).ToString();//取【分钟部分】直接先转换为String类型

        //string testNameA = "A";
        //Debug.Log(testNameA.Length);//1 检测字符串长度
        //string testNameAA = "AA";
        //Debug.Log(testNameAA.Length);//2 检测字符串长度

        if (secondsPart.Length == 1)//如果【秒数部分】转换后的字符串长度为1,那么说明这个数字,需要前面加一个0
            secondsPart = "0" + secondsPart;
        if (minsPart.Length == 1)//同上
            minsPart = "0" + minsPart;

        timerText.text = string.Format("{0} : {1}", minsPart, secondsPart);//字符串格式,同89行
    }

}
  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值