Subversion Repository Public Repository

Nextrek

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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
 *
 *	Adventure Creator
 *	by Chris Burton, 2013-2014
 *	
 *	"Highlight.cs"
 * 
 *	This script is attached to any gameObject that glows
 *	when a cursor is placed over it's associated interaction
 *	object.  These are not always the same object.
 * 
 */

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

public class Highlight : MonoBehaviour
{

	private float maxHighlight = 2f;
	private float highlight = 1f;
	private bool doHightlight = false;
	private int direction = 1;
	private float fadeStartTime;
	private float fadeTime = 0.3f;
	private bool isFlashing = false;
	
	private List<Color> originalColors = new List<Color>();
	
	
	private void Awake ()
	{
		// Go through own materials
		if (renderer)
		{
			foreach (Material material in renderer.materials)
			{
				if (material.HasProperty ("_Color"))
				{
					originalColors.Add (material.color);
				}
			}
		}
		
		// Go through any child materials
		Component[] children;
		children = GetComponentsInChildren <Renderer>();
		foreach (Renderer childRenderer in children)
		{
			foreach (Material material in childRenderer.materials)
			{
				if (material.HasProperty ("_Color"))
				{
					originalColors.Add (material.color);
				}
			}
		}

		if (guiTexture)
		{
			originalColors.Add (guiTexture.color);
		}
	}
	
	
	private void FixedUpdate ()
	{
		if (doHightlight)
		{	
			if (direction == 1)
			{
				// Add highlight
				highlight = Mathf.Lerp (1f, maxHighlight, AdvGame.LinearTimeFactor (fadeStartTime, fadeTime));
				
				if (highlight >= maxHighlight)
				{
					highlight = maxHighlight;
					
					if (isFlashing)
					{
						direction = -1;
						fadeStartTime = Time.time;
					}
					else
					{
						doHightlight = false;
					}
				}
			}
			else
			{
				// Remove highlight
				highlight = Mathf.Lerp (maxHighlight, 1f, AdvGame.LinearTimeFactor (fadeStartTime, fadeTime));

				if (highlight <= 1f)
				{
					highlight = 1f;
					doHightlight = false;
					isFlashing = false;
				}
			}

			int i = 0;
			float alpha;

			// Go through own materials
			if (renderer)
			{
				foreach (Material material in renderer.materials)
				{
					if (material.HasProperty ("_Color"))
					{
						alpha = material.color.a;
						Color newColor = originalColors[i] * highlight;
						newColor.a = alpha;
						material.color = newColor;
						i++;
					}
				}
			}
			
			// Go through any child materials
			Component[] children;
			children = GetComponentsInChildren <Renderer>();
			foreach (Renderer childRenderer in children)
			{
				foreach (Material material in childRenderer.materials)
				{
					if (originalColors.Count <= i)
					{
						break;
					}

					if (material.HasProperty ("_Color"))
					{
						alpha = material.color.a;
						Color newColor = originalColors[i] * highlight;
						newColor.a = alpha;
						material.color = newColor;
						i++;
					}
				}
			}

			if (guiTexture)
			{
				alpha = Mathf.Lerp(0.2f, 1f, highlight - 1f); // highlight is between 1 and 2
				Color newColor = originalColors[i];
				newColor.a = alpha;
				guiTexture.color = newColor;
			}
		}
	}
	
	
	public void HighlightOn ()
	{
		doHightlight = true;
		isFlashing = false;
		direction = 1;
		fadeStartTime = Time.time;
		
		if (highlight > 1f)
		{
			fadeStartTime -= (highlight - 1f) / (maxHighlight - 1f) * fadeTime;
		}
		else
		{
			highlight = 1f;
		}
	}
	
	
	public void HighlightOff ()
	{
		doHightlight = true;
		isFlashing = false;
		direction = -1;
		fadeStartTime = Time.time;
		
		if (highlight < maxHighlight)
		{
			fadeStartTime -= (maxHighlight - highlight) / (maxHighlight - 1) * fadeTime;
		}
		else
		{
			highlight = maxHighlight;
		}
	}
	
	
	public void Flash ()
	{
		if (!isFlashing && (!doHightlight || (doHightlight && direction == -1)))
		{
			doHightlight = true;
			isFlashing = true;
			highlight = 1f;
			direction = 1;
			fadeStartTime = Time.time;
		}
	}
	
	
	public void HighlightOffInstant ()
	{
		doHightlight = false;
		isFlashing = false;
		
		// Go through any child materials
		int i=0;
		Component[] children;
		children = GetComponentsInChildren <Renderer>();
		foreach (Renderer childRenderer in children)
		{
			foreach (Material material in childRenderer.materials)
			{
				if (material.HasProperty ("_Color"))
				{
					Color newColor = originalColors[i];
					material.color = newColor;
					i++;
				}
			}
		}

		if (guiTexture)
		{
			Color newColor = originalColors[i];
			guiTexture.color = newColor;
		}
	}
	
}

Commits for Nextrek/SpaceCrew/SpaceCrew/Assets/AdventureCreator/Scripts/Object/Highlight.cs

Diff revisions: vs.
Revision Author Commited Message
83 FMMortaroli picture FMMortaroli Tue 13 May, 2014 11:32:51 +0000