Preface
ScriptableObject supports serializing class object data into Unity asset files and deserializing it into class objects. This article explains the use of ScriptableObject through two cases. The first is a simple case, serializing simple bool values and Sting data. The second is a complex case, where the class object is nested within the class object and serialized into an asset file, and then deserialized out.
Case 1
[CreateAssetMenu]
public class GameSetting : ScriptableObject
{
public static GameSetting Instance;
public bool AssetbundleMode;
public string GameName = "Story";
}
Case 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>();
}
Text
Let’s start with the first case, the serialized data is as follows
using UnityEngine;
[CreateAssetMenu]
public class GameSetting : ScriptableObject
{
public static GameSetting Instance;
public bool AssetbundleMode;
public string GameName = "Test!!!!!";
}
Two ways to create Unity Aseet files
Creation method 1, create through the menu, the file will appear in the Assets folder
Creation method 2, through code creation, you can set the creation path and set the data
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();
}
Three ways to load GameSetting.asset
Method 1: Resources loading
GameSetting setting = Resources.Load<GameSetting>("GameSetting");
Method 2: Editor loading
private void EditorLoadGameSetting()
{
GameSetting setting = AssetDatabase.LoadAssetAtPath<GameSetting>("Assets/Works/Resource/AllGameSetting/GameSetting.asset");
Debug.LogError(setting.AssetbundleMode);
FSMManager.Instance.Init();
}
Method 3: assetbundle loads GameSetting
Package this GameSetting.asset file into an ab package
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);
}
Case 2
Let’s start with the second complex case.
You can create a Unity Asset file by calling the following code
private void GetBundleRelationFile()
{
string path = "Assets/Works/Resource/AllGameSetting/TestData.asset";
TestScriptableObject testData = ScriptableObject.CreateInstance<TestScriptableObject>();
AssetDatabase.CreateAsset(test, path);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
Then package this Asset into ab
Load AB package and deserialize data from
Resources loading method and editor loading will not be displayed. Data is deserialized directly from the ab package.
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);
}