Unity3D: dynamically load resources (Resources.Load and AssetBundle.LoadAsset)

Unity3D a dynamic loading (Resources)


  1. Unity3D create a folder in the Project window: Resources.
  2. Will need to dynamically load the file into which, for example, Texture, Sprite, prefab and so on.
  3. Related interfaces can call API interface Resources.Load () in the script.

E.g:

using UnityEngine;
using UnityEngine.UI;
public class LoadTest1:MonoBehaviour
{
	private void Start()
	{
		Sprite sp=Resources.Load<Sprite>("Pic");  //"Pic"是资源的名字
		GameObject obj=Resources.Load<GameObject>("Prefab01"); 
		Instantiate(obj);
	}
}

In this way can only access resources Resources folder.


Unity3D dynamically loaded two


The second way is to use AssetBundle (AssetBundle is a collection of resources, Unity3D can be supported by any resource format).

Create a file AssetBundle

  • Select the package AssetBundle resources from the Project view.
  • Inspector view at the bottom of the dispensing portion can be seen and variants AssetBundle
  • Select the left drop-down selection distribution AssetBundle. Which displays "None", click on "New ..." to create a new AssetBundle, and enter the appropriate name. To add a sub-folder, you can "/" to separate the name used.
  • AssetBundle build script
  • Editor to create a folder in the Assets (Project windows) folder, create a script CreateAssetBundles.cs
using UnityEditor;

public class CreateAssetBundles
{
  [MenuItem("AB/Build AssetBundles")]
  static void BuildAllAssetBundles()
  {
  	//要创建的目录
  	string assetBundleDirectory = "Assets/AssetBundles";
  	if(!Directory.Exists(assetBundleDirectory))
  	{
  		Directory.CreateDirectory(assetBundleDirectory);
  	}
  	//三个参数:第一个是创建的目录位置,第二个是AssetBundle的压缩方式,第三个是创建的平台。
  	BuildPipeline.BuildAssetBundles(assetBundleDirectory, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);
  }
}
  • After saving a good script, return to Unity3D, the toolbar will appear above the "AB" tab, select click "Build AssetBundles" to complete the construction of AssetBundle.

After building the output directory will be more of the same with a directory and file name AssetBundle his manifest file; then the rest you assign a AssetBundle each corresponding to a file with the same name and the manifest file.

Load AssetBundle file

  • Load AssetBundle resources (LoadFromFile) from local
using UnityEngine;
public class LoadTest02:MonoBehaviour
{
	private void Start()
	{
		string path=Application.dataPath+"/AssetBundles/"+"roles";  //最后一个字符串是AssetBundle的名字
		AssetBundle assetBundle=AssetBundle.LoadFromFile(path);
		if(assetBundle==null)
		{
			return;
		}
		GameObject obj=assetBundle.LoadAsset<GameObject>("Role01"); //字符串是AssetBundle中资源名称
		Instantiate(obj);
	}
}
  • Load AssetBundle (LoadFromMemory) from local
 using UnityEngine;
 using System.IO;
public class LoadTest02:MonoBehaviour
{
	private void Start()
	{
		string path=Application.dataPath+"/AssetBundles/"+"roles";  //最后一个字符串是AssetBundle的名字
		AssetBundle assetBundle=AssetBundle.LoadFromMemory(File.ReadAllBytes(path));
		if(assetBundle==null)
		{
			return;
		}
		GameObject obj=assetBundle.LoadAsset<GameObject>("Role01"); //字符串是AssetBundle中资源名称
		Instantiate(obj);
	}
}

Compared with this LoadFromFile just replaced File.ReadAllBytes path (path), i.e. LoadFromMemory parameter is accepted as a byte array, may be replaced by any AssetBundle File.ReadAllBytes byte array ().

  • Remote or local load AssetBundle (UnityWebRequest)
using UnityEngine;
using UnityEngine.Networking;
using System.IO;
public class LoadTest03:MonoBehaviour
{
	private void Start()
	{
		StartCoroutine(LoadFromRemote());
	}
  	private IEnumerator LoadFromRemote()
  	{
  		string path="D:/Documents/ABRep/roles";
  		string uri="file:///"+path;  //可将uri换成任何相关的AssetBundle服务器
  		UnityWebRequest reques=UnityWebRequestAssetBundle.GetAssetBundle(uri,0);
  		yield return request.SendWebRequest();
  		AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(request);
  		GameObject obj= bundle.LoadAsset<GameObject>("Role02");
  		Instantiate(obj);
  	}
}

These are used through the present method, as well as the corresponding asynchronous method LoadFromFileAsync, LoadFromMemoryAsync etc.

Extended: About version update is to use AssetBundle. By Version.txt custom version of the file relating to, every time you open the game begins with the remote server to pull the latest Version.txt, after comparison, to download the expansion pack (AssetBundle resources). After downloading and then talk about related issues.

Published 10 original articles · won praise 11 · views 1798

Guess you like

Origin blog.csdn.net/qq_34406755/article/details/104071017