assetbundle和ScriptableObject的使用

 1 using System.Collections;
 2 using System;
 3 
 4 [Serializable]
 5 public class ExcelData : ScriptableObject {
 6 
 7     public string excelName;
 8     public string Data;
 9     public string ItemName;
10 }
 1 using UnityEngine;
 2 using System.Collections;
 3 using UnityEditor;
 4 using System.Collections.Generic;
 5 
 6 
 7 public class BuildExcelEditor : Editor {
 8 
 9     [MenuItem("Build AssetsBundle/CreateAssets")]
10     public static void CreateAssets() {
11         ExcelData excelData = ScriptableObject.CreateInstance<ExcelData>();
12         excelData.excelName = "CharacterConfig";
13         excelData.ItemName = "superman";
14         excelData.Data = "1";
15         string path= "Assets/CharacterConfig.asset";
16         AssetDatabase.CreateAsset(excelData , path);
17         
18         //AssetDatabase.DeleteAsset(path);
19         AssetDatabase.Refresh();
20 
21     }
22 
23     [MenuItem("Build AssetsBundle/BuildExcel")]
24     public static void BuildAssets()
25     {
26         string path = "Assets/CharacterConfig.asset";
27         UnityEngine.Object o = AssetDatabase.LoadAssetAtPath(path, typeof(ExcelData));
28         if (BuildPipeline.BuildAssetBundle(o, null, "Assets/joi.assetbundle"))
29         {
30             
31 
32             Debug.Log("打包成功!");
33         }
34         else
35         {
36             Debug.Log("打包失败");
37         }
38 
39         AssetDatabase.Refresh();
40 
41         
42     }
43 }
 1 using UnityEngine;
 2 using System.Collections;
 3 using System;
 4 public class TestMono : MonoBehaviour {
 5   [SerializeField]
 6   TestSeriels MyData;
 7   public MyTeam TeamData;
 8   [Serializable]
 9   public class MyTeam {
10       public string Name;
11       public string HelloText;
12   }
13   IEnumerator LoadData() {
14       string DataPath =  "file://"+Application.dataPath + "/joi.assetbundle";
15       WWW dataLoad = new WWW(DataPath);
16       yield return dataLoad;
17       //byte[] decryptedBytes = dataLoad.bytes;
18       //AssetBundle assetBundle = AssetBundle.LoadFromMemory(decryptedBytes);   //从内存中同步加载资源资源包
19       //yield return assetBundle;
20       //if (assetBundle != null)
21       //{
22 
23       //}
24       UnityEngine.Object[] objs = dataLoad.assetBundle.LoadAllAssets(typeof(ExcelData));
25       foreach (UnityEngine.Object obj in objs)
26       {
27           Debug.LogError(obj.name);
28 
29       }
30       //ExcelData excelData = dataLoad.assetBundle.mainAsset as ExcelData;
31       //Debug.LogError( "excelData"+excelData.name);
32      
33       
34   }
35     // Use this for initialization
36     void Start () {
37         StartCoroutine("LoadData");
38     }
39     
40     // Update is called once per frame
41     void Update () {
42     
43     }
44 }

 

posted @ 2017-06-19 15:26  zhengjiayu  阅读(329)  评论(0编辑  收藏  举报