SHOW:
|
|
- or go back to the newest paste.
1 | using UnityEngine; | |
2 | using System.Collections; | |
3 | using System.Collections.Generic; | |
4 | using UnityEngine.UI; | |
5 | ||
6 | [AddComponentMenu("UI/Effects/Gradient")] | |
7 | public class Gradient : BaseVertexEffect { | |
8 | [SerializeField] | |
9 | private Color32 topColor = Color.white; | |
10 | [SerializeField] | |
11 | private Color32 bottomColor = Color.black; | |
12 | ||
13 | public override void ModifyVertices(List<UIVertex> vertexList) { | |
14 | - | if (!IsActive()) { |
14 | + | if (!IsActive() || vertexList.Count == 0) { |
15 | return; | |
16 | } | |
17 | ||
18 | int count = vertexList.Count; | |
19 | float bottomY = vertexList[0].position.y; | |
20 | float topY = vertexList[0].position.y; | |
21 | ||
22 | for (int i = 1; i < count; i++) { | |
23 | float y = vertexList[i].position.y; | |
24 | if (y > topY) { | |
25 | topY = y; | |
26 | } | |
27 | else if (y < bottomY) { | |
28 | bottomY = y; | |
29 | } | |
30 | } | |
31 | ||
32 | float uiElementHeight = topY - bottomY; | |
33 | ||
34 | for (int i = 0; i < count; i++) { | |
35 | UIVertex uiVertex = vertexList[i]; | |
36 | uiVertex.color = Color32.Lerp(bottomColor, topColor, (uiVertex.position.y - bottomY) / uiElementHeight); | |
37 | vertexList[i] = uiVertex; | |
38 | } | |
39 | } | |
40 | } |