Created
February 20, 2017 02:35
-
-
Save enue/bec55e0d0be859274d7f72f959bec555 to your computer and use it in GitHub Desktop.
[Unity]UIにグラデーションをつける
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine.UI; | |
using System; | |
// Unity5.5.1p4 | |
namespace TSKT | |
{ | |
public class DirectionalGradation : BaseMeshEffect | |
{ | |
[SerializeField] | |
Color32 color = Color.white; | |
[SerializeField] | |
[Range(0f, 360f)] | |
float angle = 0f; | |
public override void ModifyMesh(VertexHelper vh) | |
{ | |
if (!IsActive()) | |
{ | |
return; | |
} | |
var list = UIVerticesPool.Get(); | |
vh.GetUIVertexStream(list); | |
ModifyMesh(list); | |
vh.Clear(); | |
vh.AddUIVertexTriangleStream(list); | |
} | |
void ModifyMesh(List<UIVertex> list) | |
{ | |
float max = float.MinValue; | |
float min = float.MaxValue; | |
var vector = new Vector2(Mathf.Cos(Mathf.Deg2Rad * angle), Mathf.Sin(Mathf.Deg2Rad * angle)); | |
foreach(var it in list) | |
{ | |
var dot = it.position.x * vector.x + it.position.y * vector.y; | |
max = Mathf.Max(dot, max); | |
min = Mathf.Min(dot, min); | |
} | |
if (max == min) | |
{ | |
return; | |
} | |
for (int i = 0; i < list.Count; ++i) | |
{ | |
var vertex = list[i]; | |
var dot = vertex.position.x * vector.x + vertex.position.y * vector.y; | |
var t = Mathf.InverseLerp(min, max, dot); | |
var c = Color.Lerp(Color.white, color, t); | |
vertex.color = c * vertex.color; | |
list[i] = vertex; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
こっちも必要
https://gist.github.com/enue/9999df944dd39bd1fc3a2677e14db26e