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.SetFloat

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 SetFloat(key: string, value: float): void;
public static void SetFloat(string key, float value);

Parameters

key Name of key to write float into.
value Float value to write into the storage.

Description

Sets the float value of the preference identified by key.

// Simple scripts that creates an empty window and remembers the position and size
// of the created window.

class RememberPosition extends EditorWindow { var width : float = 300; var height : float = 300; var x : float = 100; var y : float = 100;

@MenuItem("Examples/Remember position") static function Init() { var window = GetWindow(RememberPosition); window.Show(); } function Awake() { width = EditorPrefs.GetFloat("EditorWindowWidth", 300); height = EditorPrefs.GetFloat("EditorWindowHeight", 300); x = EditorPrefs.GetFloat("EditorWindowX", 100); y = EditorPrefs.GetFloat("EditorWindowY", 100);

position = Rect(x, y, width, height); } function OnDestroy() { EditorPrefs.SetFloat("EditorWindowWidth", width); EditorPrefs.SetFloat("EditorWindowHeight", height); EditorPrefs.SetFloat("EditorWindowX", x); EditorPrefs.SetFloat("EditorWindowY", y); } function OnGUI() { if(GUILayout.Button("Close")) this.Close(); } }
// Simple script that allows a float value to be editted
// in a slider. The final value is written into the Editor Preferences.

using UnityEngine; using UnityEditor; using System;

public class SetFloatExample : EditorWindow { static float floatValue = 0.0f;

[MenuItem("Examples/Preferences SetFloat Example")] static void Init() { Rect r = new Rect(10, 10, 200, 100); SetFloatExample window = (SetFloatExample)EditorWindow.GetWindowWithRect(typeof(SetFloatExample), r); window.Show(); }

void Awake() { floatValue = EditorPrefs.GetFloat("FloatExample", floatValue); }

void OnGUI() { floatValue = EditorGUILayout.Slider(floatValue, -1.0f, 1.0f); if (GUILayout.Button("Save float " + Convert.ToString(floatValue) + "?")) { EditorPrefs.SetFloat("FloatExample", floatValue); } if (GUILayout.Button("Close")) this.Close(); } }

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