Tanoda
LightChanger.cs
Go to the documentation of this file.
1using System.Collections;
2using System.Collections.Generic;
3using UnityEngine;
5
6[RequireComponent(typeof(Light))]
7public class LightChanger : MonoBehaviour
8{
9 public UnityEvent OnStart;
10
11 private Coroutine started;
12 private new Light light;
13 private Color from, to;
14
15 private void Start()
16 {
17 light = GetComponent<Light>();
18 }
19
20 public void ChangeToGreen()
21 {
22 LerpColor(Color.green);
23 }
24
25 public void ChangeToRed()
26 {
27 LerpColor(Color.red);
28 }
29
30
31 public void LerpColor(Color color)
32 {
33 from = light.color;
34 to = color;
35 StartCoroutine(DoWork());
36 }
37
38 private IEnumerator DoWork()
39 {
40 for (float i = 0; i <= 1; i += 0.05f)
41 {
42 light.color = Color.Lerp(@from, to, i);
43 yield return new WaitForEndOfFrame();
44 }
45 }
46
47 public void StartClicked()
48 {
49 if (started != null)
50 return;
51
52 started = StartCoroutine(Started());
53 }
54
55 private IEnumerator Started()
56 {
57 for (float i = 1; i <= 3; i += 0.2f)
58 {
59 light.intensity = i;
60 yield return new WaitForEndOfFrame();
61 }
62 for (float i = 3; i > 0; i -= 0.2f)
63 {
64 light.intensity = i;
65 yield return new WaitForEndOfFrame();
66 }
67 OnStart.Invoke();
68 light.intensity = 10;
69 light.color = Color.red;
70
71 started = null;
72 }
73}
UnityEngine.Color Color
Definition: TestScript.cs:32
void ChangeToRed()
Definition: LightChanger.cs:25
void StartClicked()
Definition: LightChanger.cs:47
UnityEvent OnStart
Definition: LightChanger.cs:9
void LerpColor(Color color)
Definition: LightChanger.cs:31
void ChangeToGreen()
Definition: LightChanger.cs:20