一、保存数据
1.
using UnityEngine;
using System.Collections;
using UnityEditor;
/// <summary>
/// EditorPrefs可以序列化的数据有:bool/float/int/string
/// </summary>
public class TestEditor : EditorWindow {
string s;
[MenuItem("Window/TestEditor")]
static void Init()
{
EditorWindow.GetWindow<TestEditor>();
}
void OnEnable()
{
s = EditorPrefs.GetString(GetType().Name + "s");
}
void OnDisable()
{
EditorPrefs.SetString(GetType().Name + "s", s);
}
void OnGUI()
{
s = EditorGUILayout.TextField("data:", s);
}
}
2.
using UnityEngine;
using System.Collections;
using UnityEditor;
/// <summary>
/// NGUISettings是对EditorPrefs的进一步封装,可以序列化Object
/// </summary>
public class TestEditor2 : EditorWindow {
Object o;
[MenuItem("Window/TestEditor2")]
static void Init()
{
EditorWindow.GetWindow<TestEditor2>();
}
void OnEnable()
{
o = NGUISettings.Get<GameObject>(GetType().Name + "o", null);
}
void OnDisable()
{
NGUISettings.Set(GetType().Name + "o", o);
}
void OnGUI()
{
o = EditorGUILayout.ObjectField("data:", o, typeof(GameObject));
}
}
3.
using UnityEngine;
using System.Collections;
public class TestEditor3SO : ScriptableObject {
public int id;
public Vector3 pos;
public GameObject go;
}
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
public class TestEditor3 : EditorWindow {
public int id;
public Vector3 pos;
public GameObject go;
[MenuItem("Window/TestEditor3")]
static void Init()
{
EditorWindow.GetWindow<TestEditor3>();
}
void OnEnable()
{
string path = "Assets/Editor/Data/" + GetType().Name + ".asset";
if (!File.Exists(path)) return;
TestEditor3SO so = AssetDatabase.LoadAssetAtPath(path, typeof(TestEditor3SO)) as TestEditor3SO;
id = so.id;
pos = so.pos;
go = so.go;
}
void OnDisable()
{
TestEditor3SO so = CreateInstance<TestEditor3SO>();
so.id = id;
so.pos = pos;
so.go = go;
string path = "Assets/Editor/Data/" + GetType().Name + ".asset";
AssetDatabase.CreateAsset(so, path);
AssetDatabase.Refresh();
}
void OnGUI()
{
id = EditorGUILayout.IntField("id:", id);
pos = EditorGUILayout.Vector3Field("pos:", pos);
go = EditorGUILayout.ObjectField("go:", go, typeof(GameObject)) as GameObject;
}
}
二、