HDRPでカスタムポストプロセスを使うで追加したエフェクトのパラメーターをスクリプトで変えてみました。
自分で作ったVolumeコンポーネントもTryGetメソッドの引数にoutキーワードを前置きして取得できます。
- using UnityEngine;
- using UnityEngine.Rendering;
-
- public class ChangeVolumeParameter : MonoBehaviour
- {
- [SerializeField] Volume volume;
- NewPostProcessVolume postProcessVolume;
-
- void Awake()
- {
- // NewPostProcessVolumeを取得
- volume.profile.TryGet(out postProcessVolume);
- }
-
- // エフェクトの強さを変える
- public void EnableEffect()
- {
- if(postProcessVolume.intensity.value >= 1f)
- {
- postProcessVolume.intensity.value = 0f;
- }
- else
- {
- postProcessVolume.intensity.value = 1f;
- }
- }
-
- public void EnableEffect(bool enabled)
- {
- postProcessVolume.enabled.value = enabled;
- }
-
-
- }
このカスタムボリュームコンポーネントでは、デフォルトでintensityというフィールドが宣言されていました。
- using UnityEngine;
- using UnityEngine.Rendering;
- using UnityEngine.Rendering.HighDefinition;
- using System;
-
- [Serializable, VolumeComponentMenu("Post-processing/Custom/New Post Process Volume")]
- public sealed class NewPostProcessVolume : CustomPostProcessVolumeComponent, IPostProcessComponent
- {
- [Tooltip("Controls the intensity of the effect.")]
- public ClampedFloatParameter intensity = new ClampedFloatParameter(0f, 0f, 1f);
-
- public BoolParameter enabled = new BoolParameter(true);
-
- Material m_Material;
- // ...
型はfloatではなく、ClampedFloatParameterです。float型の値はvalueフィールドに入っています。
- postProcessVolume.intensity.value = 1f;
BoolParameter型の変数を宣言するとbool型も使えます。publicキーワードや[SerializeField]属性を付けると、インスペクタに表示されます。
Volumeのゲームオブジェクトをスクリプトにアタッチして、ボタンを押したときにintensityを変えるメソッドを呼ぶとエフェクトを切り替えられました。