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中編輯;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM