|
资源信息 Tutorial Information
教程名称: | Unity AssetBundle的使用两种方法(发帖教程) |
适用引擎: | Unity3D (适用引擎,为空默认为Unity) |
教程语种: | 中文 |
教程等级: | 1 |
教程格式: | 图文(请用IE9以上浏览器访问本版块) |
教程作者: | 转载自互联网 (如有问题请短消息联系作者或发表回复) |
下载地址: | 无 (兑换积分) |
1、所有打包参数在代码里设置,完全代码控制;
第一种:
在代码中指定bundle包名,指定包含在该bundle里的所有资源路径
新建TestBundle.cs注意此脚本放在Editor文件夹下,
using UnityEngine;
using UnityEditor;
using System.IO;
[InitializeOnLoad]
public static class TestBundle
{
[MenuItem("AssetsBundle/Build Bundle -- 代码中设置参数")]
static void BuildAB()
{
string path = Application.streamingAssetsPath + "/AssetsBundles/";
if (Directory.Exists(path))
Directory.Delete(path, true);
Directory.CreateDirectory(path);
AssetBundleBuild[] buildMap = new AssetBundleBuild[2];
//指定bundle包名
buildMap[0].assetBundleName = "prefab_bundle";
string[] assets = new string[2];
assets[0] = "Assets/AssetsBundleObj/Cube.prefab"; // 必须是完整的文件名(包括后缀)
assets[1] = "Assets/AssetsBundleObj/Sphere.prefab";
//指定bundle包含的资源路径数组
buildMap[0].assetNames = assets;
buildMap[1].assetBundleName = "scene_bundle";
string[] scenes = new string[1];
scenes[0] = "Assets/_Scenes/Scene.unity";
buildMap[1].assetNames = scenes;
BuildPipeline.BuildAssetBundles(path, buildMap, BuildAssetBundleOptions.DeterministicAssetBundle, BuildTarget.StandaloneWindows);
}
}
代码中设置参数
2、在编辑器中设置打包参数,代码简便
新建一个prefab,如下配置,一个名字,一个后缀
新建TestBundle.cs注意此脚本放在Editor文件夹下,代码如下:
using UnityEngine;
using UnityEditor;
using System.IO;
[InitializeOnLoad]
public static class TestBundle
{
[MenuItem("AssetsBundle/Build Bundle -- 编辑器中设置参数")]
static void BuildAB2()
{
string path = Application.streamingAssetsPath + "/AssetsBundles/";
if (Directory.Exists(path))
Directory.Delete(path, true);
Directory.CreateDirectory(path);
BuildPipeline.BuildAssetBundles(path, BuildAssetBundleOptions.DeterministicAssetBundle, BuildTarget.StandaloneOSX);
}
} 再新建一个脚本AssetBundleTest.cs 代码如下, using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AssetBundleTest : MonoBehaviour
{
public GameObject obj;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnGUI()
{
if (GUILayout.Button("加载预制体Cube"))
{
StartCoroutine(LoadObj("cube_ab", "cube.prefab"));//有没有.prefab后缀都正常加载
}
if (GUILayout.Button("加载预制体Sphere"))
{
StartCoroutine(LoadObj("prefab_bundle", "sphere"));
}
}
/// <summary>
/// 加载预制体
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
IEnumerator LoadObj(string bundle_name, string name)
{
string path = "file://" + Application.streamingAssetsPath + "/AssetsBundles/" + bundle_name;
//Debug.LogError(string.Format("obj {0}", path));
WWW www = new WWW(path);
yield return www;
if (www.error == null)
{
AssetBundle bundle = www.assetBundle;
//这里LoadAsset第二个参数 有没有都能正常运行,这个类型到底什么用途还有待研究
obj = Instantiate(bundle.LoadAsset(name, typeof(GameObject))) as GameObject;
obj.transform.parent = transform;
// 上一次加载完之后,下一次加载之前,必须卸载AssetBundle,不然再次加载报错:
// The AssetBundle 'Memory' can't be loaded because another AssetBundle with the same files is already loaded
bundle.Unload(false);
}else
{
Debug.LogError(www.error);
}
}
}
工具栏中会出现
点击生成bundle目录,会看到AssetBundle文件夹在StreamingAssets目录中,
将AssetBundleTest.js绑定到场景中运行,点击 “加载预制体Cube” cube就会出现在场景中了。
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
×
|