コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【Unity】グラデーション情報を管理するクラス「Gradient」

Inspector で設定する場合

using UnityEngine;

public class Example : MonoBehaviour
{
    public Gradient gradient;
}

f:id:baba_s:20171227230833p:plain

f:id:baba_s:20171227230756p:plain

スクリプトから設定する場合

using UnityEngine;

public class Example : MonoBehaviour
{
    private void Awake()
    {
        // カラーキーの配列
        var colorKeys = new []
        {
            new GradientColorKey( Color.red, 0 ),
            new GradientColorKey( Color.blue, 1 ),
        };

        // アルファキーの配列
        var alphaKeys = new []
        {
            new GradientAlphaKey( 1, 0 ),
            new GradientAlphaKey( 0, 1 ),
        };
        var gradient = new Gradient();
        gradient.SetKeys( colorKeys, alphaKeys );
    }
}

スクリプトから参照する場合

using UnityEngine;

public class Example : MonoBehaviour
{
    public Gradient gradient;

    private void Awake()
    {
        Debug.Log( gradient.Evaluate( 0.5f ) );
    }
}