두 개의 조명 구성 요소가 있습니다. 먼저 조명을 찾고 조명을 꺼야합니다. 그런 다음 일부 개체 배율을 변경하고 조명이 흐려지는 동안 개체 배율 지속 시간을 사용할 때이를 활성화하려고합니다.어떻게 조명 부품을 어둡게 할 수 있습니까?
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;
}
}
}
를 ChangeScale 내가 DimLights 스크립트있어서 LightDim를 사용하여 광을 어둡게 할 scaleOverTime 방법 안에 원하는 제 스크립트.
빛이 어둡게 될 때와 밝아 질 때를 언급하지 않았습니다. – Programmer
@Programmer 오른쪽, 스케일 업하면 밝아지고 스케일을 줄이면 끄기 전까지 어두워 져야합니다. –
배열의 모든 표시등이이 작업을 수행해야합니까? – Programmer