【2d游戏开发】实现昼夜系统

前言

本次文章将编写unity如何根据时间实现昼夜系统,也因此,本文章需要依托于前面文章的时间系统功能。
同样,这里只把重要的部分编写出来,具体的实现过程请到b站搜索本人的2d游戏开发-unity实现系列视频教程。

功能说明

本次的昼夜系统依托于时间系统,前面我已经将24分钟设置为游戏的一天,那么这次就根据不同的时间显示不同的亮度.
本次的不同时间亮度设置如下:

            21:00-05:00 保持最暗 
            06:01-12:00 逐渐变亮 
            12:01-15:00 保持最亮 
            15:01-20:59 降低亮度 

实现原理

本次昼夜系统实现的原理如下:
在unity中,调节光线的RGB会导致光线的明暗程度不同,于是,本次文章依据于此进行制造。

实现过程

材质

要实现这个效果,需要给受光线影响的物体添加一个材质
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

光源

新建一个光源,我这里创建的是直射光源,然后给这个光源添加我下面的代码组件
在这里插入图片描述
在这里插入图片描述

代码

具体的实现代码如下

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class lightControll : MonoBehaviour
{
    // Start is called before the first frame update
    // 时间系统
    public GameObject timeSystem;
    // 获取光源
    private Light Light;
    void Start()
    {
        Light = GetComponent<Light>();
    }

    // Update is called once per frame
    void Update()
    {
        CircadianSystem();
    }

    // 昼夜系统



    void CircadianSystem() {
        /**
            21:00-05:00 保持最暗 RGB(0,0,0)
            06:01-12:00 逐渐变亮 RGB(1~254,1~254,1~254)
            12:01-15:00 保持最亮 RGB(255,255,255)
            15:01-20:59 降低亮度 RGB(254~1,254~1,254~1)
            // 根据时间长度决定白天变亮和晚上变暗的速度
            06:01-12:00 6小时 360秒 254/360 = 0.705
        15:01-20:59 6小时 360秒 254/360 = 0.705
        */
        // 实例化时间系统脚本
        TimeSystemContoller timeSystemContoller = timeSystem.GetComponent<TimeSystemContoller>();
        //
        int minutes = timeSystemContoller.showMinute;
        int seconds = timeSystemContoller.showSeconds;
        // 21:00 - 05:00
        if (minutes >=21 && minutes <=23) {
            Light.color = new Color(0/255f,0/255f,0/255f);
        }
        if (minutes>=0 && minutes <=4) {
            Light.color = new Color(0 / 255f, 0 / 255f, 0 / 255f);
        }
        // 06:01-12:00
        if (minutes>=5 && minutes <=11) {
            //当前经过了多长,在这个时间段
            int totalSeconds = (minutes - 5) * 60 + seconds;
            // 经过多少RGB
            float groundRGB = totalSeconds * (float)0.705;
            // 当前的RGB是多少
            float currentRGB = 1 + groundRGB;
            Light.color = new Color(currentRGB/255f, currentRGB / 255f, currentRGB / 255f);
        }
        // 12:01-15:00
        if (minutes>=12 && minutes <=14) {
            Light.color = new Color(255/255f,255/255f,255/255f);
        }
        // 15:01-20:59
        if (minutes>=15 && minutes<=20) {
            // 当前经过多长时间
            int totalSconds = (minutes - 15) * 60 + seconds;
            // 经过多少RGB
            float groundRGB = totalSconds * (float)0.705;
            // 当前RGB
            float currentRGB = 254 - groundRGB;
            // 
            Light.color = new Color(currentRGB / 255f, currentRGB / 255f, currentRGB / 255f);
        }
    }
}

效果截图

在这里插入图片描述
在这里插入图片描述

结语

以上为我实现昼夜系统的过程,如需要更详细的教学,请到b站搜索本人的2d游戏开发-unity实现系列教学视频。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值