uGUIでグラデーション文字 その2
前にuGUIの文字にグラデーションをつけるコンポーネントを作った。
が、Unityが5.1から5.2になるにあたって、Textの頂点生成処理が変更されており(1文字4頂点だったのが6頂点に変更)、上記のやりかただとパラメータを再設定する必要が出てくる。
再設定すればちゃんと表示されるんだけど、そもそも頂点構成に影響されてる時点でウカツなので、先のことを考えて直す。
using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityEngine.UI; public class DirectionalGradation : BaseMeshEffect { [SerializeField] Color32 color = Color.white; [SerializeField] [Range(0f, 360f)] float angle = 0f; public override void ModifyMesh(Mesh mesh) { if (!IsActive()) { return; } var list = new List<UIVertex>(); using (var helper = new VertexHelper(mesh)) { helper.GetUIVertexStream(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); vertex.color = Color32.Lerp(vertex.color, color, t); list[i] = vertex; } using (var helper2 = new VertexHelper()) { helper2.AddUIVertexTriangleStream(list); helper2.FillMesh(mesh); } } }
マア、頂点ごとに固定で色を指定するだけの旧バージョンのほうが、速くシンプルではあるのだが……ままならぬ。
追記
Unity5.5対応版。