1, packaging needs to use the path of Android packaging
Using Unityengine;
Using System.Collections;
Using Unityeditor;
public class Test:editor
{
[MenuItem ("Custom editor/create assetbunldes Main")]
static void Createassetbunldesmain ()
{
Get all the game objects selected in Project view
object[] Selectedasset = selection.getfiltered (typeof (Object), selectionmode.deepassets);
Traverse all the game objects
foreach (Object obj in Selectedasset)
{
Local test: It is recommended to put Assetbundle in the Streamingassets folder, if not, create one, because the mobile platform can only read this path
Streamingassets is a read-only path and cannot be written to
Server Download: There is no need to put it here, the server on the client with the WWW class to download.
String TargetPath = Application.datapath + "/streamingassets/" + Obj.name + ". Assetbundle";
Buildpipeline.buildassetbundle (obj, null, TargetPath, buildassetbundleoptions.collectdependencies
if (buildpipeline.buildassetbundle (obj, null, TargetPath, Buildassetbundleoptions.collectdependencies, buildtarget.android))
{
Debug.Log (Obj.name + "Resource Package success");
}
Else
{
Debug.Log (Obj.name + "resource packaging failed");
}
}
Refresh Editor
Assetdatabase.refresh ();
}
[MenuItem ("Custom editor/create assetbunldes All")]
static void Createassetbunldesall ()
{
Caching.cleancache ();
String Path = Application.datapath + "/streamingassets/all.assetbundle";
object[] Selectedasset = selection.getfiltered (typeof (Object), selectionmode.deepassets);
foreach (Object obj in Selectedasset)
{
Debug.Log ("Create assetbunldes name:" + obj);
}
Note the second parameter here.
if (Buildpipeline.buildassetbundle (null, Selectedasset, Path, buildassetbundleoptions.collectdependencies))
{
Assetdatabase.refresh ();
}
Else
{
}
}
[MenuItem ("Custom editor/create Scene")]
static void Createsceneall ()
{
Clear the cache.
Caching.cleancache ();
String Path = Application.datapath + "/myscene.unity3d";
String[] levels = {"Assets/level.unity"};
Packaging scenarios
Buildpipeline.buildplayer (Levels, Path, Buildtarget.webplayer, buildoptions.buildadditionalstreamedscenes);
Assetdatabase.refresh ();
}
}
2, loading also need to use the Android path to load
Using Unityengine;
Using System.Collections;
Using System.IO;
public class Runscript:monobehaviour
{
String downpath = "";
The path of streamingassets under different platforms is different, here need to pay attention to.
public static readonly String pathurl =
#if unity_android
"jar:file://" + Application.datapath + "!/assets/";
#elif Unity_iphone
Application.datapath + "/raw/";
#elif Unity_standalone_win | | Unity_editor
"file://" + Application.datapath + "/streamingassets/";
#else
String. Empty;
#endif
void Ongui ()
{
if (GUI. button (new Rect (+, +, +), "Main Assetbundle"))
{
Startcoroutine (Loadmaingameobject ("Http://10.131.229.166/MyItems/Cube.assetbundle"));
Startcoroutine (Loadmaingameobject ("jar:file://" + Application.datapath + "!/assets/" + "Cube.assetbundle"));
}
}
Read a resource
Private IEnumerator Loadmaingameobject (string path)
{
www bundle = new www (path);
Yield return bundle;
Loading into the game
Yield return instantiate (Bundle.assetBundle.mainAsset);
Debug.Log ("+++++++++++++++++++++++++++load successful!!");
Bundle.assetBundle.Unload (FALSE);
}
Download resources from the server
Private IEnumerator downloadtolocal (string url)
{
WWW.EscapeURL (URL); URL encoding
www www = new www (URL);//Access URL
WWW.UnEscapeURL (URL); URL decoding
string filename = URL. Substring (URL. LastIndexOf ('/') + 1); Gets the name of the file based on the URL.
yield return www; Wait for download
if (Www.error = = null)
{
FileStream fs = file.create (Application.persistentdatapath + "/" + filename); Path is where you want to save the file.
Downpath = Application.persistentdatapath + "/" + filename; Write the downloaded data to a file
Fs. Write (www.bytes, 0, www.bytes.Length);
Fs. Close ();
Debug.Log (filename + "Download complete");
}
Else
{
Debug.Log (Www.error);
}
}
}
The name of the folder should not be mistaken.
Two points to note about Android loading Assetbundle in Unity