AssetBundle.CreateFromFile的有趣事情,assetbundle
有趣的事情發生了:
[@MenuItem("AssetBundles/Build AssetBundles")]
staticvoid BuildABs ()
{
AssetBundleBuild[] buildMap = new AssetBundleBuild[1];
buildMap [0].assetBundleName ="test.assetbundle";//打包的資源套件名稱
string[] resourcesAssets =newstring[7];//此資源套件下面有多少檔案
resourcesAssets [0] ="Assets/BuildABRes/test1.csv";
resourcesAssets [1] ="Assets/BuildABRes/test1.txt";
resourcesAssets [2] ="Assets/BuildABRes/test2.txt";
resourcesAssets [3] ="Assets/BuildABRes/test3.txt";
resourcesAssets [4] ="Assets/BuildABRes/test4.txt";
resourcesAssets [5] ="Assets/BuildABRes/test4/test4.txt";
resourcesAssets [6] ="Assets/BuildABRes/test1.xml";
buildMap [0].assetNames = resourcesAssets;
BuildPipeline.BuildAssetBundles (Application.persistentDataPath +"/ABs", buildMap,BuildAssetBundleOptions.UncompressedAssetBundle,EditorUserBuildSettings.activeBuildTarget);
}
對以上檔案進行打包後,打出的ab解出時會先對resourcesAssets進行排序,
string path =Application.persistentDataPath +"/ABs/test.assetbundle";
//不能使用file://
Debug.Log (path);
AssetBundle ab =AssetBundle.CreateFromFile (path);
foreach (var ain ab.GetAllAssetNames()) {
Debug.Log (a);
}
TextAsset txt = ab.LoadAsset<TextAsset> ("test1.xml");
Debug.Log (txt.text);
LoadAsset(name) name可以使用三種方式, 1,fullname; 2, 帶尾碼的簡稱; 3,不帶尾碼的簡稱
那麼上面的方法可以使用
1、ab.LoadAsset<TextAsset> (“Assets/BuildABRes/test1.xml”);=>輸出test1.xml的值
2、ab.LoadAsset<TextAsset> (“test1.xml"); =>輸出test1.xml的值
3、ab.LoadAsset<TextAsset> (“test1”); =>輸出test1.csv檔案的值
3之所以輸出test.csv的值 是排序後的結果。以最先匹配到的檔案為準
unity 編報認三種通用文字檔格式:txt,xml,csv
另外還認一種二進位檔案格式:.bytes [lua,使用這種方式 + + 加密]
除此之外的文字檔無法被unity打包。
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。