【Unity】VFXGraphで使用できる自作ブロック (custom spawner block)を作成する

はじめに

今回はVFXGraphで使用できる自作ブロック (custom spawner block)を作成する方法を紹介します

環境は Unity 2022.3.1f1です

作成方法

VFXSpawnerCallbacksクラスを継承することでCustom spawner blockを作成することができます

using System.Collections;
using UnityEngine;
using UnityEngine.VFX;

    public class TestCustomBlock : VFXSpawnerCallbacks
    {
        public override void OnPlay(VFXSpawnerState state, VFXExpressionValues vfxValues, VisualEffect vfxComponent)
        {
            Debug.Log("エフェクトの再生開始");
        }

        public override void OnUpdate(VFXSpawnerState state, VFXExpressionValues vfxValues, VisualEffect vfxComponent)
        {
        }

        public override void OnStop(VFXSpawnerState state, VFXExpressionValues vfxValues, VisualEffect vfxComponent)
        {
        }
    }

上記クラスを作成するとSpawnでCreate Block→Customの場所に作成したブロックが追加されます

変数公開方法

Block内でInputPropertiesを定義してそのクラス内でpublic変数を追加することで公開することができます

using System.Collections;
using UnityEngine;
using UnityEngine.VFX;

    public class TestCustomBlock : VFXSpawnerCallbacks
    {
        //公開変数 クラス名はInputPropertiesである必要がありそう
        //VFXGraphのプロパティで公開できる型が使用出来る
        public class InputProperties
        {
            public int TestCount = 1;
        }
        
        public override void OnPlay(VFXSpawnerState state, VFXExpressionValues vfxValues, VisualEffect vfxComponent)
        {
            Debug.Log("エフェクトの再生開始");
        }

        public override void OnUpdate(VFXSpawnerState state, VFXExpressionValues vfxValues, VisualEffect vfxComponent)
        {
        }

        public override void OnStop(VFXSpawnerState state, VFXExpressionValues vfxValues, VisualEffect vfxComponent)
        {
        }
    }

変数取得方法

VFXExpressionValuesからGet~を使用することで取得できます

using System.Collections;
using UnityEngine;
using UnityEngine.VFX;

 public class TestCustomBlock : VFXSpawnerCallbacks
    {
        //公開変数 クラス名はInputPropertiesである必要がありそう
        //VFXGraphのプロパティで公開できる型が使用出来る
        public class InputProperties
        {
            public int TestCount = 1;
        }
        //公開した変数名のtPropertyIDを用意
        static private readonly int loopCountPropertyID = Shader.PropertyToID("TestCount");
        private int m_TestCount;
        
        public override void OnPlay(VFXSpawnerState state, VFXExpressionValues vfxValues, VisualEffect vfxComponent)
        {
            //公開した変数の情報を取得
            m_TestCount = vfxValues.GetInt(loopCountPropertyID);
            Debug.Log("m_TestCount = " + m_TestCount.ToString());
        }

        public override void OnUpdate(VFXSpawnerState state, VFXExpressionValues vfxValues, VisualEffect vfxComponent)
        {
        }

        public override void OnStop(VFXSpawnerState state, VFXExpressionValues vfxValues, VisualEffect vfxComponent)
        {
        }
    }