第一步:
新建unity工程,在场景中放一张图片
并创建图片的预制体,放在prefabs文件下
image.png
第二步:
指定AssetBundles标签
选中预制体prefabs>Alita
在Inspector面板最下面设置,如果没有就new一个,后缀名随意(注意:AssetBundles名不区分大小写)
第三步:
创建Editor目录,并在该目录下编辑脚本CreatAssetBundles.cs
using UnityEditor;
using System.IO;
public class CreatAssetBundles
{
[MenuItem("Assets/Build AssetBundles")]
static void BuildAllAssetBundles()
{
string dir = UnityEngine.Application.streamingAssetsPath;
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
BuildPipeline.BuildAssetBundles("AssetBundles",BuildAssetBundleOptions.None,BuildTarget.StandaloneWindows64);
}
}
第四步:
在点击Assets>Build AssetBundles运行,
在窗口闪过运行小窗口后就可以在目录下看到打包结果了
第五步加载
本地加载为例
创建脚本LoadFromFileExample.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LoadFromFileExample : MonoBehaviour
{
public GameObject canvas;
// Start is called before the first frame update
void Start()
{
AssetBundle ab = AssetBundle.LoadFromFile("AssetBundles/alita.unity3d");
//Alita为预制体的名字
GameObject alitaPrefab = ab.LoadAsset<GameObject>("Alita");
Instantiate(alitaPrefab, canvas.transform);
}
}
网友评论