.unity3d格式的导出与加载

Posted 杨铭宇

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了.unity3d格式的导出与加载相关的知识,希望对你有一定的参考价值。

1、单一文件创建unity3d

技术分享
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\Editor目录下面),选中该文件夹,再点击菜单栏上的按钮"Asset/Build AssetBundles From Directory of Files",就成功转成unity3d格式了。

2、单一文件unity3d格式的加载

技术分享
function Start () {  
  
    var www = new WWW ("file:///"+Application.dataPath+"/resourse/Cube.unity3d");//Windows  
  
    yield www;  
  
    Instantiate(www.assetBundle.mainAsset);  
  
}  
技术分享

 

技术分享
function Start ()  
  
{   
  
    var www = WWW.LoadFromCacheOrDownload("http://210.30.12.33:8080/YangChun/file/Cube.unity3d",5);  //webPlayer   
  
    yield www;  
  
    if (www.error != null)  
  
    {  
  
        Debug.Log (www.error);  
  
        return;  
  
    }  
  
   Instantiate(www.assetBundle.mainAsset);  
  
}
技术分享

 

3、场景转化成unity3D格式

 

技术分享
@MenuItem ("Build/BuildScene") 
static function MyBuild(){  
  
    var levels : String[] = ["Assets/main.unity"];  //main为场景名称
  
    BuildPipeline.BuildStreamedSceneAssetBundle( levels, "test.unity3d", BuildTarget.WebPlayer);//BuildTarget.Andrdoid   “test.unity3d”为生成的文件名称
  
}  
//或者  
@MenuItem ("Build/BuildScene")  
  
static function MyBuild(){  
  
    BuildPipeline.BuildPlayer(["Assets/main.unity"],"test.unity3d",BuildTarget.WebPlayer, BuildOptions.BuildAdditionalStreamedScenes);   
  
}
技术分享

说明:默认生成的文件是在工程根目录下面,同样此文件得放在Assets\Editor目录下面;BuildTarget,转化成不同的平台,这个根据实际需要设置即可。

 

4、unity3D场景文件加载

 

技术分享
function Start () {
    // Download compressed scene. If version 5 of the file named "test.unity3d" was previously downloaded and cached.
    // Then Unity will completely skip the download and load the decompressed scene directly from disk.
    //下载压缩的场景。如果名为test.unity3d的文件版本为5,预先下载并缓存。
    //然后Unity将完全跳过下载并直接从磁盘加载解压的场景。
    var download = WWW.LoadFromCacheOrDownload ("file:///"+Application.dataPath + "/test.unity3d", 5);
//    var download = WWW.LoadFromCacheOrDownload ("http://myWebSite.com/test.unity3d", 5);

print(Application.dataPath + "/test.unity3d");
    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 ("main");
}
技术分享

 

以上是关于.unity3d格式的导出与加载的主要内容,如果未能解决你的问题,请参考以下文章

Unity3D Asset文件导出3DMax 可编辑格式

unity3d游戏开发学习分享之表面着色器讲解

Unity3d外部加载音频,视频,图片等资源 及根据路径获取制定格式的文件

Unity3d 与IOS 相互调用

使用Blender导出多个动画的GLTF模型

如何将模型从Blender导出到Unity3d