AssetBundle.CreateFromFile的有趣事情

时间:2021-08-14 08:29:44

有趣的事情发生了:

[@MenuItem("AssetBundles/Build AssetBundles")]

staticvoid BuildABs ()

{

AssetBundleBuild[] buildMap =
new AssetBundleBuild[];

buildMap [].assetBundleName ="test.assetbundle";//打包的资源包名称

string[] resourcesAssets =new
];//此资源包以下有多少文件

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 [].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打包。