在Unity3D中使用ScriptableObject進行序列化

來源:互聯網
上載者:User

 

ScriptableObject類型經常用於儲存一些unity3d本身不可以打包的一些object,比如字串,一些類對象等。用這個類型的子類型,則可以用BuildPipeline打包成assetbundle包供後續使用,非常方便。這樣除了playerpref和c#檔案讀取外,另外的一種存取一些資料對象的方法

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:
 

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]);}

 

4、這其實是把整個對象執行個體打包,如果ScriptableObject屬性中包括其他的類執行個體,則這些類需加上[Serializable]序列化。通過這種方法,可以將系統需要的資料(如角色分類、matrix4x4資料等等)打包,載入後直接轉換為預定的類型,具有更高的效率。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.