Unity - A计划(一年有效期) 扫二维码继续学习 二维码时效为半小时

(61评价)
价格: 2155.00元
UGUI渐变色如何代码动态更改?
Khaleesi发起了问答2018-05-14
7
回复
973
浏览
[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赋值,老师这个应该怎么赋值呢?

所有回复
  • siki 2018-05-14
    vertText.color = Color32.Lerp(bottomColor, topColor, (vertText.position.y - bottomY) / dis);

    你这里的赋值并没有什么问题,主要是看看你后面赋的值,是否有问题,可以把这里赋的值输出出来检查下,是不是有问题 

    还有-4条回复,点击查看
    你还没有登录,请先登录注册
  • Khaleesi 2018-05-14

    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);
    
            }
    
        }
    }

    我是在外部这样调用的,但是并没有输出,没有进入那个函数,用上这种先隐藏再打开他就会更新显示,老师这是什么原因呢?

    还有-5条回复,点击查看
    你还没有登录,请先登录注册
  • siki 2018-05-16

    是不是你的vertText获取到的不正确呢

    还有-5条回复,点击查看
    你还没有登录,请先登录注册
  • siki 2018-05-16

    你可以不适用gradient,自己直接运行一个设置text颜色的代码,看下是否有效,如果有效的话,那说明就是你的gradient的问题

    • Khaleesi 2018-05-17

      老师有点忙,刚看见您的回复,我设置完gradient颜色之后,又设置了遍text本身的颜色它就刷新了,可能是vertText不能这是设置,现在效果是实现了,我在研究研究吧,谢谢老师的指导。

      (0) 回复
    • siki 2018-05-18

      回复 @ Khaleesi: 那就是你的脚本的问题,颜色设置本身是没有问题的

      (0) 回复
    还有-3条回复,点击查看
    你还没有登录,请先登录注册
发表回复
你还没有登录,请先 登录或 注册!