前言
ScriptableObject支持把类对象数据序列化成Unity asset文件,同时支持反序列化成类对象。本文章通过两个案例讲解ScriptableObject的使用方式,第一个是简单案例,序列化简单的bool值和Sting数据。第二个是复杂案例,在类对象里嵌套类对象并序列化成asset文件,再把它反序列化出来。
案例1
[CreateAssetMenu]
public class GameSetting : ScriptableObject
{
public static GameSetting Instance;
public bool AssetbundleMode;
public string GameName = "Story";
}
案例2
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[Serializable]
public class TestData
{
public string Name;
public int index;
}
[CreateAssetMenu]
public class TestScriptableObject : ScriptableObject
{
public List<TestData> dataList = new List<TestData>();
}
正文
下面开始第一个案例,序列化数据如下
using UnityEngine;
[CreateAssetMenu]
public class GameSetting : ScriptableObject
{
public static GameSetting Instance;
public bool AssetbundleMode;
public string GameName = "Test!!!!!";
}
创建Unity Aseet文件的两种方式
创建方式1,通过菜单创建,文件会出现在Assets文件夹内
创建方式2,通过代码创建,可以设置创建路径,并设置数据
private void CreateGameSetting()
{
string path = "Assets/Works/Resource/AllGameSetting/GameSetting.asset";
GameSetting setting = ScriptableObject.CreateInstance<GameSetting>();
setting.AssetbundleMode = true;
setting.GameName = "Test!!!!!";
AssetDatabase.CreateAsset(setting, path);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
加载GameSetting.asset的三种方式
方式1:Resources加载
GameSetting setting = Resources.Load<GameSetting>("GameSetting");
方式2:编辑器加载
private void EditorLoadGameSetting()
{
GameSetting setting = AssetDatabase.LoadAssetAtPath<GameSetting>("Assets/Works/Resource/AllGameSetting/GameSetting.asset");
Debug.LogError(setting.AssetbundleMode);
FSMManager.Instance.Init();
}
方式3:assetbundle加载GameSetting
把这个GameSetting.asset文件打包成ab包
void LoadGameSettingFromBundle()
{
string loadPath = Application.streamingAssetsPath + "/assets/works/resource/allgamesetting/gamesetting.unity3d";
AssetBundle bundle = AssetBundle.LoadFromFile(loadPath);
//全路径加载也可以
//GameSetting.Instance = bundle.LoadAsset<GameSetting>("assets/works/resource/allgamesetting/gamesetting.asset");
GameSetting.Instance = bundle.LoadAsset<GameSetting>("GameSetting.asset");
Debug.LogError("GameName: " + GameSetting.Instance.GameName);
}
下面开始第二个复杂案例。
调用下面的代码都可以创建一个Unity Asset文件
private void GetBundleRelationFile()
{
string path = "Assets/Works/Resource/AllGameSetting/TestData.asset";
TestScriptableObject testData = ScriptableObject.CreateInstance<TestScriptableObject>();
AssetDatabase.CreateAsset(test, path);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
填充数据到Asset内
加载BundleRelation.asset的三种方式
方式1:Resources加载
把BundleRelation.asset拷贝到Resources文件夹内,执行下面代码
BundleRelation relation = Resources.Load<BundleRelation>("BundleRelation");
方式2:Editor加载
private void EditorLoadBundleRelation()
{
BundleRelation setting = AssetDatabase.LoadAssetAtPath<BundleRelation>("Assets/Works/Resource/AllGameSetting/BundleRelation.asset");
Debug.LogError(setting.AssetRefs.Length);
}
方式3:Bundle加载
先打包成bundle
public void LoadBundleRelation()
{
string loadPath = Application.streamingAssetsPath + "/assets/works/resource/allgamesetting/testdata.unity3d";
AssetBundle bundle = AssetBundle.LoadFromFile(loadPath);
TestScriptableObject TestData = bundle.LoadAsset<TestScriptableObject>("TestData.asset");
Debug.LogError("TestScriptableObject.TestData.dataList[1].Name: " + TestData.dataList[1].Name);
Debug.LogError("TestScriptableObject.TestData.dataList[1].Index: " + TestData.dataList[1].index);
}
出现报错的情况
我刚才测试特别复杂的类对象,多级嵌套,出现了反序列化失败的情况,可能是我某个地方写的有问题,没时间看了,先记录下载,以后有机会解决