簡體   English   中英

我如何調暗照明元件?

[英]How can i dim a light component?

我有兩個燈組件。 首先,我要找到兩個指示燈並禁用它們。 然后,我想在更改某些對象比例並使用對象比例持續時間使燈光變暗時啟用它們。

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方法使燈光變暗。

沒那么復雜。 通過將scaleOverTime函數復制為新函數,可以將scaleOverTime函數更改為可以工作。 唯一要更改的是將Vector3.Lerp函數更改為Mathf.Lerp函數,還將targetObj.transform.localScale更改為targetObj.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)); ;
        }
    }
}

注意新變量“ lightCoroutine ”。 就像我們對scaleCoroutine所做的那樣,它用來存儲舊的協程。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM