光照变日夜变化系统

梦想游戏人
目录:
Unity

大概实现日夜亮度变化

using UnityEngine;
using System.Collections;

/// <summary>
/// @brief  is for weather system
/// </summary>
public class WeatherSystem : MonoBehaviour
{
    [SerializeField]
    private Light light;
    private float _current_intensity = 1.0f;


    // Use this for initialization
    void Start()
    {
        StartCoroutine(SyncLight());


    }

    // Update is called once per frame
    void Update()
    {

    }



    IEnumerator SyncLight()
    {
        yield return new WaitForSeconds(10.0f);

        for (; _current_intensity >= 0; _current_intensity -= 0.01f)
        {
            yield return new WaitForSeconds(1);
            light.GetComponent<Light>().intensity = _current_intensity;

        }

        yield return new WaitForSeconds(5.0f);

        for (_current_intensity = 0.0f; _current_intensity <= 1; _current_intensity += 0.01f)
        {
            yield return new WaitForSeconds(1);
            light.GetComponent<Light>().intensity = _current_intensity;

        }

        StartCoroutine(SyncLight());

    }


    public void OnUIDayNight(bool _is)
    {
        if (_is)
        {
           
            StopAllCoroutines();
            StartCoroutine(SyncLight());
        }
        else
        {
            StopAllCoroutines();
        }
        light.GetComponent<Light>().intensity = 1.0f;
   
    }
}
Scroll Up