ScriptableObject類型經常用於存儲一些unity3d本身不可以打包的一些object,比如字符串,一些類對象等。用這個類型的 子類型,則可以用BuildPipeline打包成assetbundle包供后續使用,非常方便。這是除了PlayerPrefs和c# XML文件讀取外,另外 的一種存取一些數據對象的方法
- using UnityEngine;
- public class XMLContains : ScriptableObject
- {
- public string theXML;
- }
By making your class enherit from ScriptableObject, it becomes an "Asset Type" to unity, in the sense that you can store them on disk, and use the Unity IDE interface to drag it from the assetsfolder to a behaviour or whatever. It also automatically plays nice with Unity's dependency tracking system that decides what does and what does not go into a webplayer. Going a bit further with this example, you could have let's say a TieFighter class, actually get some of its information (max health, max speed, acceleration), from your xml file, by making the TieFighter class look like this:
通過使你的類從ScriptableObject腳本對象化這個類派生,使你的類稱為一種資源類型,這樣的話,你可以在硬盤上存儲他們,並使用unity3d的IDE接口去資源文件拖拽他們的行為或者任何其他。
- using UnityEngine;
- public class TieFighter : MonoBehaviour
- {
- public XMLContainer myXMLSettings;
- void Awake()
- {
- //parse myXMLSettings.theXML into reasonable data
- }
- void Update()
- {
- //use the reasonable data
- }
- }
You could then attach the TieFighter script to a gameobject, and then drag your xml-asset from your assetsfolder onto the myXMLSettings public field.Please note that this example is much better at showing how you could use a ScriptableObject than how to properly implement a TieFighter, as I would never really have that parse an actual xml file on runtime. Tiefighters are too cool for xml anyway.
以上是不做成bundle包的形式,下面提供了一種打包成bundle資源,通過www來進行載入的方案:
通常,可以用BuildPipeline.BuildAssetBundle打包資源,這些資源包括模型、紋理、貼圖等。其實也可以打包ScriptableObject,而ScriptableObject中可以存儲任何數據類型。
以下為一個完整示例。
1、先創建一個類:
- using UnityEngine;
- using System.Collections.Generic;
- public class SysData : ScriptableObject
- {
- public List<Vector3> content;
- }
該類只有一個屬性content。當然也可以聲明更多屬性、事件和方法。
在后面,這個類的實例將作為資源被打包。
2、創建一個編輯器腳本:
該腳本用於實例化SysData,設置一些數據,然后打包。
- using UnityEngine;
- using UnityEditor;
- using System.Collections.Generic;
- public class Export
- {
- [MenuItem("Assets/Export")]
- public static void Execute()
- {
- //實例化SysData
- SysData sd = ScriptableObject.CreateInstance<SysData>();
- //隨便設置一些數據給content
- sd.content = new List<Vector3>();
- sd.content.Add(new Vector3(1,2,3));
- sd.content.Add(new Vector3(4,5,6));
- // SysData將創建為一個對象,這時在project面板上會看到這個對象。
- string p = "Assets/SysData.asset";
- AssetDatabase.CreateAsset(sd, p);
- Object o = AssetDatabase.LoadAssetAtPath(p, typeof(SysData));
- //打包為SysData.assetbundle文件。
- BuildPipeline.BuildAssetBundle(o, null, "SysData.assetbundle");
- //刪除面板上的那個臨時對象
- AssetDatabase.DeleteAsset(p);
- }
- }
3、運行時加載這個數據資源。
- IEnumerator Start ()
- {
- WWW www = new WWW("file://" + Application.dataPath + "/../SysData.assetbundle");
- yield return www;
- //轉換資源為SysData,這個sd對象將擁有原來在編輯器中設置的數據。
- SysData sd = www.assetBundle.mainAsset as SysData;
- //如打印sd.content[0],將得到Vector3(1,2,3);
- print(sd.content[0]);
- }
