コガネブログ

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

【Unity】ParticleSystem の Inspector に Play・Pause などのボタンを追加するエディタ拡張

ソースコード

using System;
using UnityEditor;
using UnityEngine;

[CustomEditor( typeof( ParticleSystem ) )]
public sealed class ParticleSystemInspector : Editor
{
    private static readonly Type BASE_EDITOR_TYPE = typeof( Editor )
        .Assembly
        .GetType( "UnityEditor.ParticleSystemInspector" );

    public override void OnInspectorGUI()
    {
        var particleSystem = ( ParticleSystem ) target;

        using ( new EditorGUILayout.HorizontalScope() )
        {
            if ( GUILayout.Button( "Play" ) )
            {
                particleSystem.Play();
            }

            if ( GUILayout.Button( "Stop" ) )
            {
                particleSystem.Stop();
            }

            if ( GUILayout.Button( "Pause" ) )
            {
                particleSystem.Pause();
            }

            if ( GUILayout.Button( "Clear" ) )
            {
                particleSystem.Clear();
            }
        }

        var editor = CreateEditor( particleSystem, BASE_EDITOR_TYPE );

        editor.OnInspectorGUI();
    }
}

使用例

f:id:baba_s:20220216184622p:plain