- Home /
Question by
Daxva · Sep 21, 2011 at 12:40 PM ·
prefabloadeditorwindowdata
Load Data from EditorWindow to Prefab
So I’ve created a EditorWindow where I can input some variable to set some settings that will later be used by a script. I store away the variables using the EditorPrefs.SetFloat(). What I wonder is how I can retrieve the variable from the script later. Can I use EditorPrefs or do I need to use something else?
Here is the script for the settings window:
class PowerUpSettings : EditorWindow
{ #region Members
bool showAttackRange = false;
bool showEffectRange = false;
bool showPushForce = false;
bool showTime = false;
//Attack Range
float rangeMelee = 1;
float rangeShort = 5;
float rangeMedium = 10;
float rangeLong = 15;
float rangeVeryLong = 20;
//Effect Range
float effectSmall = 1.5f;
float effectMedium = 3f;
float effectBig = 5f;
//Push Force
float pushShort = 2;
float pushMedium = 4;
float pushLong = 6;
//Time
float timeVeryShort = 1f;
float timeShort = 2.5f;
float timeMedium = 5f;
float timeLong = 10f;
float timeVeryLong = 15f;
#endregion
[MenuItem("Settings/Power Up Settings")]
static void Init()
{
PowerUpSettings window = (PowerUpSettings)EditorWindow.GetWindow(typeof(PowerUpSettings));
}
void Awake()
{
LoadData();
}
void SaveData()
{
//Attack Range
EditorPrefs.SetFloat("powerupAttackMelee", rangeMelee);
EditorPrefs.SetFloat("powerupAttackShort", rangeShort);
EditorPrefs.SetFloat("powerupAttackMedium", rangeMedium);
EditorPrefs.SetFloat("powerupAttackLong", rangeLong);
EditorPrefs.SetFloat("powerupAttackVeryLong", rangeVeryLong);
//Effect Range
EditorPrefs.SetFloat("powerupEffectSmall", effectSmall);
EditorPrefs.SetFloat("powerupEffectMedium", effectMedium);
EditorPrefs.SetFloat("powerupEffectBig", effectBig);
//Push Force
EditorPrefs.SetFloat("powerupPushShort", pushShort);
EditorPrefs.SetFloat("powerupPushMedium", pushMedium);
EditorPrefs.SetFloat("powerupPushLong", pushLong);
//Time
EditorPrefs.SetFloat("powerupTimeVeryShort", timeVeryShort);
EditorPrefs.SetFloat("powerupTimeVShort", timeShort);
EditorPrefs.SetFloat("powerupTimeMedium", timeMedium);
EditorPrefs.SetFloat("powerupTimeLong", timeLong);
EditorPrefs.SetFloat("powerupTimeVeryLong", timeVeryLong);
}
void LoadData()
{
//Attack Range
rangeMelee = EditorPrefs.GetFloat("powerupAttackMelee", 1);
rangeShort = EditorPrefs.GetFloat("powerupAttackShort", 5);
rangeMedium = EditorPrefs.GetFloat("powerupAttackMedium", 10);
rangeLong = EditorPrefs.GetFloat("powerupAttackLong", 15);
rangeVeryLong = EditorPrefs.GetFloat("powerupAttackVeryLong", 20);
//Effect Range
effectSmall = EditorPrefs.GetFloat("powerupEffectSmall", 1.5f);
effectMedium = EditorPrefs.GetFloat("powerupPushMedium", 3);
effectBig = EditorPrefs.GetFloat("powerupPushLong", 5);
//Push Force
pushShort = EditorPrefs.GetFloat("powerupPushShort", 2);
pushMedium = EditorPrefs.GetFloat("powerupPushMedium", 2);
pushLong = EditorPrefs.GetFloat("powerupEffectBig", 2);
//Time
timeVeryShort = EditorPrefs.GetFloat("powerupTimeVeryShort", 1);
timeShort = EditorPrefs.GetFloat("powerupTimeVShort", 2.5f);
timeMedium = EditorPrefs.GetFloat("powerupTimeMedium", 5);
timeLong = EditorPrefs.GetFloat("powerupTimeLong", 10);
timeVeryLong = EditorPrefs.GetFloat("powerupTimeVeryLong", 15);
}
void OnGUI()
{
GUILayout.Label("Power Up Settings", EditorStyles.boldLabel);
//Attack Range
showAttackRange = EditorGUILayout.Foldout(showAttackRange, "Attack Range");
if (showAttackRange)
{
rangeMelee = EditorGUILayout.FloatField(" Melee: ", rangeMelee);
rangeShort = EditorGUILayout.FloatField(" Short: ", rangeShort);
rangeMedium = EditorGUILayout.FloatField(" Medium: ", rangeMedium);
rangeLong = EditorGUILayout.FloatField(" Long: ", rangeLong);
rangeVeryLong = EditorGUILayout.FloatField(" Very Long: ", rangeVeryLong);
}
//Effect Range
showEffectRange = EditorGUILayout.Foldout(showEffectRange, "Effect Range");
if (showEffectRange)
{
effectSmall = EditorGUILayout.FloatField(" Small: ", effectSmall);
effectMedium = EditorGUILayout.FloatField(" Medium: ", effectMedium);
effectBig = EditorGUILayout.FloatField(" Big: ", effectBig);
}
//Push Force
showPushForce = EditorGUILayout.Foldout(showPushForce, "Push Force");
if (showPushForce)
{
pushShort = EditorGUILayout.FloatField(" Small: ", pushShort);
pushMedium = EditorGUILayout.FloatField(" Medium: ", pushMedium);
pushLong = EditorGUILayout.FloatField(" Long: ", pushLong);
}
//Time
showTime = EditorGUILayout.Foldout(showTime, "Time");
if (showTime)
{
timeVeryShort = EditorGUILayout.FloatField(" Very Short: ", timeVeryShort);
timeShort = EditorGUILayout.FloatField(" Short: ", timeShort);
timeMedium = EditorGUILayout.FloatField(" Medium: ", timeMedium);
timeLong = EditorGUILayout.FloatField(" Long: ", timeLong);
timeVeryLong = EditorGUILayout.FloatField(" Very Long: ", timeVeryLong);
}
EditorGUILayout.Space();
EditorGUILayout.Space();
if (GUILayout.Button("Apply", GUILayout.MaxWidth(50)))
{
SaveData();
}
}
}
Comment