UGUI_Text_Gradient: GradientLeftToRight / GradientTopToBottom
2017-09-12 20:34
597 查看
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
[AddComponentMenu ("UI/Effect/GradientLeftToRight")]
public class GradientLeftToRight : BaseMeshEffect
{
[SerializeField]
private Color32 leftColor = Color.black;
[SerializeField]
private Color32 rightColor = Color.black;
public override void ModifyMesh (VertexHelper vh)
{
if (!IsActive ()) {
return;
}
var vertextList = new List<UIVertex> ();
vh.GetUIVertexStream (vertextList);
int count = vertextList.Count;
ApplyGradient (vertextList, 0, count);
vh.Clear ();
vh.AddUIVertexTriangleStream (vertextList);
}
private void ApplyGradient (List<UIVertex> vertexList, int start, int end)
{
float leftX = 0;
float RightX = 0;
print (end);
for (int i = 0; i < end; i++) {
float x = vertexList [i].position.x;
if (x > RightX) {
RightX = x;
} else if (x < leftX) {
leftX = x;
}
}
float uiElementWeight = RightX - leftX;
for (int i = 0; i < end; i++) {
UIVertex uiVertex = vertexList [i];
uiVertex.color = Color32.Lerp (leftColor, rightColor, (uiVertex.position.x - leftX) / uiElementWeight);
vertexList [i] = uiVertex;
}
}
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
[AddComponentMenu ("UI/Effects/GradientTopToBottom")]
public class GradientTopToBottom : BaseMeshEffect
{
[SerializeField]
private Color32
topColor = Color.white;
[SerializeField]
private Color32
bottomColor = Color.black;
public override void ModifyMesh (VertexHelper vh)
{
var vertexList = new List<UIVertex> ();
vh.GetUIVertexStream (vertexList);
int count = vertexList.Count;
ApplyGradient (vertexList, 0, count);
vh.Clear ();
vh.AddUIVertexTriangleStream (vertexList);
}
private void ApplyGradient (List<UIVertex> vertexList, int start, int end)
{
float bottomY = vertexList [0].position.y;
float topY = vertexList [0].position.y;
for (int i = start; i < end; ++i) {
float y = vertexList [i].position.y;
if (y > topY) {
topY = y;
} else if (y < bottomY) {
bottomY = y;
}
}
float uiElementHeight = topY - bottomY;
for (int i = start; i < end; ++i) {
UIVertex uiVertex = vertexList [i];
uiVertex.color = Color32.Lerp (bottomColor, topColor, (uiVertex.position.y - bottomY) / uiElementHeight);
vertexList [i] = uiVertex;
}
}
}
using UnityEngine;
using UnityEngine.UI;
[AddComponentMenu ("UI/Effect/GradientLeftToRight")]
public class GradientLeftToRight : BaseMeshEffect
{
[SerializeField]
private Color32 leftColor = Color.black;
[SerializeField]
private Color32 rightColor = Color.black;
public override void ModifyMesh (VertexHelper vh)
{
if (!IsActive ()) {
return;
}
var vertextList = new List<UIVertex> ();
vh.GetUIVertexStream (vertextList);
int count = vertextList.Count;
ApplyGradient (vertextList, 0, count);
vh.Clear ();
vh.AddUIVertexTriangleStream (vertextList);
}
private void ApplyGradient (List<UIVertex> vertexList, int start, int end)
{
float leftX = 0;
float RightX = 0;
print (end);
for (int i = 0; i < end; i++) {
float x = vertexList [i].position.x;
if (x > RightX) {
RightX = x;
} else if (x < leftX) {
leftX = x;
}
}
float uiElementWeight = RightX - leftX;
for (int i = 0; i < end; i++) {
UIVertex uiVertex = vertexList [i];
uiVertex.color = Color32.Lerp (leftColor, rightColor, (uiVertex.position.x - leftX) / uiElementWeight);
vertexList [i] = uiVertex;
}
}
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
[AddComponentMenu ("UI/Effects/GradientTopToBottom")]
public class GradientTopToBottom : BaseMeshEffect
{
[SerializeField]
private Color32
topColor = Color.white;
[SerializeField]
private Color32
bottomColor = Color.black;
public override void ModifyMesh (VertexHelper vh)
{
var vertexList = new List<UIVertex> ();
vh.GetUIVertexStream (vertexList);
int count = vertexList.Count;
ApplyGradient (vertexList, 0, count);
vh.Clear ();
vh.AddUIVertexTriangleStream (vertexList);
}
private void ApplyGradient (List<UIVertex> vertexList, int start, int end)
{
float bottomY = vertexList [0].position.y;
float topY = vertexList [0].position.y;
for (int i = start; i < end; ++i) {
float y = vertexList [i].position.y;
if (y > topY) {
topY = y;
} else if (y < bottomY) {
bottomY = y;
}
}
float uiElementHeight = topY - bottomY;
for (int i = start; i < end; ++i) {
UIVertex uiVertex = vertexList [i];
uiVertex.color = Color32.Lerp (bottomColor, topColor, (uiVertex.position.y - bottomY) / uiElementHeight);
vertexList [i] = uiVertex;
}
}
}
相关文章推荐
- Problem 15 - Starting in the top left corner in a 20 by 20 grid, how many routes are there to the bottom right corner?
- Unity UGUI 设置 left 、right、top、bottom
- Unity3d UGUI 自适应屏幕 代码修改Left,Top,Right,Bottom属性值
- 【Android】TextView动态设置android:drawableLeft|Right|Top|Bottom,SetColor
- TextView 代码设置drawableLeft、drawableRight、drawableTop、drawableBottom
- html中 left,right,top,bottom图示
- DIV绝对定位 position、z-index、top、right、bottom和left属性
- 动态设置 android:drawableTop|Right|Left|Bottom
- 安卓在代码中设置TextView的drawableLeft、drawableRight、drawableTop、drawableBottom
- Android 设置 drawableLeft|Right|Top|Bottom
- css中absolute定位的top bottom left right
- Android动态设置android:drawableLeft|Right|Top|Bottom 并根据分辨率自适应
- 面试总结(4):top、left、right、bottom 和 translationX、translationY 和 scrollX,scrollY的区别
- absolute,relative,left,right,bottom,top,float,z-index属性的讨论
- travel the binary tree by level 3 ( from down to top and from right to left every level )
- android关于TextView的drawableLeft,drawableTop,drawableRight,drawableRight的drawable大小及位置问题
- Android 实现滑动的几种方法(一)onLayout方法 和 offsetLeftAndRight()与offsetTopAndBottom();
- Android-区分 View 的 translationX、Y , X、Y 和 Left、Top,Right、Bottom
- left top right bottom问题
- [转]实现文字跑马灯效果,scrolling text from right to left