【2d游戏开发】时间系统编写

前言

关于本游戏的开发已经开发了将近一两个月,虽然都是利用空余时间去进行;并且已经在b站进行发布了自己对该游戏的制作教程,本人并不是特别会unity上的操作,但是好歹也是有一些编程基础,于是才有了这个打算。
同时,因本人主体制作了一次,教程视频又制作了一次,所以在这里的话只会给出关键的代码,(真要一步一步教学的话就重新制作三遍了!!!!)
因此,有兴趣的可以b站搜索“2d游戏开发”
这一个系列的教学视频都是本人进行详细制作教学的。
ok,话不多说,现在来进行游戏的开发

游戏背景

本人这次打算以经营建造方向进行开发制作,由于该类型的游戏大都依托于时间系统,所以,最开始也是完成时间系统,当然,因为手上并没有多少素材,所以游戏的画面想精美就。。。。想想就可以了。

时间系统介绍

本次时间系统,包含涵盖时间、季节以及日期;
游戏内24分钟为一天,30天为一个季节,4个季节为一年
实效的样式效果如下图
在这里插入图片描述
具体怎么制作界面请参照本人的b站视频:2d游戏开发-unity实现xxx系列教程视频

具体c#脚本

unity内创建名称TimeSystemContoller的C#脚本
该脚本代码如下:

using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
using UnityEngine.UI;

public class TimeSystemContoller : MonoBehaviour
{
    // 时间显示 // 24分钟为一天
    public Text TimeShow;
    // 季节切换
    public Text season;
    // 节日切换
    public Text holiday;
    // 天气
    public Text weather;
    public Text day;
    // 累加时间
    public float totalSeconds =0;
    // 分钟,初始进游戏为08:00
    public int showMinute = 0;
    // 秒钟
    public int showSeconds = 0;
    // 季节切换判断,1春天,2夏天,3秋天,4冬天
    public int seasonTime = 1;
    // 日期,一个月30,一个月一个季节
    public int dayTime = 30;
    // Start is called before the first frame update
    void Start()
    {
        
    }
    private void FixedUpdate()
    {
        // 累加时间到1秒,然后重置秒钟为0(每秒钟执行一次)
        totalSeconds += Time.fixedDeltaTime;
        if (totalSeconds >=0.98) {
            
            UpdateTime();
            UpdateDayAndSeason();
            totalSeconds = 0;
        }
        
    }

    // 更新时间
    public void UpdateTime() {
        // 判断分钟是否为23
        if (showMinute == 23)
        {
            // 判断秒钟是否为59
            if (showSeconds == 59)
            {
                // 重置分钟和秒钟为0
                showMinute = 0;
                showSeconds = 0;
                //界面显示时间为凌晨
                TimeShow.text = "00:00"; 
            }
            else
            {
                // 秒钟累加
                showSeconds += 1;
                //判断秒钟位数是否为2,为1在前面加零
                if (showSeconds.ToString().Length == 1)
                {
                    string seconds = "0" + showSeconds.ToString();
                    TimeShow.text = "23:" + seconds;
                }
                else
                {
                    TimeShow.text = "23:" + showSeconds.ToString();
                }
            }
        }
        else {
           // print("分钟:"+showMinute+",秒钟:"+showSeconds);
            // 当时间不为23分钟时
            //判断秒钟是否为59秒
            if (showSeconds == 59)
            {
                // 重置秒钟为0;
                showSeconds = 0;
                // 分钟+1;
                showMinute += 1;
                // 判断分钟是否为2位,1位补0
                if (showMinute.ToString().Length == 1)
                {
                    // 将时间显示在界面
                    TimeShow.text = "0" + showMinute.ToString() + ":00";
                }
                else {
                    // 将时间显示在界面
                    TimeShow.text = showMinute.ToString() + ":00";
                }


            }
            else {
                // 当秒钟小于59
                // 秒钟++
                showSeconds += 1;
                //判断秒钟位数

                // 保存分钟
                string minutes = showMinute.ToString();
                // 保存秒钟
                string seconds = showSeconds.ToString();

                // 分钟一位的时候补零
                if (minutes.Length == 1) {
                    minutes = "0" + showMinute.ToString();
                }
                // 秒钟一位补0
                if (seconds.Length == 1) {
                    seconds = "0" + showSeconds.ToString();
                }
                // 将时间显示在界面
                TimeShow.text = minutes + ":" + seconds;
            }
        }

    }

    // 更新日期和季节
    public void UpdateDayAndSeason() {
        // 判断是否为00:00(即代表),这里代码只有当第二天的时候才会执行
        if (showMinute == 0 && showSeconds == 0 ) {
            // 判断是否是春天
            if (seasonTime == 1) {
                // 判断日期是否是30
                if (dayTime == 30)
                {
                    // 重置日期,为了调用下一个季节代码准备
                    dayTime = 0;
                    // 调整季节为夏天
                    seasonTime = 2;
                    // 更新季节显示到界面
                    ColorUtility.TryParseHtmlString("#EE3838", out Color nowColor);
                    season.text = "夏";
                    season.color = nowColor;
                    // 更新日期到界面,
                    day.text = "1日";
                }
                else {
                    // 日期++
                    dayTime += 1;
                    // 更新日期到界面
                    day.text = dayTime.ToString() + "日";
                }
            }

            // 判断是否是夏天
            if (seasonTime == 2)
            {
                // 判断日期是否是30
                if (dayTime == 30)
                {
                    // 重置日期
                    dayTime = 0;
                    // 调整季节为夏天
                    seasonTime = 3;
                    // 更新季节显示到界面
                    season.text = "秋";
                    ColorUtility.TryParseHtmlString("#CD0000", out Color nowColor);
                    season.color = nowColor;
                    // 更新日期到界面
                    day.text = "1日";
                }
                else
                {
                    // 日期++
                    dayTime += 1;
                    // 更新日期到界面
                    day.text = dayTime.ToString() + "日";
                }
            }

            // 判断是否是秋天
            if (seasonTime == 3)
            {
                // 判断日期是否是30
                if (dayTime == 30)
                {
                    // 重置日期
                    dayTime = 0;
                    // 调整季节为冬天
                    seasonTime = 4;
                    // 更新季节显示到界面
                    season.text = "冬";
                    ColorUtility.TryParseHtmlString("#FFFFFF", out Color nowColor);
                    season.color = nowColor;
                    // 更新日期到界面
                    day.text = "1日";
                }
                else
                {
                    // 日期++
                    dayTime += 1;
                    // 更新日期到界面
                    day.text = dayTime.ToString() + "日";
                }
            }

            // 判断是否是冬天
            if (seasonTime == 4)
            {
                // 判断日期是否是30
                if (dayTime == 30)
                {
                    // 重置日期
                    dayTime = 0;
                    // 调整季节为春天
                    seasonTime = 1;
                    // 更新季节显示到界面
                    season.text = "春";
                    ColorUtility.TryParseHtmlString("#5CACEE", out Color nowColor);
                    season.color = nowColor;
                    // 更新日期到界面
                    day.text = "1日";
                }
                else
                {
                    // 日期++
                    dayTime += 1;
                    // 更新日期到界面
                    day.text = dayTime.ToString() + "日";
                }
            }
        }
        // 随时更新季节
        if (seasonTime == 1)
        {
            season.text = "春";
            ColorUtility.TryParseHtmlString("#5CACEE", out Color nowColor);
            season.color = nowColor;
        }
        if (seasonTime == 2)
        {
            season.text = "夏";
            ColorUtility.TryParseHtmlString("#EE3838", out Color nowColor);
            season.color = nowColor;
        }
        if (seasonTime == 3)
        {
            season.text = "秋";
            ColorUtility.TryParseHtmlString("#CD0000", out Color nowColor);
            season.color = nowColor;
        }
        if (seasonTime == 4)
        {
            season.text = "冬";
            ColorUtility.TryParseHtmlString("#FFFFFF", out Color nowColor);
            season.color = nowColor;
        }
    }
}

天气

本次脚本未实现天气系统,后面开发到一定进度再进行详细的脚本代码说明

结语

以上未本人实现unity的游戏内的时间系统

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值