Unity中AssetBundle打包
程序员文章站
2022-04-03 15:33:23
...
第一步:设置好AssetBundle
第二步:代码实现,打包本地,需要编译器扩展
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEditor;
public class AssetBuildr{
[MenuItem("Assets/MyAssetBundle")]
static void TextAssetBuildr()
{
//保存AssetBundle资源
string str = "AssetBundle";
if(Directory.Exists(str)==false)
{
//创建文件
Directory.CreateDirectory(str);
}
BuildPipeline.BuildAssetBundles("AssetBundle",BuildAssetBundleOptions.None,BuildTarget.StandaloneWindows64);
}
}
第三步:本地使用
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LoadAsset : MonoBehaviour {
// Use this for initialization
void Start () {
LoadResoure("AssetBundle/scene/cube.unity3d", "Cube",1);
LoadResoure("AssetBundle/scene/sphere.unity3d", "Sphere",2);
}
void LoadResoure(string path,string name,int num)
{
//加载AssetBundle
AssetBundle ab = AssetBundle.LoadFromFile(path);
//强转GameObject
GameObject ob = ab.LoadAsset<GameObject>(name);
if(num==1)
ob.transform.position = Vector3.zero;
else
ob.transform.position = Vector3.right;
Instantiate(ob);
}
}