[AddComponentMenu("UI/Effects/Gradient")]
public class Gradient : BaseMeshEffect
{
[SerializeField]
private Color32 topColor = Color.white;
[SerializeField]
private Color32 bottomColor = Color.black;
public override void ModifyMesh(VertexHelper vh)
{
if (!IsActive())
return;
List<UIVertex> vertices = new List<UIVertex>();
vh.GetUIVertexStream(vertices);
ModifyVertices(vertices);
vh.Clear();
vh.AddUIVertexTriangleStream(vertices);
}
public void ModifyVertices(List<UIVertex> vertices)
{
if (!IsActive() || vertices.Count <= 0)
{
return;
}
for (int i = 0; i < vertices.Count;)
{
float bottomY = vertices[i].position.y;
float topY = bottomY;
float dis = 1f;
for (int k = 1; k < 6; k++)
{
float y = vertices[k + i].position.y;
if (y > topY)
{
topY = y;
}
else if (y < bottomY)
{
bottomY = y;
}
}
dis = topY - bottomY;
for (int k = 0; k < 6; k++)
{
UIVertex vertText = vertices[k + i];
vertText.color = Color32.Lerp(bottomColor, topColor, (vertText.position.y - bottomY) / dis);
vertices[k + i] = vertText;
}
i += 6;
}
}
public void SetColor(Color top, Color bottom)
{
topColor = top;
bottomColor = bottom;
}
}
我在外部通过SetColor函数更改颜色Inspector面板中的颜色更改了,但是UI显示颜色没有更改,我觉得应该再给vertText.color赋值,老师这个应该怎么赋值呢?
public class Test : MonoBehaviour {
public GameObject test;
float timer;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
timer += Time.deltaTime;
if (timer >= 2)
{
//if (timer >= 2 && timer < 2.02f)
//{
// test.gameObject.SetActive(false);
// test.GetComponent<Gradient>().SetColor(Color.yellow, Color.white);
//}
//if (timer >= 2.03f)
//{
// test.gameObject.SetActive(true);
//}
GetComponent<Gradient>().SetColor(Color.yellow, Color.red);
}
}
}
我是在外部这样调用的,但是并没有输出,没有进入那个函数,用上这种先隐藏再打开他就会更新显示,老师这是什么原因呢?