unity3d格式的導出與加載

1.導出。unity3d格式資源:

     http://game.ceeger.com/Script/BuildPipeline/BuildPipeline.BuildAssetBundle.html

    這裏我稍微改了一點點~~~代碼如下:

  1. using UnityEngine; 
  2. using UnityEditor; 
  3. using System.IO; 
  4. public class BuildAssetBundlesFromDirectory { 
  5.  
  6.     [@MenuItem("Asset/Build AssetBundles From Directory of Files")] 
  7.  
  8.     static void ExportAssetBundles () { 
  9.  
  10.         // Get the selected directory 
  11.  
  12.         //獲取選擇的目錄 
  13.  
  14.         string path = AssetDatabase.GetAssetPath(Selection.activeObject); 
  15.  
  16.         Debug.Log("Selected Folder: " + path); 
  17.  
  18.         if (path.Length != 0) { 
  19.  
  20.             path = path.Replace("Assets/", ""); 
  21.  
  22.             string [] fileEntries = Directory.GetFiles(Application.dataPath+"/"+path); 
  23.  
  24.             foreach(string fileName in fileEntries) { 
  25.  
  26.                 string filePath = fileName.Replace("\\","/"); 
  27.  
  28.                 int index = filePath.LastIndexOf("/"); 
  29.  
  30.                 filePath = filePath.Substring(index+1); 
  31.  
  32.                 Debug.Log("filePath:"+filePath); 
  33.  
  34.                 string localPath = "Assets/" + path+"/"
  35.  
  36.                 if (index > 0
  37.  
  38.                 localPath += filePath; 
  39.  
  40.                 Object t = AssetDatabase.LoadMainAssetAtPath(localPath); 
  41.  
  42.                 if (t != null) { 
  43.  
  44.                     Debug.Log(t.name); 
  45.  
  46.                     string bundlePath = "Assets/" + path + "/" + t.name + ".unity3d"
  47.  
  48.                     Debug.Log("Building bundle at: " + bundlePath); 
  49.  
  50.                     // Build the resource file from the active selection. 
  51.  
  52.                     //從激活的選擇編譯資源文件 
  53.  
  54.                     BuildPipeline.BuildAssetBundle 
  55.  
  56.                     (t, null, bundlePath, BuildAssetBundleOptions.CompleteAssets); 
  57.  
  58.                 } 
  59.  
  60.  
  61.   
  62.             } 
  63.  
  64.         } 
  65.  
  66.     } 
  67.  
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:

  1. function Start () { 
  2.  
  3.     var www = new WWW ("file:///"+Application.dataPath+"/resourse/Cube.unity3d"); 
  4.  
  5.     yield www; 
  6.  
  7.     Instantiate(www.assetBundle.mainAsset); 
  8.  
function Start () {

	var www = new WWW ("file:///"+Application.dataPath+"/resourse/Cube.unity3d");

	yield www;

	Instantiate(www.assetBundle.mainAsset);

}
注:Application.dataPath獲取改程序的資源路徑。



  1. function Start () 
  2.  
  3. {  
  4.  
  5.     var www = WWW.LoadFromCacheOrDownload("http://210.30.12.33:8080/YangChun/file/Cube.unity3d",5);    
  6.  
  7.     yield www; 
  8.  
  9.     if (www.error != null
  10.  
  11.     { 
  12.  
  13.         Debug.Log (www.error); 
  14.  
  15.         return
  16.  
  17.     } 
  18.  
  19.    Instantiate(www.assetBundle.mainAsset); 
  20.  
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格式了。貌似
  1. LoadFromCacheOrDownload方法只能加載.unity3d格式文件,我用Tomcat服務器小測了一下,可以達到緩存的效果。 
LoadFromCacheOrDownload方法只能加載.unity3d格式文件,我用Tomcat服務器小測了一下,可以達到緩存的效果。



3.加載場景的話:

先把場景轉化成unity3d格式的。

注:以下代碼的腳本必須放在Editor文件夾下(如果沒有改文件,新建一個就行),BuildTarget注意哈,轉化成不同的平臺~~~BuildTarget.Andrdoid

  1. <pre class="java" name="code">@MenuItem ("Build/BuildWebplayerStreamed"
  2.  
  3. static function MyBuild(){ 
  4.  
  5.     var levels : String[] = ["Assets/yaya.unity"]; 
  6.  
  7.     BuildPipeline.BuildStreamedSceneAssetBundle( levels, "yaya.unity3d", BuildTarget.WebPlayer);//BuildTarget.Andrdoid 
  8.  
  9. </pre>或者 
  10. <pre></pre> 
  11. <pre style="background-color: rgb(255, 255, 255);" class="java" name="code"><pre class="java" name="code">@MenuItem ("Build/BuildWebplayerStreamed"
  12.  
  13. static function MyBuild(){ 
  14.  
  15.     BuildPipeline.BuildPlayer(["Assets/main.unity"],"VR.unity3d",BuildTarget.WebPlayer, BuildOptions.BuildAdditionalStreamedScenes);  
  16.  
  17. }</pre><br> 
  18. <br> 
  19. <br> 
  20. <pre></pre> 
  21. <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 () { 
  22.     // Download compressed scene. If version 5 of the file named "Streamed-Level1.unity3d" was previously downloaded and cached. 
  23.     // Then Unity will completely skip the download and load the decompressed scene directly from disk. 
  24.     //下載壓縮的場景。如果名爲Streamed-Level1.unity3d的文件版本爲5,預先下載並緩存。 
  25.     //然後Unity將完全跳過下載並直接從磁盤加載解壓的場景。 
  26.     var download = WWW.LoadFromCacheOrDownload ("http://210.30.12.16:8080/chunge/yaya.unity3d", 5); 
  27.     yield download; 
  28.  
  29.     // Handle error 
  30.     if (download.error != null
  31.     { 
  32.         Debug.LogError(download.error); 
  33.         return
  34.     } 
  35.  
  36.     // In order to make the scene available from LoadLevel, we have to load the asset bundle. 
  37.     // The AssetBundle class also lets you force unload all assets and file storage once it is no longer needed. 
  38.     //爲了使場景LoadLevel可用,必須加載資源包 
  39.     //AssetBundle類,還可以強制卸載所有的資源和文件存儲,一旦不再需要。 
  40.     var bundle = download.assetBundle; 
  41.  
  42.     // Load the level we have just downloaded 
  43.     //加載剛纔下載的關卡 
  44.     Application.LoadLevel ("yaya");//這裏面的“yaya”是指“Assets/yaya.unity”而不是指“yaya.unity3d” 
  45. }</pre><br><br></pre> 
  46. <br> 
  47. <br> 
  48. <br> 
  49.    
  50. <p></p> 
  51. <pre></pre> 
  52. <pre></pre> 
  53. <pre></pre> 
  54.  
  55. </pre> 
發佈了61 篇原創文章 · 獲贊 30 · 訪問量 38萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章