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万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章