继承ScriptableObject的子类对象可以被序列化成文件保存。
ScriptableObjectEditor.cs
using UnityEngine; using UnityEditor; using System.Collections; public class ScriptableObjectEditor { [MenuItem("Assets/Export/TestData")] private static void ExportData() { Object data = ScriptableObject.CreateInstance<ScriptableObjectTest>(); //data.asset可以在编辑器中通过AssetDatabase.LoadAssetAtPath()加载,或者运行时通过Resources.Load()加载 AssetDatabase.CreateAsset(data, "Assets/data.asset"); //data.bundle需要在运行时通过WWW加载 BuildAssets(data, null, "Assets/data.bundle"); AssetDatabase.Refresh(); } public static void BuildAssets(UnityEngine.Object mainAsset, UnityEngine.Object[] assets, string filePath) { BuildPipeline.BuildAssetBundle(mainAsset, assets, filePath, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, BuildTarget.Android); AssetDatabase.Refresh(); } }
ScriptableObjectTest.cs
using UnityEngine; using System.Collections; public class ScriptableObjectTest : ScriptableObject { public string[] strArr = new string[3]; public ScriptableObjectTest() { strArr[0] = "str0"; strArr[1] = "str1"; strArr[2] = "str2"; } }