コガネブログ

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

【Unity】EditorPrefs で保存されているすべてのキーと値を取得するエディタ拡張

ソースコード

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEditor;
using UnityEngine;

public static class Example
{
    [MenuItem( "Tool/Log" )]
    private static void Log()
    {
        foreach ( var (key, value) in GetEditorPrefsKeyValuePairAll().OrderBy( x => x.key ) )
        {
            Debug.Log( key + ": " + value );
        }
    }

    private static IEnumerable<(string key, object value)> GetEditorPrefsKeyValuePairAll()
    {
        var name = @"Software\Unity Technologies\Unity Editor 5.x\";
        using ( var registryKey = Registry.CurrentUser.OpenSubKey( name, false ) )
        {
            foreach ( var valueName in registryKey.GetValueNames() )
            {
                var value = registryKey.GetValue( valueName );
                var key = valueName.Split( new[] { "_h" }, StringSplitOptions.None )[ 0 ];

                if ( value is byte[] byteValue )
                {
                    yield return ( key, Encoding.UTF8.GetString( byteValue ) );
                }
                else
                {
                    yield return ( key, value );
                }
            }
        }
    }
}