前言
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打包成ab
加载AB包并从反序列化数据
Resources加载方式和编辑器加载就不展示了,直接从ab包里反序列化数据文章来源:https://www.toymoban.com/news/detail-853037.html
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);
}
文章来源地址https://www.toymoban.com/news/detail-853037.html
到了这里,关于[游戏开发][Unity] ScriptableObject数据创建与各种加载方式的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!