ScriptableObject

作者:追风剑情 发布于:2015-12-31 17:58 分类:Unity3d

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

 

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号