-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathColorChangeScript.cs
137 lines (129 loc) · 3.75 KB
/
ColorChangeScript.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ColorChangeScript : MonoBehaviour
{
// Start is called before the first frame update
//InspectorValues
[SerializeField] List<Color> colors;
[SerializeField] bool RunOnStart = true;
[SerializeField] bool RunOnce = false;
[SerializeField] [Range(0f,2f)]float speed = 0.3f;
Color color_present;
Color color_target;
bool changing_color = true;
float treshhold = 0.1f;
int component_has;
Component component_color;
bool is_running = false;
bool beginnig = true;
void Start()
{
component_has = ComponentHas();
ControlErrors();
if (RunOnStart)
Run();
}
public void Run()
{
if (!is_running)
{
beginnig = true;
is_running = true;
Change_Color();
StartCoroutine(Changing_Color());
}
}
public void Stop()
{
changing_color = false;
is_running = false;
}
IEnumerator Changing_Color()//Renk değişim işlemi
{
changing_color = true;
while (changing_color)
{
if (Mathf.Abs(color_present.r - color_target.r) + Mathf.Abs(color_present.g - color_target.g) + Mathf.Abs(color_present.b - color_target.b) > treshhold)
{
color_present = Color.Lerp(color_present, color_target, speed * Time.deltaTime);
}
else
{
if (RunOnce)
{
break;
}
Change_Color();
}
Apply_Color();
yield return new WaitForSeconds(Time.deltaTime);
}
Stop();
}
void Change_Color()//Hedef rengi güncelle
{
color_present = beginnig ? colors[0] : color_target;
beginnig = false;
color_target = color_present;
while (color_target == color_present)
{
if (colors.Count <= 1)
{
throw new System.Exception("colors dizisi değeri 1'den büyük olmalıdır");
return;
}
color_target = colors[Random.Range(0, colors.Count)];
}
}
#region Component Type
void Apply_Color()//Rengi Uygula
{
switch (component_has)
{
case 0:
(component_color as Image).color = color_present;
break;
case 1:
(component_color as SpriteRenderer).color = color_present;
break;
case 2:
(component_color as Camera).backgroundColor = color_present;
break;
default:
break;
}
}
int ComponentHas()
{
if (GetComponent<Image>() != null)
{
component_color = GetComponent<Image>();
return 0;
}
else if (GetComponent<SpriteRenderer>() != null)
{
component_color = GetComponent<SpriteRenderer>();
return 1;
}
else if (GetComponent<Camera>() != null)
{
component_color = GetComponent<Camera>();
return 2;
}
return -1;
}
void ControlErrors()
{
if (component_has == -1)
{
throw new System.Exception("Need Component SpriteRenderer , Image or Camera");
}
if (colors[0].a == 0f)
{
// throw new System.Exception("First Color musn't %100 transparent. Change the first color 'a' value.");
}
}
#endregion
}