Legacy Documentation: Version 2017.2 (Go to current version)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

Gradient

class in UnityEngine

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

Gradient used for animating colors.

function Start () {
    var g : Gradient;
    var gck : GradientColorKey[];
    var gak : GradientAlphaKey[];

g = new Gradient();

// Populate the color keys at the relative time 0 and 1 (0 and 100%) gck = new GradientColorKey[2]; gck[0].color = Color.red; gck[0].time = 0.0f; gck[1].color = Color.blue; gck[1].time = 1.0f;

// Populate the alpha keys at relative time 0 and 1 (0 and 100%) gak = new GradientAlphaKey[2]; gak[0].alpha = 1.0f; gak[0].time = 0.0f; gak[1].alpha = 0.0f; gak[1].time = 1.0f;

g.SetKeys(gck, gak);

// What's the color at the relative time 0.25 (25 %) ? Debug.Log(g.Evaluate(0.25f)); }
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { void Start() { Gradient g; GradientColorKey[] gck; GradientAlphaKey[] gak; g = new Gradient(); gck = new GradientColorKey[2]; gck[0].color = Color.red; gck[0].time = 0.0F; gck[1].color = Color.blue; gck[1].time = 1.0F; gak = new GradientAlphaKey[2]; gak[0].alpha = 1.0F; gak[0].time = 0.0F; gak[1].alpha = 0.0F; gak[1].time = 1.0F; g.SetKeys(gck, gak); Debug.Log(g.Evaluate(0.25F)); } }

Note that the alpha and colors keys will be automatically sorted by time value and that it is ensured to always have a minimum of 2 color keys and 2 alpha keys.

Properties

alphaKeysAll alpha keys defined in the gradient.
colorKeysAll color keys defined in the gradient.
modeControl how the gradient is evaluated.

Constructors

GradientCreate a new Gradient object.

Public Methods

EvaluateCalculate color at a given time.
SetKeysSetup Gradient with an array of color keys and alpha keys.
对文档有任何疑问,请移步至开发者社区提问,我们将尽快为您解答