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>