- 使用 ScriptableObject 保存到硬盘后在编辑器中可以二次编辑
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
public class TestScriptableObject : Editor {
[MenuItem("Tools/Serialize")]
private static void Serialize () {
Rect rect = ScriptableObject.CreateInstance<Rect>();
rect.width = 10;
rect.height = 20;
// *注意 AssetDatabase.CreateAsset 方法使用的是相对路径
string path = "Assets/Data";
if (!Directory.Exists(path)) {
Directory.CreateDirectory(path);
}
// Assets/Data/Rect.asset
path += $"/Rect.asset";
AssetDatabase.CreateAsset(rect, path);
}
[MenuItem("Tools/Deserialize")]
private static void Deserialize () {
Rect rect = AssetDatabase.LoadAssetAtPath<Rect>("Assets/Data/Rect.asset");
Debug.Log(rect.width); // 10
Debug.Log(rect.height); // 20
}
public class Rect : ScriptableObject {
public int width;
public int height;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
public class TestBinary : Editor {
[MenuItem("Tools/BinarySerialize")]
private static void BinarySerialize () {
Rect rect = new Rect();
rect.width = 10;
rect.height = 20;
string path = Application.dataPath + "/Data/Rect.bytes";
FileStream fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(fileStream,rect);
fileStream.Close();
AssetDatabase.Refresh(); // 保存到 Assets 文件夹下需要刷新才能看到
}
[MenuItem("Tools/BinaryDeserialize")]
private static void BinaryDeserialize () {
// 非 Assets 文件夹下时,使用 byte[] bytes = File.ReadAllBytes(path);
TextAsset textAsset = AssetDatabase.LoadAssetAtPath<TextAsset>("Assets/Data/Rect.bytes");
MemoryStream memoryStream = new MemoryStream(textAsset.bytes);
BinaryFormatter binaryFormatter = new BinaryFormatter();
Rect rect = (Rect)binaryFormatter.Deserialize(memoryStream);
memoryStream.Close();
Debug.Log(rect.width); // 10
Debug.Log(rect.height); // 20
}
// * 注意必须标记可序列化
[System.Serializable]
public class Rect{
public int width;
public int height;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Xml.Serialization;
public class TestXml : Editor {
[MenuItem("Tools/XmlSerialize")]
private static void XmlSerialize () {
Rect rect = new Rect();
rect.width = 10;
rect.height = 20;
string path = Application.dataPath + "/Data/Rect.xml";
FileStream fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
StreamWriter streamWriter = new StreamWriter(fileStream, System.Text.Encoding.UTF8);
XmlSerializer xmlSerializer = new XmlSerializer(rect.GetType());
xmlSerializer.Serialize(streamWriter, rect);
streamWriter.Close();
fileStream.Close();
AssetDatabase.Refresh(); // 保存到 Assets 文件夹下需要刷新才能看到
}
[MenuItem("Tools/XmlDeserialize")]
private static void XmlDeserialize () {
string path = Application.dataPath + "/Data/Rect.xml";
FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Rect));
Rect rect = (Rect)xmlSerializer.Deserialize(fileStream);
fileStream.Close();
Debug.Log(rect.width); // 10
Debug.Log(rect.height); // 20
}
// * 注意必须标记可序列化
[System.Serializable]
public class Rect {
public int width;
public int height;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
public class TestJson : Editor {
[MenuItem("Tools/JsonSerialize")]
private static void JsonSerialize () {
Rect rect = new Rect();
rect.width = 10;
rect.height = 20;
string jsonString = JsonUtility.ToJson(rect);
string path = Application.dataPath + "/Data/Rect.json";
File.WriteAllText(path, jsonString);
AssetDatabase.Refresh(); // 保存到 Assets 文件夹下需要刷新才能看到
}
[MenuItem("Tools/JsonDeserialize")]
private static void JsonDeserialize () {
string path = Application.dataPath + "/Data/Rect.json";
StreamReader streamReader = File.OpenText(path);
string jsonString = streamReader.ReadToEnd();
streamReader.Close();
Rect rect = JsonUtility.FromJson<Rect>(jsonString);
Debug.Log(rect.width); // 10
Debug.Log(rect.height); // 20
}
public class Rect {
public int width;
public int height;
}
}
#if UNITY_EDITOR
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class RefData : ScriptableObject {
public Foo[] foos;
}
[System.Serializable]
public class Foo {
public int a;
public Vector2 b;
}
[System.Serializable]
public class Goo : Foo {
public string c;
}
public class EditorTest : Editor {
[MenuItem("Tools/Serialize")]
private static void Serialize() {
var refData = ScriptableObject.CreateInstance<RefData>();
refData.foos = new Foo[3];
refData.foos[0] = new Foo { a = 0, b = Vector2.up };
refData.foos[1] = new Foo { a = 1, b = Vector2.down };
refData.foos[2] = new Goo { a = 2, b = Vector2.left, c = "c" };
AssetDatabase.CreateAsset(refData, "Assets/RefData.asset");
}
[MenuItem("Tools/Deserialize")]
private static void Deserialize() {
RefData refData = AssetDatabase.LoadAssetAtPath<RefData>("Assets/RefData.asset");
// 当重载加载当前程序集时(重新编译脚本或重启unity),输出 false
Debug.Log(refData.foos[2] is Goo);
}
}
#endif