zoukankan      html  css  js  c++  java
  • BuildPipeline.BuildAssetBundle 编译资源包

    原文出自:http://blog.csdn.net/nateyang/article/details/7567831

    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.   
    68. }  
    
    

      注意: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.   
    9. }  
    注: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.   
    21. }  

          我试了一下用Resources类的方法还不能加载unity3d格式的文件。不过如果是本地加载的话直接加载prefab就可以了,用不着用unity3d格式了。貌似
    1. LoadFromCacheOrDownload方法只能加载.unity3d格式文件,我用Tomcat服务器小测了一下,可以达到缓存的效果。  

    3.加载场景的话:

    先把场景转化成unity3d格式的。

    注:以下代码的脚本必须放在Editor文件夹下(如果没有改文件,新建一个就行),BuildTarget注意哈,转化成不同的平台~~~BuildTarget.Andrdoid

      1. <pre name="code" class="java">@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. }  
      10. </pre>或者  
      11. <pre></pre>  
      12. <pre name="code" class="java" style="background-color: rgb(255, 255, 255); "><pre name="code" class="java">@MenuItem ("Build/BuildWebplayerStreamed")  
      13.   
      14. static function MyBuild(){  
      15.   
      16.     BuildPipeline.BuildPlayer(["Assets/main.unity"],"VR.unity3d",BuildTarget.WebPlayer, BuildOptions.BuildAdditionalStreamedScenes);   
      17.   
      18. }</pre><br>  
      19. <br>  
      20. <br>  
      21. <pre></pre>  
      22. <pre style="margin-top:0px; margin-bottom:0px; background-color:rgb(238,238,238); font-family:Verdana,Geneva,sans-serif,宋体; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; white-space:pre-wrap; word-wrap:break-word; color:rgb(53,47,40); line-height:20px"><pre name="code" class="java">function Start () {  
      23.     // Download compressed scene. If version 5 of the file named "Streamed-Level1.unity3d" was previously downloaded and cached.  
      24.     // Then Unity will completely skip the download and load the decompressed scene directly from disk.  
      25.     //下载压缩的场景。如果名为Streamed-Level1.unity3d的文件版本为5,预先下载并缓存。  
      26.     //然后Unity将完全跳过下载并直接从磁盘加载解压的场景。  
      27.     var download = WWW.LoadFromCacheOrDownload ("http://210.30.12.16:8080/chunge/yaya.unity3d"5);  
      28.     yield download;  
      29.   
      30.     // Handle error  
      31.     if (download.error != null)  
      32.     {  
      33.         Debug.LogError(download.error);  
      34.         return;  
      35.     }  
      36.   
      37.     // In order to make the scene available from LoadLevel, we have to load the asset bundle.  
      38.     // The AssetBundle class also lets you force unload all assets and file storage once it is no longer needed.  
      39.     //为了使场景LoadLevel可用,必须加载资源包  
      40.     //AssetBundle类,还可以强制卸载所有的资源和文件存储,一旦不再需要。  
      41.     var bundle = download.assetBundle;  
      42.   
      43.     // Load the level we have just downloaded  
      44.     //加载刚才下载的关卡  
      45.     Application.LoadLevel ("yaya");//这里面的“yaya”是指“Assets/yaya.unity”而不是指“yaya.unity3d”  
      46. }</pre><br><br></pre>  
      47. <br>  
      48. <br>  
      49. <br>  
      50.     
      51. <p></p>  
      52. <pre></pre>  
      53. <pre></pre>  
      54. <pre></pre>  
      55.   
      56. </pre>  
  • 相关阅读:
    Drupal 学习笔记 (4)
    property和field有什么区别
    Razor4Orchard v1.2
    Mono喜迁新家http://www.xamarin.com/
    jq中获取属性名的方法
    jquery中的文档操作之五
    PHP基础知识之三
    php基础知识之一
    注意点
    jquery中ajax的学习之$.ajax()
  • 原文地址:https://www.cnblogs.com/hewei2012/p/3779108.html
Copyright © 2011-2022 走看看