14
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PlayerPrefs の値を編集するエディタ拡張

Posted at

コード

以下のコードを Assets/Editor 配下などに格納。

EditPlayerPrefs.cs
using UnityEngine;
using UnityEditor;
using System.Collections;

public class EditPlayerPrefs : EditorWindow {
	
	class PrefInfo {
		public string Key;
		public string Type;
	}
	
    // 表示対象のキーとタイプ
	PrefInfo[] keys = new PrefInfo[] {
		new PrefInfo { Key = "hoge", Type = "string" },
		new PrefInfo { Key = "fuga", Type = "int" },
	};
	
	[MenuItem("Window/Edit PlayerPrefs")]
	public static void ShowWindow() {
		EditorWindow win = EditorWindow.GetWindow<EditPlayerPrefs>();
		win.title = "PlayerPrefs";
	}
	
	void OnGUI() {
		foreach(var keyInfo in keys) {
			switch(keyInfo.Type) {
			case "string":
				var originalString = PlayerPrefs.GetString(keyInfo.Key);
				var currentString = EditorGUILayout.TextField(keyInfo.Key, originalString);
				if (currentString != originalString) {
					PlayerPrefs.SetString(keyInfo.Key, currentString);
					Debug.Log ("*** update " + originalString + " => " + currentString);
				}
				break;
				
			case "int":
				var originalInteger = PlayerPrefs.GetInt (keyInfo.Key);
				var currentInteger = System.Convert.ToInt32(EditorGUILayout.TextField(keyInfo.Key, originalInteger.ToString()));
				if (currentInteger != originalInteger) {
					PlayerPrefs.SetInt(keyInfo.Key, currentInteger);
					Debug.Log ("*** update " + originalInteger.ToString() + " => " + currentInteger.ToString());
				}
				break;
				
			default:
				throw new Claris.System.Exception.ApplicationError("Unsupported pref type : " + keyInfo.Type);
			}
		}
	}
}

使い方

PrefInfo[] keys の値を適宜変更。

[Window]-[Edit PlayerPrefs] で起動

kobito.1404288964.632680.png

14
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?