将游戏资源制作为 .unity3d 文件使用 WWW 加载或者使用 File.ReadAllBytes加载
一、 选择资源文件或者文件夹右键,
弹出如下窗口,选择最后一项 “Build AssetBundles、、、、”
弹出如下对话框,选择要保存的路径和要保存的名字
保存后在所选路径下即生成相应的文件
然后通过代码将导出的资源加载到场景中,实例化出对象即可
代码如下
在 Editor 文件夹下创建脚本 “TestWWW”
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
public class TestWWW{
// 将资源导出为 .unity3d 的文件
// 在Assets 下创建菜单, 在Project下,选择一个文件或文件夹,右键弹出框下出现创建的菜单
[MenuItem("Assets/Build AssetBundles From Directory of Files")]
static void ExportAssetBundles()
{
// 弹出一个对话框,选择需要导出文件的路径
string path = EditorUtility.SaveFilePanel("Save Resource", "", "", "unity3d");
Debug.Log("path " + path);
if (path.Length != 0) // 如果路径不为空则保存
{
Object[] selection = Selection.GetFiltered(typeof(Object),
SelectionMode.DeepAssets | SelectionMode.Unfiltered); // 获取选择的所有资源
// 保存为 .unity3d 类型的文件 ,参数1 为 保存对象,
//参数2 为需要保存的对象数组, 参数3为保存路径, 参数4 为保存时包含的类型
BuildPipeline.BuildAssetBundle(Selection.activeObject,
selection, path, BuildAssetBundleOptions.CollectDependencies
| BuildAssetBundleOptions.CompleteAssets);
}
}
}
加载代码如下
创建脚本 “Loadd”将其挂在摄像机等场景中的对象即可
using UnityEngine;
using System.Collections;
using System;
using System.IO;
public class Loadd : MonoBehaviour
{
private AssetBundle assetBundle;
private AssetBundleCreateRequest request;
// Use this for initialization
void Start()
{
StartCoroutine(LoadAsset());
//StartCoroutine( LoadByte());
}
// 方法一 和 方法二 功能相同,但是不要同时调用 方法一,和方法二,
//这样会出现冲突, Unity会自动检查是否已经加载了同一个 .unity3d 文件
// 方法 一
IEnumerator LoadAsset()
{ //如果有出现类型 Destroy AssetBundled 问题的,将Unity关闭,
//然后重启, 将平台改为 其他的,不要设置成 Web Player
// 利用 WWW 通过路径加载 文件
WWW www = new WWW("file:///C:/Users/Administrator/Desktop/111111.unity3d");
yield return www;
if (www.error != null)
{
Debug.Log(www.error);
}
if (www.isDone)
{
assetBundle = www.assetBundle;
string[] names = { "Dynamo", "Itamae" };
for (int i = 0; i < names.Length; i++)
{
// 通过名字获取下载资源文件中的对象模型
if (assetBundle.Contains(names[i])) //判断是否包含改名字的对象
{
UnityEngine.Object obj = assetBundle.Load(names[i]);
if (obj != null)
{
// 实例化对象, 实例化出的对象如果有贴图会自动添加贴图
Instantiate(obj);
}
}
}
}
}
// 方法 二
IEnumerator LoadByte()
{
// 如果出现 说 还有未定义的 ReadAllBytes, 查看平台不要设置为 WebPlayer
// 读取字节的方式通过路径加载, 该路径为PC路径
byte[] bs = File.ReadAllBytes("C:/Users/Administrator/Desktop/111111.unity3d");
request = AssetBundle.CreateFromMemory(bs);
yield return new WaitForSeconds( 1.0f);
assetBundle = request.assetBundle;
string[] names = { "Dynamo", "Itamae" };
for (int i = 0; i < names.Length; i++)
{
if (assetBundle.Contains(names[i])) // 判断是否包含 该名字的对象
{
// 通过名字获取下载资源文件中的对象模型
UnityEngine.Object obj = assetBundle.Load(names[i]);
if (obj != null)
{
// 实例化对象, 实例化出的对象如果有贴图会自动添加贴图
Instantiate(obj);
}
}
}
}
}
最核心的方法其实就它:
BuildPipeline.BuildAssetBundle (obj, null, targetPath, BuildAssetBundleOptions.CollectDependencies)
参数1:它只能放一个对象,因为我们这里是分别打包,所以通过循环将每个对象分别放在了这里。
参数2:可以放入一个数组对象。
默认情况下打的包只能在电脑上用,如果要在手机上用就要添加一个参数。
Android上:
BuildPipeline.BuildAssetBundle (obj, null, targetPath, BuildAssetBundleOptions.CollectDependencies,BuildTarget.Android)
IOS上:
BuildPipeline.BuildAssetBundle (obj, null, targetPath, BuildAssetBundleOptions.CollectDependencies,BuildTarget.iPhone)
另外,电脑上和手机上打出来的Assetbundle不能混用,不同平台只能用自己的。