如何调暗光组件?

时间:2017-12-25 11:31:34

标签: c# unity3d unity5

我有两个灯组件。首先,我找到了两个灯并禁用它们。 然后我想在更改某些对象比例时使用它们,并使用对象缩放持续时间为灯光调暗。

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

public class DimLights : MonoBehaviour
{
    //Lights Change
    public Light[] lightsToDim = null;
    public float maxTime;

    private GameObject[] myLights;
    private float mEndTime = 0;
    private float mStartTime = 0;

    private void Start()
    {
        myLights = GameObject.FindGameObjectsWithTag("Light");
        mStartTime = Time.time;
        mEndTime = mStartTime + maxTime;
        LightsState(false);
    }

    public void LightsState(bool state)
    {
        foreach (GameObject go in myLights)
        {
            go.GetComponent<Light>().enabled = state;
        }
    }

    public void LightDim()
    {
        foreach (Light light in lightsToDim)
        {
            light.intensity = Mathf.InverseLerp(mStartTime, mEndTime, Time.time);
        }
    }
}

第二个脚本是缩放某个对象:

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

public class ChangeScale : MonoBehaviour
{
    //Scaling change
    public GameObject objectToScale;
    public float duration = 1f;
    public Vector3 minSize;
    public Vector3 maxSize;

    private bool scaleUp = false;
    private Coroutine scaleCoroutine;

    //Colors change
    public Color startColor;
    public Color endColor;
    public float colorDuration; // duration in seconds

    private void Start()
    {
        startColor = GetComponent<Renderer>().material.color;
        endColor = Color.green;
        objectToScale.transform.localScale = minSize;
    }

    // Use this for initialization
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.F))
        {
            //Flip the scale direction when F key is pressed
            scaleUp = !scaleUp;

            //Stop old coroutine
            if (scaleCoroutine != null)
                StopCoroutine(scaleCoroutine);

            //Scale  up
            if (scaleUp)
            {
                //Start new coroutine and scale up within 5 seconds and return the coroutine reference
                scaleCoroutine = StartCoroutine(scaleOverTime(objectToScale, maxSize, duration));
            }

            //Scale Down
            else
            {
                //Start new coroutine and scale up within 5 seconds and return the coroutine reference
                scaleCoroutine = StartCoroutine(scaleOverTime(objectToScale, minSize, duration));
            }
        }

        if (Input.GetKeyDown(KeyCode.C))
        {
            StartCoroutine(ChangeColor());
        }
    }

    IEnumerator scaleOverTime(GameObject targetObj, Vector3 toScale, float duration)
    {
        float counter = 0;

        //Get the current scale of the object to be scaled
        Vector3 startScaleSize = targetObj.transform.localScale;

        while (counter < duration)
        {
            counter += Time.deltaTime;
            targetObj.transform.localScale = Vector3.Lerp(startScaleSize, toScale, counter / duration);
            yield return null;
        }
    }

    IEnumerator ChangeColor()
    {
        float t = 0;

        while (t < colorDuration)
        {
            t += Time.deltaTime;
            GetComponent<Renderer>().material.color = Color.Lerp(startColor, endColor, t / colorDuration);
            yield return null;
        }
    }
}

在第二个脚本中,我想在scaleOverTime方法中使用DimLights脚本中的LightDim方法调暗光线。

1 个答案:

答案 0 :(得分:2)

它并不复杂。您可以通过复制它来更改scaleOverTime函数来处理光照,从中创建新函数。唯一要改变的是Vector3.Lerp函数到Mathf.Lerp函数以及targetObj.transform.localScaletargetObj.intensity

一个简单的Light Dim功能:

IEnumerator dimLightOverTime(Light targetObj, float toIntensity, float duration)
{
    float counter = 0;

    //Get the current intensity of the Light 
    float startIntensity = targetObj.intensity;

    while (counter < duration)
    {
        counter += Time.deltaTime;
        targetObj.intensity = Mathf.Lerp(startIntensity, toIntensity, counter / duration);
        yield return null;
    }
}

不幸的是,您正在使用数组,因此应该使用一个数组:

IEnumerator dimLightOverTime(Light[] targetObj, float toIntensity, float duration)
{
    float counter = 0;
    //Get the current intensity of the Light 
    float[] startIntensity = new float[targetObj.Length];
    for (int i = 0; i < targetObj.Length; i++)
    {
        startIntensity[i] = targetObj[i].intensity;
    }

    while (counter < duration)
    {
        counter += Time.deltaTime;

        for (int i = 0; i < targetObj.Length; i++)
        {
            targetObj[i].intensity = Mathf.Lerp(startIntensity[i], toIntensity, counter / duration);
        }
        yield return null;
    }
}

这可以防止必须为每个Light启动新协程并节省一些时间。

新的Update功能:

public Light[] lightsToDim = null;
private Coroutine lightCoroutine;

// Use this for initialization
void Update()
{
    if (Input.GetKeyDown(KeyCode.F))
    {
        //Flip the scale direction when F key is pressed
        scaleUp = !scaleUp;

        //Stop old coroutine
        if (scaleCoroutine != null)
            StopCoroutine(scaleCoroutine);

        if (lightCoroutine != null)
            StopCoroutine(lightCoroutine);


        //Scale  up
        if (scaleUp)
        {
            //Start new coroutine and scale up within 5 seconds and return the coroutine reference
            scaleCoroutine = StartCoroutine(scaleOverTime(objectToScale, maxSize, duration));
            lightCoroutine = StartCoroutine(dimLightOverTime(lightsToDim, 1, duration)); ;
        }

        //Scale Down
        else
        {
            //Start new coroutine and scale up within 5 seconds and return the coroutine reference
            scaleCoroutine = StartCoroutine(scaleOverTime(objectToScale, minSize, duration));
            lightCoroutine = StartCoroutine(dimLightOverTime(lightsToDim, 0, duration)); ;
        }
    }
}

注意新变量&#34; lightCoroutine&#34;。用于存储旧协程,就像我们为scaleCoroutine所做的那样。