PreferenceItem Manual     Reference     Scripting  
Scripting > Editor Attributes > PreferenceItem
PreferenceItem Inherits from Attribute

The PreferenceItem attribute allows you to add preferences sections to the Preferences Window.

Note: This is an editor class. To use it you have to place your script in Assets/Editor inside your project folder. Editor classes are in the UnityEditor namespace so for C# scripts you need to add "using UnityEditor;" at the beginning of the script.

The PreferenceItem attribute turns any public static function into an OnGUI callback. Only static functions can use the PreferenceItem attribute.

public class OurPreferences {
// Have we loaded the prefs yet
private static var prefsLoaded : boolean = false;

// The Preferences
public static var boolPreference : boolean = false;

// Add preferences section named "My Preferences" to the Preferences Window
@PreferenceItem ("My Preferences")
static function PreferencesGUI () {
// Load the preferences
if (!prefsLoaded) {
boolPreference = EditorPrefs.GetBool ("BoolPreferenceKey", false);
prefsLoaded = true;
}

// Preferences GUI
boolPreference = EditorGUILayout.Toggle ("Bool Preference", boolPreference);

// Save the preferences
if (GUI.changed)
EditorPrefs.SetBool ("BoolPreferenceKey", boolPreference);
}
}

Constructors
PreferenceItem

Creates a section in the Preferences Window called name and invokes the static function following it for the section's GUI.