1. Single File Creation Unity3d
Using Unityengine; Using Unityeditor; Using System.IO; public class Buildassetbundlesfromdirectory {[@MenuItem ("Asset/build assetbundles from Directory of Files")]//Build menu static void Exportassetbundles () {//Get the selected directory//Get selected directory str ing path = Assetdatabase.getassetpath (Selection.activeobject); Debug.Log ("Selected Folder:" + path); if (path. Length! = 0) {Path = path. Replace ("assets/", "" "); string [] fileentries = Directory.GetFiles (application.datapath+ "/" +path); foreach (String fileName in fileentries) {string filePath = Filename.replace ("\ \", "/"); int index = Filepath.lastindexof ("/"); FilePath = filepath.substring (index+1); Debug.Log ("FilePath:" +filepath); String LocalPath = "assets/" + path+ "/"; if (Index > 0) LoCalpath + = FilePath; Object t = Assetdatabase.loadmainassetatpath (LocalPath); if (t! = null) {Debug.Log (t.name); String Bundlepath = "assets/" + path + "/" + T.name + ". Unity3d"; Debug.Log ("Building bundle at:" + bundlepath); Build the resource file from the active selection. Compile the resource file from the active selection buildpipeline.buildassetbundle (t, NULL, Bundlepath, Buildas Setbundleoptions.completeassets); } } } } }
Description: String filePath = Filename.replace ("\ \", "/"); Turn "\" into "/". Put the above code script in a folder (should be placed under the Assets\editor directory), select the folder, and then click the button on the menu bar "Asset/build assetbundles from Directory of Files", Successfully turned into Unity3d format.
2, single file Unity3d format loading
function Start () { var www = new www ("file:///" +application.datapath+ "/resourse/cube.unity3d");//windows Yield www; Instantiate (Www.assetBundle.mainAsset); }
function Start () { var www = www. Loadfromcacheordownload ("Http://210.30.12.33:8080/YangChun/file/Cube.unity3d", 5); Webplayer yield www. if (www.error! = null) { Debug.Log (www.error); return; } Instantiate (Www.assetBundle.mainAsset); }
3. Conversion of scenes into Unity3d format
@MenuItem ("Build/buildscene") static function Mybuild () { var levels:string[] = ["assets/main.unity"]; Main is the scene name Buildpipeline.buildstreamedsceneassetbundle (Levels, "Test.unity3d", buildtarget.webplayer);// Buildtarget.andrdoid "Test.unity3d" is the generated file name } //or @MenuItem ("Build/buildscene") static function Mybuild () { buildpipeline.buildplayer (["Assets/main.unity"], "Test.unity3d", Buildtarget.webplayer, buildoptions.buildadditionalstreamedscenes); }
Note: The default generated file is under the project root directory, the same file should be placed under the Assets\editor directory, Buildtarget, converted to different platforms, this according to the actual need to set.
4. Unity3d Scene File loading
function Start () {//Download compressed scene. If version 5 of the file named "Test.unity3d" was previously downloaded and cached. Then Unity would completely skip the download and load the decompressed scene directly from disk. Download the compressed scene. If the file named Test.unity3d is version 5, it is pre-downloaded and cached. Then unity will skip the download completely and load the unzipped scene directly from the disk. var download = WWW. Loadfromcacheordownload ("file:///" +application.datapath + "/test.unity3d", 5);//var download = WWW. Loadfromcacheordownload ("Http://myWebSite.com/test.unity3d", 5);p rint (Application.datapath + "/test.unity3d"); Yield download; Handle error if (download.error! = null) {debug.logerror (download.error); Return }//In order-to-make the scene available from Loadlevel, we had to load the asset bundle. The Assetbundle class also lets you force unload all assets and file storage once it is no longer needed. To make the scene loadlevel available, the resource pack//assetbundle class must be loaded, and all resources and file storage can be forced to unload once it is no longer needed. var bundle = Download.assetBundle; Load the level we have just downloaded//load the scene just downloaded Application.loadlevel ("main");}
Export and load of. Unity3d format