There are several ways to load Unity for resources in the game, among which Asset Bundle is very important for dynamic and online updates.
This blogger explains the steps of packaging, publishing, transmitting, parsing and loading resources through AssetBundle.
1. pack
There are two ways to package the resource files we want to package out:
- Code packing
For Unity packaged resources prior to version 5.0, they were implemented through code
For example:
public class PutBundles : MonoBehaviour
{
[MenuItem("Put/MyBundles")] //Give options in the menu
static void PutBundleAssetes()
{
//Create the array of bundle build details.
List<AssetBundleBuild> buildMap = new List<AssetBundleBuild>();
AssetBundleBuild build = new AssetBundleBuild();
build.assetBundleName = "picture.unity3d";
build.assetNames = new[] { "Assets/huajianghu.jpg" };
buildMap.Add(build);
BuildPipeline.BuildAssetBundles("Assets/ABs", buildMap.ToArray(), BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);
}
}
We need to prepare a C# script for packing actions and add our own methods, which are mainly through
AssetBundleBuild objects are packaged, which is relatively complex, requiring us to manually add file paths and packages. But to make a point, we need to create a new editor folder under the Assets folder and put our scripts under the folder, otherwise there will be errors.
-
Editor Packing
Select the object we want to package under the Assets folder:
Click to appear:
Here we can customize the packaged file name and file type
Then we can package our resources with the same script:
BuildPipeline.BuildAssetBundles("Assets/MyBundles", BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
It only takes a few lines of code to get it done. Compared with the old version of Unity, the new version gives us a higher level than the old one, which eliminates a series of complicated operations in selecting the address. Then we can package the resources in the menu.
When the package is finished, it will appear:
(http://img.blog.csdn.net/20170317124554667? Watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFMjQ2NjE2OTU=/font/5a6L5L2T/fontsize/400/fill/I0JBQFCMA=/disso/70/gravity/East)
2. release
We can publish it to our game server to provide remote clients for download.
Here we just put it locally for parsing and loading.
3. Transfer Loading Analysis and Loading
Generally, we use the protocol to download and transfer the resource files.
IEnumerator Load()
{
//Download and load from a remote server
WWW www = new WWW("ftp://123.778.**"perhaps"http://www.baidu.com");
//Loading from local files
WWW www = new WWW("file://E:/Projects/Games/Resources/Assets/MyBundles/cube.unity3d");
yield return www;
AssetBundle bundle = www.assetBundle;
AssetBundleRequest request = bundle.LoadAssetAsync("Assets/Cube.prefab", typeof(GameObject));
go = Instantiate(request.asset as GameObject, new Vector3(0f, 0f, 0f), Quaternion.identity) as GameObject;
yield return request;
www.Dispose();
hadLoad = true;
}
Result picture: