EditorPrefOptionSample.cs
using UnityEngine;
using UnityEditor;
namespace EyE.EditorUnity
{
namespace Examples
{
[System.Serializable]
public class SampleDataClass
{
public int anInt=100;
public string aString="default string";
public float aFloat=1.0f;
}
public class SampleDataPrefs<TInterfaceType> : PrefOptionBase<SampleDataClass, TInterfaceType> where TInterfaceType : IStorePreferences, new()
{
public SampleDataPrefs(string keyString, GUIContent guiLabel, bool alwaysLoadAndSaveOnValueAccess = true)
: base(new SampleDataClass(), keyString, guiLabel, alwaysLoadAndSaveOnValueAccess)
{
Load();
}
string anIntKey { get{ return Key + "SampleDataClass.IntKey"; } }
string aStringKey { get { return Key + "SampleDataClass.StringKey"; } }
string aFloatKey { get { return Key + "SampleDataClass.FloatKey"; } }
override protected bool HasKey()
{
return HasKey(anIntKey);
}
override public void Load()
{
if (!HasKey())
{
base.Load();// sets to default value
return;
}
Value.anInt = PrefOptionBase<int, TInterfaceType>.LoadAndCast(anIntKey);
Value.aString = PrefOptionBase<string, TInterfaceType>.LoadAndCast(aStringKey);
Value.aFloat = PrefOptionBase<float, TInterfaceType>.LoadAndCast(aFloatKey);
}
override public void Save()
{
PrefOptionBase<int, TInterfaceType>.Save(anIntKey, Value.anInt);
PrefOptionBase<string, TInterfaceType>.Save(aStringKey, Value.aString);
PrefOptionBase<float, TInterfaceType>.Save(aFloatKey, Value.aFloat);
}
override public void Delete()
{
PrefOptionBase<int, TInterfaceType>.Delete(anIntKey);
PrefOptionBase<int, TInterfaceType>.Delete(aStringKey);
PrefOptionBase<int, TInterfaceType>.Delete(aFloatKey);
}
}
public class EditorPrefOptionSample
{
static EditorPrefOption<float> floatValue = new EditorPrefOption<float>(3.14f, "Airspeed", new GUIContent("Air speed", "Average airspeed of an unladen swallow."));
static PlayerPrefOption<string> stringValue = new PlayerPrefOption<string>("To seek the holy grail.", "Quest", new GUIContent("Your quest", "What is your quest?"));
static EditorPrefOption<Color> colorValue = new EditorPrefOption<Color>(/*Color.blue*/Color.yellow, "Color", new GUIContent("favorite color", "What is your favorite Color?"));
//static PrefOptionBase<Object,EditorPrefInterface> SaveObjectPath = new PrefOptionBase<Object, EditorPrefInterface>(null, "SampleSaveObject", new GUIContent("SampleObject", "saves path to object"));
//static PrefOptionBase<Object, PlayerPrefInterface> SaveObjectPath = new PrefOptionBase<Object, PlayerPrefInterface>(null, "SampleSaveObject", new GUIContent("SampleObject", "saves path to object"));
static PrefOptionBase<Object, XmlPreferenceInterface> SaveObjectPath = new PrefOptionBase<Object, XmlPreferenceInterface>(null, "SampleSaveObject", new GUIContent("SampleObject", "saves path to object"));
static EditorPrefOptionArray<string> listValue = new EditorPrefOptionArray<string>(new string[3] { "one", "two", "three" }, "Sample array object", new GUIContent("list example", "Array ListExample"));
static PrefOptionArrayBase<string, XmlPreferenceInterface> xmlListValue = new PrefOptionArrayBase<string, XmlPreferenceInterface>(new string[3] { "one", "two", "three" }, "SampleXmlArrayObject", new GUIContent("xmlList example", "Array ListExample"));
static XmlPrefOption<string> xmlStringValue = new XmlPrefOption<string>("To seek the holy grail.", "Quest", new GUIContent("Your quest", "What is your quest?"));
static SampleDataPrefs<XmlPreferenceInterface> classObject = new SampleDataPrefs<XmlPreferenceInterface>("SampleClassObjectKey", new GUIContent("A whole Object"));
[PreferenceItem("Sample Settings")] //this unity attribute will use this function to draw a new tab (called "Sample Settings"), under Edit->Preferences in the unity editor
public static void OnGUI()
{
floatValue.Value = EditorGUILayout.FloatField(floatValue.Label, floatValue.Value);
stringValue.Value = EditorGUILayout.TextField(stringValue.Label, stringValue.Value);
xmlStringValue.Value = EditorGUILayout.TextField(xmlStringValue.Label, xmlStringValue.Value);
colorValue.Value = EditorGUILayout.ColorField(colorValue.Label, colorValue.Value);
SaveObjectPath.Value = (Object)EditorGUILayout.ObjectField(SaveObjectPath.Label, SaveObjectPath.Value, typeof(Object), false);
PropHolder tempHolder = ScriptableObject.CreateInstance<PropHolder>();
tempHolder.list = listValue.Value;
tempHolder.xmlList = xmlListValue.Value;
tempHolder.sampleObject = classObject.Value;
SerializedObject so = new SerializedObject(tempHolder);
SerializedProperty prop = so.FindProperty("list");
EditorGUILayout.PropertyField(prop, listValue.Label, true, null);
SerializedProperty xmlProp = so.FindProperty("xmlList");
EditorGUILayout.PropertyField(xmlProp, xmlListValue.Label, true, null);
SerializedProperty objProp = so.FindProperty("sampleObject");
EditorGUILayout.PropertyField(objProp, classObject.Label, true, null);
so.ApplyModifiedProperties();
listValue.Value = tempHolder.list;
xmlListValue.Value = tempHolder.xmlList;
classObject.Value = tempHolder.sampleObject;
ScriptableObject.DestroyImmediate(tempHolder);
if (GUILayout.Button("Reset options to defaults."))
{
floatValue.SetToDefault();
stringValue.SetToDefault();
colorValue.SetToDefault();
SaveObjectPath.SetToDefault();
}// end reset button
// XmlPreferenceInterface.SaveSingleton();
}//end onGUI function
[System.Serializable]
class PropHolder : ScriptableObject
{
public string[] list;
public string[] xmlList;
public SampleDataClass sampleObject;
}
}// end EditorPrefOptionSample class
}
}
/// example EditorPrefOptionSample.cs
EyE.EditorUnity.Examples.EditorPrefOptionSample.OnGUI
static void OnGUI()
In this callback-function the EditorPrefOptios automatically load the preference values,...
Definition: EditorPrefOptionSample.cs:105
EyE.Unity.Extensions.PrefOptionBase< SampleDataClass, TInterfaceType >::HasKey
static bool HasKey(string keyParam)
Confirm the key is already stored in preferences
Definition: PrefOptionBase.cs:370
EyE.Unity.Extensions.PrefOptionBase< SampleDataClass, TInterfaceType >::Delete
static void Delete(string keyParam)
The static function will delete all preferences of ValueType specified by the key.
Definition: PrefOptionBase.cs:402
EyE.EditorUnity.Extensions
Definition: EditorPrefInterface.cs:5
EyE
This namespace contains classes that provide various tools for use in the Unity Editor.
Definition: EmbededXMLTooltip.cs:5
EyE.Unity.Extensions.PrefOptionBase< SampleDataClass, TInterfaceType >::Save
static void Save(string keyParam, object _value)
A Primary Function of this class, this static version takes a key and an object, determines the appro...
Definition: PrefOptionBase.cs:293
EyE.Unity.Extensions.PrefOptionBase< SampleDataClass, TInterfaceType >::Key
string Key
The unique key used to reference this prefOption in storage.
Definition: PrefOptionBase.cs:97
EyE.Unity.Extensions
Definition: IStorePreferences.cs:3
EyE.Unity
This namespace holds classes that extended or inherit from various Unity defined classes....
Definition: EmbededXMLTooltip.cs:5
EyE.Unity.Extensions.PrefOptionBase< SampleDataClass, TInterfaceType >::Value
virtual ValueType Value
This Accessor's get function returns the current value of this preference. This current value is load...
Definition: PrefOptionBase.cs:140
EyE.EditorUnity
This namespace is used to provide a User Interface, in Unity's Editor, for various library classes....
Definition: ExtractEyeEnginesLogo.cs:6
EyE.Unity.Extensions.PrefOptionBase< SampleDataClass, TInterfaceType >::Load
static object Load(string keyParam)
This function will attempt to retrieve from storage, the preference value specified by the key-parame...
Definition: PrefOptionBase.cs:221