1. Create a resource package:
Public class sript_01: monobehaviour
{
[Menuitem ("Custom Resource Package/create resource package")]
Static void execcreateassetbunldes ()
{
// Set the root path for saving the resource package
String targetdir = application. datapath + "/assetbundles ";
Object [] selectedasset = selection. getfiltered (typeof (object), selectionmode. deepassets );
// Create a directory to save the resource package
If (! Eirectory. exists (targetdir ))
{
Directory. createdirectory (targetdir );
}
For (INT I = 0; I <selectedasset. length; I ++)
{
// Generate the full path of the Resource Package
String filepath = targetdir + "/" + selectedasset [I]. Name + ". unity3d ";
// Determine whether the resource package file exists based on the path
If (file. exists (filepath ))
{
File. Delete (filepath );
}
// Generate a new resource package file
If (buildpipeline. buildassetbundle (selectedasset [I], null, filepath, buildassetbundleoptions. collectdependencies ))
{
Debug. Log ("Resource Package generated successfully ");
Assetdatabase. Refresh ();
}
Else
Debug. Log ("resource package file generation failed ");
}
}
}
}
Note:This is an editor class. To use it you have to place your script in
Assets/EditorInside your project folder. Editor classes are in the unityeditor namespace so for C # scripts you need to add"Using unityeditor;"At the beginning of the script.
2. Download The resource package
Ienumerator loadbundlemat (string name)
{
Material MAT;
// Path of the resource on the local machine
WWW date = new WWW ("file: //" + application. datapath + "/assetbundles/" + name + "./unity3d ");
// Wait until the download is complete
Yield return date;
// Forcibly convert the downloaded resource into a material resource
MAT = (material) date. assetbundle. mainasset;
// Replace with the downloaded Resource
Renderer. Material = MAT;
// Release the resource object
Date. assetbundle. Unload (false );
}
Ienumerator loadbundleobject (string name)
{
WWW date = new WWW ("file: //" + application. datapath + "assetbundles/" + name + ". unity3d ");
// Wait until the download is complete and the game object is cloned,
Yield return instantiate (date. assetbundle. mainasset );
// Release the resource object
Date. assetbundle. Unload (false );
}
Note: After the resource package is downloaded, the resource file will remain in the assetbundle. mainasset object, and then use this object. An error occurs when downloading resource package objects consecutively. Therefore, you must manually release the object after downloading the resource.