zoukankan      html  css  js  c++  java
  • Unity3D之ScriptableObject学习笔记

    不同与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,同时要注意文件名必须和类名一致:

     1 using System.Collections.Generic;
     2 using UnityEngine;
     3 
     4 /// <summary>
     5 /// 商品配置表.
     6 /// </summary>
     7 public class ShopConfig : ScriptableObject
     8 {
     9     /// <summary>
    10     /// 商品页签枚举.
    11     /// </summary>
    12     public enum ShopTag
    13     {
    14         hot,
    15         item,
    16         weapon
    17     }
    18 
    19     /// <summary>
    20     /// 商品列表.
    21     /// </summary>
    22     public List<ShopListInfo> ShopList;
    23 }

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

     1 using System.Collections.Generic;
     2 
     3 /// <summary>
     4 /// 指定页签的商品列表.
     5 /// </summary>
     6 [System.Serializable]
     7 public class ShopListInfo
     8 {
     9     /// <summary>
    10     /// 页签.
    11     /// </summary>
    12     public ShopConfig.ShopTag tag;
    13 
    14     /// <summary>
    15     /// 商品列表.
    16     /// </summary>
    17     public List<ShopItemInfo> list;
    18 }

    ShopItemInfo,同上:

     1 /// <summary>
     2 /// 商品.
     3 /// </summary>
     4 [System.Serializable]
     5 public class ShopItemInfo
     6 {
     7     /// <summary>
     8     /// 名称.
     9     /// </summary>
    10     public string name;
    11 
    12     /// <summary>
    13     /// 价格.
    14     /// </summary>
    15     public int price;
    16 }

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

     1 using System.Collections.Generic;
     2 using UnityEditor;
     3 using UnityEngine;
     4 
     5 public class CreateConfig
     6 {
     7     [MenuItem("Tools/CreateConfig")]
     8     private static void Create()
     9     {
    10         CreateShopConfig();
    11     }
    12 
    13     private static void CreateShopConfig()
    14     {
    15         ShopConfig shopConfig = ScriptableObject.CreateInstance<ShopConfig>();
    16 
    17         //填充数据, 可以从外部有策划配置好的配置表(如CSV、XML、JSON甚至是二进制文件)中通过通用代码读取所有数据来进行填充
    18         //这里只是测试就直接手写了(⊙﹏⊙)b
    19 
    20         shopConfig.ShopList = new List<ShopListInfo>();
    21 
    22         ShopListInfo list = new ShopListInfo();
    23         list.tag = ShopConfig.ShopTag.hot;
    24         list.list = new List<ShopItemInfo>();
    25         list.list.Add(new ShopItemInfo { name = "优你弟内裤", price = 10000 });
    26         list.list.Add(new ShopItemInfo { name = "扣扣死内裤", price = 5000 });
    27         list.list.Add(new ShopItemInfo { name = "内裤", price = 100 });
    28         shopConfig.ShopList.Add(list);
    29 
    30         list = new ShopListInfo();
    31         list.tag = ShopConfig.ShopTag.item;
    32         list.list = new List<ShopItemInfo>();
    33         list.list.Add(new ShopItemInfo { name = "金疮药", price = 250 });
    34         list.list.Add(new ShopItemInfo { name = "和合散", price = 500 });
    35         shopConfig.ShopList.Add(list);
    36 
    37         list = new ShopListInfo();
    38         list.tag = ShopConfig.ShopTag.weapon;
    39         list.list = new List<ShopItemInfo>();
    40         list.list.Add(new ShopItemInfo { name = "轩辕剑", price = 1 });
    41         list.list.Add(new ShopItemInfo { name = "桃木剑", price = 5 });
    42         list.list.Add(new ShopItemInfo { name = "小李飞刀", price = 213 });
    43         list.list.Add(new ShopItemInfo { name = "大李飞刀", price = 313 });
    44         shopConfig.ShopList.Add(list);
    45 
    46         //填充好数据后就可以打包到 AssetBundle 中了
    47         //第一步必须先创建一个保存了配置数据的 Asset 文件, 后缀必须为 asset
    48         AssetDatabase.CreateAsset(shopConfig, "Assets/ShopConfig.asset");
    49 
    50         //第二步就可以使用 BuildPipeline 打包了
    51         BuildPipeline.BuildAssetBundle(null, new[]
    52             {
    53                 AssetDatabase.LoadAssetAtPath("Assets/ShopConfig.asset", typeof(ShopConfig))
    54             },
    55             Application.streamingAssetsPath + "/Config.assetbundle",
    56             BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets | BuildAssetBundleOptions.UncompressedAssetBundle,
    57             BuildTarget.StandaloneWindows64);
    58     }
    59 }

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

    1. 继承自ScriptableObject的类不能使用new来创建,要使用ScriptableObject.CreateInstance<T>()方法来创建;
    2. 必须先创建对应的Asset文件才能打包,同时Asset文件的后缀必须是asset,否则Unity不能识别;

    打包好了,我们弄个脚本加载看看,如下:

     1 using UnityEngine;
     2 using System.Collections;
     3 
     4 public class TestScript : MonoBehaviour
     5 {
     6     void Start()
     7     {
     8         AssetBundle assetBundle = AssetBundle.CreateFromFile(Application.streamingAssetsPath + "/Config.assetbundle");
     9 
    10         ShopConfig shopConfig = assetBundle.Load("ShopConfig", typeof(ShopConfig)) as ShopConfig;
    11         Debug.Log(shopConfig.ShopList.Count);
    12     }
    13 }

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

    数据正确没问题。

    Asset文件

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

    这个绝了,策划直接连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中编辑;
  • 相关阅读:
    宋体、新宋体、仿宋体
    单例模式————你就是我的唯一
    极乐净土代码——第一辑
    泛函是个什么概念
    http和https的区别
    get和post的区别
    浏览器输入URL按回车后都经历了什么?
    求两个列表的交集、并集、差集
    使用lamdba函数对list排序
    乐观锁和悲观锁的区别
  • 原文地址:https://www.cnblogs.com/hammerc/p/4829934.html
Copyright © 2011-2022 走看看