1、單一文件創建unity3d
using UnityEngine;
using UnityEditor;
using System.IO;
public class BuildAssetBundlesFromDirectory {
[@MenuItem("Asset/Build AssetBundles From Directory of Files")] //生成菜單
static void ExportAssetBundles () {
// Get the selected directory
//獲取選擇的目錄
string path = AssetDatabase.GetAssetPath(Selection.activeObject);
Debug.Log("Selected Folder: " + path);
if (path.Length != 0) {
path = path.Replace("Assets/", "");
string [] fileEntries = Directory.GetFiles(Application.dataPath+"/"+path);
foreach(string fileName in fileEntries) {
string filePath = fileName.Replace("\\","/");
int index = filePath.LastIndexOf("/");
filePath = filePath.Substring(index+1);
Debug.Log("filePath:"+filePath);
string localPath = "Assets/" + path+"/";
if (index > 0)
localPath += filePath;
Object t = AssetDatabase.LoadMainAssetAtPath(localPath);
if (t != null) {
Debug.Log(t.name);
string bundlePath = "Assets/" + path + "/" + t.name + ".unity3d";
Debug.Log("Building bundle at: " + bundlePath);
// Build the resource file from the active selection.
//從激活的選擇編譯資源文件
BuildPipeline.BuildAssetBundle
(t, null, bundlePath, BuildAssetBundleOptions.CompleteAssets);
}
}
}
}
}
說明:string filePath = fileName.Replace("\\","/"); 把“\”轉化成“/”。把以上代碼的腳本放到一個文件夾里面(須放在Assets\Editor目錄下面),選中該文件夾,再點擊菜單欄上的按鈕"Asset/Build AssetBundles From Directory of Files",就成功轉成unity3d格式了。
2、單一文件unity3d格式的加載
function Start () {
var www = new WWW ("file:///"+Application.dataPath+"/resourse/Cube.unity3d");//Windows
yield www;
Instantiate(www.assetBundle.mainAsset);
}
function Start ()
{
var www = WWW.LoadFromCacheOrDownload("http://210.30.12.33:8080/YangChun/file/Cube.unity3d",5); //webPlayer
yield www;
if (www.error != null)
{
Debug.Log (www.error);
return;
}
Instantiate(www.assetBundle.mainAsset);
}
3、場景轉化成unity3D格式
@MenuItem ("Build/BuildScene")
static function MyBuild(){
var levels : String[] = ["Assets/main.unity"]; //main為場景名稱
BuildPipeline.BuildStreamedSceneAssetBundle( levels, "test.unity3d", BuildTarget.WebPlayer);//BuildTarget.Andrdoid “test.unity3d”為生成的文件名稱
}
//或者
@MenuItem ("Build/BuildScene")
static function MyBuild(){
BuildPipeline.BuildPlayer(["Assets/main.unity"],"test.unity3d",BuildTarget.WebPlayer, BuildOptions.BuildAdditionalStreamedScenes);
}
說明:默認生成的文件是在工程根目錄下面,同樣此文件得放在Assets\Editor目錄下面;BuildTarget,轉化成不同的平台,這個根據實際需要設置即可。
4、unity3D場景文件加載
function Start () {
// Download compressed scene. If version 5 of the file named "test.unity3d" was previously downloaded and cached.
// Then Unity will completely skip the download and load the decompressed scene directly from disk.
//下載壓縮的場景。如果名為test.unity3d的文件版本為5,預先下載並緩存。
//然后Unity將完全跳過下載並直接從磁盤加載解壓的場景。
var download = WWW.LoadFromCacheOrDownload ("file:///"+Application.dataPath + "/test.unity3d", 5);
// var download = WWW.LoadFromCacheOrDownload ("http://myWebSite.com/test.unity3d", 5);
print(Application.dataPath + "/test.unity3d");
yield download;
// Handle error
if (download.error != null)
{
Debug.LogError(download.error);
return;
}
// In order to make the scene available from LoadLevel, we have to load the asset bundle.
// The AssetBundle class also lets you force unload all assets and file storage once it is no longer needed.
//為了使場景LoadLevel可用,必須加載資源包
//AssetBundle類,還可以強制卸載所有的資源和文件存儲,一旦不再需要。
var bundle = download.assetBundle;
// Load the level we have just downloaded
//加載剛才下載的場景
Application.LoadLevel ("main");
}

