鸟语天空
ScriptableObject
post by:追风剑情 2015-12-31 17:58

继承ScriptableObject的子类对象可以被序列化成文件保存。

3.png

1.png

2.png

 

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";
    }
}

 

评论:
发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容