using UnityEngine;
using UnityEditor;
{
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)
{
}
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()
{
}
override public void Load()
{
{
base.Load();
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);
}
{
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.yellow, "Color", new GUIContent("favorite color", "What is your favorite Color?"));
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")]
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();
}
}
[System.Serializable]
class PropHolder : ScriptableObject
{
public string[] list;
public string[] xmlList;
public SampleDataClass sampleObject;
}
}
}
}