[Unity note] understanding of AssetBundle API

Posted by Fataqui on Tue, 08 Feb 2022 19:21:32 +0100

Development platform: Unity
Programming platform: Visual Studio 2017 or above
Programming language: C#
Reference book: intensive introduction to the official case of UNITY

Returns the storage path relative to the project directory

AssetDatabase.GetAssetPath(Object assetObject)
  • For example, Assets/_Scenes/Main.unity.

Returns the first object of the specified type under the path

AssetDatabase.LoadAssetAtPath(string assetPath, Type type)
  • assetPath is the path relative to the project directory, and the path is case insensitive
  • All platforms under the assetPath path need to use forward slash ("/"), and reverse slash ("\") is not allowed

Create a new object based on the Asset object under the specified path

AssetDatabase.CreateAsset(Object asset, string path)
  • path must contain the Asset file extension supported by Unity
    For example, materials mat, animation clip anim, any other file class asset
  • After creation, you can use assetdatabase Addobjecttoasset() adds a new object to the Asset
  • This function cannot directly create an Asset from GameObject. It should be implemented by using the function provided by the PrefabUtility class

Create AssetBundle under the specified path

BuildPipeline.BuildAssetBundle(Object mainAsset, Object[] assets, string pathName, BuildAssetBundleOptions assetBundleOptions=BuildAssetBundleOptions.CollectDependencics|BuildAssetBundleOptions.CompleteAssets, BuildTargettargetPlatform=BuildTarget.WebPlayer)
  • This function is used to create asset bundles. The key parameters are the first three
  • mainAsset: the main object stored in the Asset
  • assets: the list of objects to be stored in the Asset, which can be accessed through key values after completion
  • pathName: the path of the stored file

Note: the asset bundle created for Standalone and WebPlayer platforms can no longer be used on mobile platforms.

Delete Asset under the specified path

AssetDatabase.DeleteAsset(string path)
  • If the deletion is successful, return True
  • If the deletion fails or the Asset file under the path does not exist and cannot be deleted, False is returned

Delete Asset immediately under the specified path

Object.DestroyImmediate(Object obj, bool allowDestroyAssets=false)

Creates an empty preform object under the specified path

PrefabUtility.CreateEmptyPrefab(string path)
  1. Note: if there is a precast body under this path, the new precast body will cover the old precast body (that is, delete the existing precast body and create a new precast body)

Replace the target object targetPrefab with the game object gameObject

PrefabUtility.ReplacePrefab(GameObject gameObject, Obejct targetPrefab, ReplacePrefabOptions options=ReplacePrefabOptions.Default)

Instantiate a new preform based on the preform target

PrefabUtility.InstantiatePrefab(Object target)
  1. Note: unlike instance(), the preform instantiated by this function is associated with the source preform.

Obtain the AssetBundle file of the specified version at the specified URL

WWW.LoadFromCacheOrDownLoad(string url, int version, uint crc=0)
  1. The return value is WWW instance
  2. If the downloaded AssetBundle file of the specified version does not exist in the cache, the specified version will be downloaded from the specified URL and stored in the cache for reuse in subsequent operations rather than downloading again.

Access downloaded AssetBundle content



function Start()
	var www = new WWW("http://myurl//myBundle.unity3d");
	yield www;

Inherit ScriptableObejct class

technological process

  1. Create subclasses of ScriptableObject and add data members
  2. Use scriptableobject Createinstance create instance
  3. To create an AssetBundle using an instance:
    AssetDatabase.CreateAsset -> AssetDatabase.LoadAssetPath -> BuildPipeline.BuildAssetBundle -> AssetDatabase.DeleteAsset
  4. Use WWW.assetBundle to access the created assetBundle


  • Create a new script mydata under the root directory of Assets cs
using UnityEngine;
using System.Collections.Generic;

/// <summary>
///Data member
/// </summary>
public class MyData : ScriptableObject
	public List<Vector3> content;
  • Create a new script MyExporter under the root directory of Assets CS, the script will create a new submenu item MyExporter under the Assetsc menu. After clicking the submenu item, the instance MyData of MyData will be everywhere in the Assets directory assetbundle
using UnityEngine;
using UnityEditor;
using System.Collection.Generic;

public class MyExporter : MonoBehavior
	static void MyExec()
		MyData md = ScriptableObject.CreateInstance<MyData>();  //Create instance
		md.content = new List<Vector3>();
		md.content.Add(new Vector3(0, 1, 2));
		md.content.Add(new Vector3(2, 3, 4));
		md.content.Add(new Vector3(3, 4, 5));
		string a = "Assets/MyData.asset";
		AssetDatabase.CreateAsset(md, a); //Create new object a
		//Return the first preform of type MyData under the path
		Object o = AssetDatabase.LoadAssetAtPath(a, typeof(MyData));
		string b = "Assets/MyData.assetbundle";
		BuildPipeline.BuildAssetBundle(o, null, b);
  • Create a new script client under the root directory of Assets CS, the script is downloaded through WWW.LoadFromCacheOrDownload()
using UnityEngine;
using System.Collections;

public class Client : MonoBehaviour
	IEnumerator Start()
		string a = "file://" + Application.dataPath + "/MyData.assetbundle";
		WWW www = WWW.LoadFromCacheOrDownload(a, 1);
		yield return www;
		//Judge whether the obtained content is empty
			return false;
		MyData md = www.assetBundle.mainAsset as MyData;
		if(md != null)
  • The output result is (0.0, 1.0, 2.0)

Topics: Unity