美文网首页
ScriptableObject学习笔记

ScriptableObject学习笔记

作者: _ArayA_ | 来源:发表于2020-02-18 10:30 被阅读0次

不同与C#提供的Serializable序列化功能,ScriptableObject是Unity3D提供的一个数据存储类,我们接下来学习一下这个类的功能。

官方文档

http://docs.unity3d.com/Manual/class-ScriptableObject.html
http://docs.unity3d.com/ScriptReference/ScriptableObject.html

使用情景

在Unity3D中,我们记录游戏的配置数据可以使用文件文件(XML、JSON等格式),也可以使用二进制文件(自定义格式),两种方式都需要自己进行解析,而Unity比较贴心,为我们提供了另外一种格式的数据记录方式,就是ScriptableObject。

简单的示例

下面我们来看一个简单的示例。

首先我们需要创建记录配置的类,如下:

ShopConfig,这个类是会被作为配置数据打包到AssetBundle中的类,所以必须要继承自ScriptableObject,同时要注意文件名必须和类名一致:

using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 商品配置表.
/// </summary>
public class ShopConfig : ScriptableObject
{
    /// <summary>
    /// 商品页签枚举.
    /// </summary>
    public enum ShopTag
    {
        hot,
        item,
        weapon
    }

    /// <summary>
    /// 商品列表.
    /// </summary>
    public List<ShopListInfo> ShopList;
}

ShopListInfo,这个类被ShopConfig引用也会被打包到AssetBundle中,但是其不会作为打包的数据类型所以不用继承ScriptableObject,但是必须添加[System.Serializable]的Attribute:

using System.Collections.Generic;

/// <summary>
/// 指定页签的商品列表.
/// </summary>
[System.Serializable]
public class ShopListInfo
{
    /// <summary>
    /// 页签.
    /// </summary>
    public ShopConfig.ShopTag tag;

    /// <summary>
    /// 商品列表.
    /// </summary>
    public List<ShopItemInfo> list;
}

ShopItemInfo,同上:

/// <summary>
/// 商品.
/// </summary>
[System.Serializable]
public class ShopItemInfo
{
    /// <summary>
    /// 名称.
    /// </summary>
    public string name;

    /// <summary>
    /// 价格.
    /// </summary>
    public int price;
}

下面我们要创建用于打包的脚本:

using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class CreateConfig
{
    [MenuItem("Tools/CreateConfig")]
    private static void Create()
    {
        CreateShopConfig();
    }

    private static void CreateShopConfig()
    {
        ShopConfig shopConfig = ScriptableObject.CreateInstance<ShopConfig>();

        //填充数据, 可以从外部有策划配置好的配置表(如CSV、XML、JSON甚至是二进制文件)中通过通用代码读取所有数据来进行填充
        //这里只是测试就直接手写了(⊙﹏⊙)b

        shopConfig.ShopList = new List<ShopListInfo>();

        ShopListInfo list = new ShopListInfo();
        list.tag = ShopConfig.ShopTag.hot;
        list.list = new List<ShopItemInfo>();
        list.list.Add(new ShopItemInfo { name = "优你弟内裤", price = 10000 });
        list.list.Add(new ShopItemInfo { name = "扣扣死内裤", price = 5000 });
        list.list.Add(new ShopItemInfo { name = "内裤", price = 100 });
        shopConfig.ShopList.Add(list);

        list = new ShopListInfo();
        list.tag = ShopConfig.ShopTag.item;
        list.list = new List<ShopItemInfo>();
        list.list.Add(new ShopItemInfo { name = "金疮药", price = 250 });
        list.list.Add(new ShopItemInfo { name = "和合散", price = 500 });
        shopConfig.ShopList.Add(list);

        list = new ShopListInfo();
        list.tag = ShopConfig.ShopTag.weapon;
        list.list = new List<ShopItemInfo>();
        list.list.Add(new ShopItemInfo { name = "轩辕剑", price = 1 });
        list.list.Add(new ShopItemInfo { name = "桃木剑", price = 5 });
        list.list.Add(new ShopItemInfo { name = "小李飞刀", price = 213 });
        list.list.Add(new ShopItemInfo { name = "大李飞刀", price = 313 });
        shopConfig.ShopList.Add(list);

        //填充好数据后就可以打包到 AssetBundle 中了
        //第一步必须先创建一个保存了配置数据的 Asset 文件, 后缀必须为 asset
        AssetDatabase.CreateAsset(shopConfig, "Assets/ShopConfig.asset");

        //第二步就可以使用 BuildPipeline 打包了
        BuildPipeline.BuildAssetBundle(null, new[]
            {
                AssetDatabase.LoadAssetAtPath("Assets/ShopConfig.asset", typeof(ShopConfig))
            },
            Application.streamingAssetsPath + "/Config.assetbundle",
            BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets | BuildAssetBundleOptions.UncompressedAssetBundle,
            BuildTarget.StandaloneWindows64);
    }
}

我们运行一下,就可以打包出AssetBundle了,这里要注意两点:

继承自ScriptableObject的类不能使用new来创建,要使用ScriptableObject.CreateInstance<T>()方法来创建;
必须先创建对应的Asset文件才能打包,同时Asset文件的后缀必须是asset,否则Unity不能识别;
打包好了,我们弄个脚本加载看看,如下:

using UnityEngine;
using System.Collections;

public class TestScript : MonoBehaviour
{
    void Start()
    {
        AssetBundle assetBundle = AssetBundle.CreateFromFile(Application.streamingAssetsPath + "/Config.assetbundle");

        ShopConfig shopConfig = assetBundle.Load("ShopConfig", typeof(ShopConfig)) as ShopConfig;
        Debug.Log(shopConfig.ShopList.Count);
    }
}

挂到摄像机就行了,我们看看结果:

image

数据正确没问题。

Asset文件

等等,我们好像忘了啥,创建出的Asset文件有啥用呢,我们点击该文件可以直接在Inspector窗口直接编辑!

image

这个绝了,策划直接连Excel啥别的配置工具都不需要,用Unity就可以直接编辑和配置了,不过唯一的缺点就是打出来的数据也就Unity能用了,其它语言比如后台要使用得先弄清楚Asset文件的数据结构才行(后台总不能用Unity写吧)。

总结

优点

  1. 除了支持float、int、string等常见的类型外,还支持List等复杂数据类型,最重要的支持Unity的Vector3等数据;
  2. 创建出来的文件可以直接在Unity中编辑;

缺点

  1. 其它语言要解析数据需要了解详细的格式,而且要花时间编写解析代码;
  2. 对于大量的数据还是Excel用起来舒服一点;
  3. 我要是配置表的结构改变了,asset文件中填好的数据是不是就要报废了?

使用方式

我认为有两种使用方式:

  1. 使用Excel啥的进行配置,保存为csv,打包数据时读取csv表的数据填充asset文件;
  2. 创建空的asset文件直接在Unity中编辑;

相关文章

网友评论

      本文标题:ScriptableObject学习笔记

      本文链接:https://www.haomeiwen.com/subject/qnowfhtx.html