颜色渐变如何工作?

时间:2019-03-18 22:14:24

标签: c# unity3d

我正在尝试在我的代码中实现下面的图片,但是不起作用。它仅在所有侧面显示红色。我正在使用线渲染器和颜色渐变。请检查我的代码。谢谢进阶

enter image description here

 void Start()
{
    lineGeneratorPrefab = new GameObject();

    DrawLine();
}




private void DrawLine()
{
    GameObject myLine = new GameObject();
    myLine.transform.position = start;
    myLine.AddComponent<LineRenderer>();
    lr = myLine.GetComponent<LineRenderer>();
    lr.material = new Material(Shader.Find("Sprites/Default"));     
    lr.positionCount = 4;
    lr.SetPosition(0, new Vector3(-2, 0, 0));
    lr.SetPosition(1, new Vector3(2, 0, 0));
    lr.SetPosition(2, new Vector3(2, -2, 0));
    lr.SetPosition(3, new Vector3(-2, -2, 0));
    lr.loop = true;


    Gradient gradient = new Gradient();
    gradient.SetKeys(
        new GradientColorKey[]
        {
            new GradientColorKey(Color.red, 0.25f),
            new GradientColorKey(Color.blue, 0.25f),
            new GradientColorKey(Color.green, 0.25f),
            new GradientColorKey(Color.yellow, 0.25f)
        },
            new GradientAlphaKey[] { new GradientAlphaKey(1.0f, 0.0f) }
        );
    lr.colorGradient = gradient;

}

1 个答案:

答案 0 :(得分:1)

GradientColorKey的第二个参数是[0,1],

之间的索引

所以您可以像这样修改GradientColorKey数组:

new GradientColorKey[]
    {
        new GradientColorKey(Color.red, 0.00f),
        new GradientColorKey(Color.red, 0.24f),
        new GradientColorKey(Color.blue, 0.25f),
        new GradientColorKey(Color.blue, 0.49f),
        new GradientColorKey(Color.green, 0.50f),
        new GradientColorKey(Color.green, 0.74f),
        new GradientColorKey(Color.yellow, 0.75f),
        new GradientColorKey(Color.yellow, 1.00f)
    }