Skip to content

Instantly share code, notes, and snippets.

@enue
Created February 20, 2017 02:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enue/bec55e0d0be859274d7f72f959bec555 to your computer and use it in GitHub Desktop.
Save enue/bec55e0d0be859274d7f72f959bec555 to your computer and use it in GitHub Desktop.
[Unity]UIにグラデーションをつける
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;
}
}
}
}
@enue
Copy link
Author

enue commented Feb 20, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment