1.導出。unity3d格式資源:
http://game.ceeger.com/Script/BuildPipeline/BuildPipeline.BuildAssetBundle.html
這裏我稍微改了一點點~~~代碼如下:
- 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);
- }
- }
- }
- }
- }
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/path/.prefab”和“path\.prefab”
把以上代碼的腳本放到一個文件夾裏面,選中該文件夾,再點擊菜單欄上的按鈕"Asset/Build AssetBundles From Directory of Files",就成功轉成unity3d格式了
2.加載.unity3d:
- function Start () {
- var www = new WWW ("file:///"+Application.dataPath+"/resourse/Cube.unity3d");
- yield www;
- Instantiate(www.assetBundle.mainAsset);
- }
function Start () {
var www = new WWW ("file:///"+Application.dataPath+"/resourse/Cube.unity3d");
yield www;
Instantiate(www.assetBundle.mainAsset);
}
注:Application.dataPath獲取改程序的資源路徑。- function Start ()
- {
- var www = WWW.LoadFromCacheOrDownload("http://210.30.12.33:8080/YangChun/file/Cube.unity3d",5);
- yield www;
- if (www.error != null)
- {
- Debug.Log (www.error);
- return;
- }
- Instantiate(www.assetBundle.mainAsset);
- }
function Start ()
{
var www = WWW.LoadFromCacheOrDownload("http://210.30.12.33:8080/YangChun/file/Cube.unity3d",5);
yield www;
if (www.error != null)
{
Debug.Log (www.error);
return;
}
Instantiate(www.assetBundle.mainAsset);
}
我試了一下用Resources類的方法還不能加載unity3d格式的文件。不過如果是本地加載的話直接加載prefab就可以了,用不着用unity3d格式了。貌似
- LoadFromCacheOrDownload方法只能加載.unity3d格式文件,我用Tomcat服務器小測了一下,可以達到緩存的效果。
LoadFromCacheOrDownload方法只能加載.unity3d格式文件,我用Tomcat服務器小測了一下,可以達到緩存的效果。
3.加載場景的話:
先把場景轉化成unity3d格式的。
注:以下代碼的腳本必須放在Editor文件夾下(如果沒有改文件,新建一個就行),BuildTarget注意哈,轉化成不同的平臺~~~BuildTarget.Andrdoid
- <pre class="java" name="code">@MenuItem ("Build/BuildWebplayerStreamed")
- static function MyBuild(){
- var levels : String[] = ["Assets/yaya.unity"];
- BuildPipeline.BuildStreamedSceneAssetBundle( levels, "yaya.unity3d", BuildTarget.WebPlayer);//BuildTarget.Andrdoid
- }
- </pre>或者
- <pre></pre>
- <pre style="background-color: rgb(255, 255, 255);" class="java" name="code"><pre class="java" name="code">@MenuItem ("Build/BuildWebplayerStreamed")
- static function MyBuild(){
- BuildPipeline.BuildPlayer(["Assets/main.unity"],"VR.unity3d",BuildTarget.WebPlayer, BuildOptions.BuildAdditionalStreamedScenes);
- }</pre><br>
- <br>
- <br>
- <pre></pre>
- <pre style="padding: 0px; color: rgb(53, 47, 40); line-height: 20px; font-family: Verdana,Geneva,sans-serif,宋體; margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word; background-color: rgb(238, 238, 238);"><pre class="java" name="code">function Start () {
- // Download compressed scene. If version 5 of the file named "Streamed-Level1.unity3d" was previously downloaded and cached.
- // Then Unity will completely skip the download and load the decompressed scene directly from disk.
- //下載壓縮的場景。如果名爲Streamed-Level1.unity3d的文件版本爲5,預先下載並緩存。
- //然後Unity將完全跳過下載並直接從磁盤加載解壓的場景。
- var download = WWW.LoadFromCacheOrDownload ("http://210.30.12.16:8080/chunge/yaya.unity3d", 5);
- 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 ("yaya");//這裏面的“yaya”是指“Assets/yaya.unity”而不是指“yaya.unity3d”
- }</pre><br><br></pre>
- <br>
- <br>
- <br>
- <p></p>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- </pre>