现在的位置: 首页 > 综合 > 正文

关于unity3d创建资源包和导入资源包

2019年07月21日 ⁄ 综合 ⁄ 共 5732字 ⁄ 字号 评论关闭

先看官方文档中的这两个类WWW.assetBundle,BuildPipeline

 

创建assetBundle

BuildPipeline
编译管线

Lets youprogrammatically build players or AssetBundles which can be loaded from theweb.

让你可以以编程方式生成播放器或资源包, 以便于从网页中加载。

Note: This is an editor class. To use it you have to place yourscript in Assets/Editor inside your project folder. Editor classes are in theUnityEditor namespace so for C# scripts you need to
add "usingUnityEditor;" at the beginning of the script.
注意:这是一个编辑器类,如果想使用它你需要把它放到工程目录下的Assets/Editor文件夹下。编辑器类在UnityEditor命名空间下。所以当使用C#脚本时,你需要在脚本前面加上
"usingUnityEditor"
引用。

Class Functions类函数

·        PushAssetDependencies

Lets you managecross-references and dependencies between different asset bundles and playerbuilds.
让你管理交叉引用和不同资源包和播放器构建之间的依赖关系。

·        PopAssetDependencies

Lets you managecross-references and dependencies between different asset bundles and playerbuilds.
让你管理交叉引用和不同资源包和播放器构建之间的依赖关系。

·        BuildPlayer

Builds a player (UnityPro only).
生成一个播放器(仅用于Unity Pro)。

·        BuildStreamedSceneAssetBundle

Builds one or morescenes and all it's dependencies into a compressed asset bundle. 
编译一个或多个场景和所有它依赖的压缩资源包。

·        BuildAssetBundle

Builds an asset bundle(Unity Pro only).
生成一个资源包(仅用于Unity Pro)。

·        BuildAssetBundleExplicit...

Builds an assetbundle, with custom names for the assets (Unity Pro only).
生成一个资源包,带有资源的自定义名称(仅用于Unity Pro)。

 

加载assetBundle

AssetBundle
资源包

Inherits from Object

AssetBundles let you stream additionalassets via the WWW class and instantiate them at runtime. AssetBundles arecreated via
BuildPipeline.BuildAssetBundle.

AssetBundles让你通过WWW类流式加载额外的资源并在运行时实例化它们。AssetBundles通过BuildPipeline.BuildAssetBundle创建。

参见: WWW.assetBundle,BuildPipeline.BuildPlayer.

function Start (){

        
//开始下载

        
var www = WWW("http://myserver/myBundle.unity3d");

        
//等待下载完成
    

        
yield www;

        
//获取指定的主资源并实例化

        
Instantiate(www.assetBundle.mainAsset);

}

Variables变量

mainAsset

Mainasset that was supplied when building the asset bundle (Read Only).
主资源在构建资源boundle时指定(只读)。

Functions函数

Contains

Checkif an AssetBundle contains a specific object.
如果AssetBundle的名称中包含特定的对象则进行检索。

Load

Loadsobject with name from the bundle.
bundle中加载名为name的对象。

LoadAsync

Asynchronouslyloads object with name of a given type from the bundle.
异步地从bundle中加载被指定类型的名为name的对象。

LoadAll

Loadsall objects contained in the asset bundle that inherit from type.
加载所有包含在资源bundle中且继承自type的对象。

Unload

Unloadsall assets in the bundle.
卸载包含在资源bundle中的所有对象。

Class Functions类函数

CreateFromMemory

Asynchronouslycreate an AssetBundle from a memory region.
从内存区异步创建资源包。

Inherited members继承成员

Inherited Variables继承变量

name

Thename of the object. //物体的名字

hideFlags

Shouldthe object be hidden, saved with the scene or modifiable by the user?
物体是否被隐藏、保存在场景中或被用户修改?

Inherited Functions继承函数

GetInstanceID

Returnsthe instance id of the object.
返回物体的实例ID

ToString

Returnsthe name of the game object.
返回游戏物体的名称。

Inherited Class Functions继承类函数

operator bool

Doesthe object exist?
物体是否存在?

Instantiate

Clonesthe object original and returns the clone.
克隆原始物体,并返回克隆的物体

Instantiate.<T>

Destroy

Removesa gameobject, component or asset.
删除一个游戏物体、组件或资源

DestroyImmediate

Destroysthe object obj immediately. It is strongly recommended to use Destroy instead.
立即销毁物体obj,强烈建议使用Destroy代替。

FindObjectsOfType

Returnsa list of all active loaded objects of Type type.
返回Type类型的所有激活的加载的物体列表

FindObjectOfType

Returnsthe first active loaded object of Type type.
返回Type类型第一个激活的加载的物体。

operator ==

Comparesif two objects refer to the same
比较如果两个物体相同

operator !=

Comparesif two objects refer to a different object
比较如果两个物体不同

DontDestroyOnLoad

Makesthe object target not be destroyed automatically when loading a new scene.
加载新场景的时候使目标物体不被自动销毁。

 

然后在Edit状态下生成资源包,在Editor文件下(没有则创建)加入脚本myEditor.cs

using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
using System.Text;
public class MyEditor : Editor 
{	
    //打包选中的文件和与之关联的文件成一个.unity3d文件
    [MenuItem("Assets/Build AssetBundle From Selection - Track dependencies")]
    static void ExportResource()
    {
        // Bring up save panel  弹出保存菜单      
        string path = EditorUtility.SaveFilePanel("Save Resource", "", "New Resource", "unity3d");
        if (path.Length != 0)
        {
            // Build the resource file from the active selection.            
            Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
            BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets);
            Selection.objects = selection;
        }
    }
    //只打包选中的文件成一个.unity3d文件
    [MenuItem("Assets/Build AssetBundle From Selection - No dependency tracking")]
    static void ExportResourceNoTrack()
    {
        // Bring up save panel      
        string path = EditorUtility.SaveFilePanel("Save Resource", "", "New Resource", "unity3d");
        if (path.Length != 0)
        {
            // Build the resource file from the active selection.            
            BuildPipeline.BuildAssetBundle(Selection.activeObject, Selection.objects, path);
        }
    }
    //打包选中的文件夹下的文件成一个.unity3d文件
    [MenuItem("GameObject/Build AssetBundles From Directory of Files")]
    static void ExportAssetBundles()
    {
        //获取选择的目录
        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);
                Debug.Log(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 System.Collections;

public class load : MonoBehaviour {

	// Use this for initialization
	void Start () {

        StartCoroutine(loadd());
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}

    IEnumerator loadd()
    {
        WWW www = new WWW("file:///" + Application.dataPath + "/agame.unity3d");
        yield return www;
        Instantiate(www.assetBundle.mainAsset);
        print("ok");
    }
}

 Application.dataPath为程序所在路径,把脚本挂在空的GameObject上即可把.unity3d文件加载到场景。

 

抱歉!评论已关闭.