Version: 2017.3 (switch to 2017.4)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

EditorPrefs.GetBool

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public static method GetBool(key: string, defaultValue: bool = false): bool;
public static bool GetBool(string key, bool defaultValue = false);

Description

Returns the value corresponding to key in the preference file if it exists.

If it doesn't exist, it will return defaultValue.


Round rotations/positions and remember the active option.

#pragma strict
public class EditorPrefsBoolExample extends EditorWindow {
	var showRoundPosition: boolean = true;
	var showRoundRotation: boolean = true;
	@MenuItem("Examples/Round positions-rotations")
	static function Init() {
		var window: EditorPrefsBoolExample = EditorPrefsBoolExampleEditorWindow.GetWindow(EditorPrefsBoolExample, true, "My Empty Window");
		window.Show();
	}
	function OnGUI() {
		showRoundPosition = EditorGUILayout.BeginToggleGroup("Round Position", showRoundPosition);
		if (GUILayout.Button("Round Position!"))
			DoRoundPosition();
		EditorGUILayout.EndToggleGroup();
		showRoundRotation = EditorGUILayout.BeginToggleGroup("Round Rotation", showRoundRotation);
		if (GUILayout.Button("Round Rotation!"))
			DoRoundRotation();
		EditorGUILayout.EndToggleGroup();
	}
	function DoRoundPosition() {
		for (var t: Transform in Selection.transforms)
			t.localPosition = new Vector3(Mathf.Round(t.localPosition.x), Mathf.Round(t.localPosition.z), Mathf.Round(t.localPosition.y));
	}
	function DoRoundRotation() {
		for (var t: Transform in Selection.transforms)
			t.rotation = Quaternion.Euler(new Vector3(Mathf.Round(t.eulerAngles.x / 45f) * 45f, Mathf.Round(t.eulerAngles.y / 45f) * 45f, Mathf.Round(t.eulerAngles.z / 45f) * 45f));
	}
	function OnFocus() {
		if (EditorPrefs.HasKey("ShowRoundPosition"))
			showRoundPosition = EditorPrefs.GetBool("ShowRoundPosition");
		if (EditorPrefs.HasKey("ShowRoundRotation"))
			showRoundPosition = EditorPrefs.GetBool("ShowRoundRotation");
	}
	function OnLostFocus() {
		EditorPrefs.SetBool("ShowRoundPosition", showRoundPosition);
		EditorPrefs.SetBool("ShowRoundRotation", showRoundRotation);
	}
	function OnDestroy() {
		EditorPrefs.SetBool("ShowRoundPosition", showRoundPosition);
		EditorPrefs.SetBool("ShowRoundRotation", showRoundRotation);
	}
}
using UnityEngine;
using UnityEditor;

public class EditorPrefsBoolExample : EditorWindow { bool showRoundPosition = true; bool showRoundRotation = true;

[MenuItem("Examples/Round positions-rotations")] static void Init() { EditorPrefsBoolExample window = (EditorPrefsBoolExample)EditorWindow.GetWindow(typeof(EditorPrefsBoolExample), true, "My Empty Window"); window.Show(); }

void OnGUI() { showRoundPosition = EditorGUILayout.BeginToggleGroup("Round Position", showRoundPosition); if (GUILayout.Button("Round Position!")) DoRoundPosition(); EditorGUILayout.EndToggleGroup(); showRoundRotation = EditorGUILayout.BeginToggleGroup("Round Rotation", showRoundRotation); if (GUILayout.Button("Round Rotation!")) DoRoundRotation(); EditorGUILayout.EndToggleGroup(); }

void DoRoundPosition() { foreach (Transform t in Selection.transforms) t.localPosition = new Vector3(Mathf.Round(t.localPosition.x), Mathf.Round(t.localPosition.z), Mathf.Round(t.localPosition.y)); }

void DoRoundRotation() { foreach (Transform t in Selection.transforms) t.rotation = Quaternion.Euler( new Vector3(Mathf.Round(t.eulerAngles.x / 45f) * 45f, Mathf.Round(t.eulerAngles.y / 45f) * 45f, Mathf.Round(t.eulerAngles.z / 45f) * 45f)); }

void OnFocus() { if (EditorPrefs.HasKey("ShowRoundPosition")) showRoundPosition = EditorPrefs.GetBool("ShowRoundPosition"); if (EditorPrefs.HasKey("ShowRoundRotation")) showRoundPosition = EditorPrefs.GetBool("ShowRoundRotation"); }

void OnLostFocus() { EditorPrefs.SetBool("ShowRoundPosition", showRoundPosition); EditorPrefs.SetBool("ShowRoundRotation", showRoundRotation); }

void OnDestroy() { EditorPrefs.SetBool("ShowRoundPosition", showRoundPosition); EditorPrefs.SetBool("ShowRoundRotation", showRoundRotation); } }

Did you find this page useful? Please give it a rating: