美文网首页
AssetBundle用来打包场景模型资源与加载场景模型资源

AssetBundle用来打包场景模型资源与加载场景模型资源

作者: 你们不在我幸福给谁看 | 来源:发表于2017-05-12 15:29 被阅读0次

    加载模型资源及场景和模型的坑,注意保存名字不要默认名

    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    打包场景和模型资源脚本图和代码:

    using System.Collections;

    using System.Collections.Generic;

    using UnityEditor;

    using UnityEngine;

    public class ExportAssetBundles : MonoBehaviour {

    //在Unity编辑器中添加菜单

    [MenuItem("Assets/Build AssetBundle From Selection")]

    static void ExportResourceRGB2()

    {

    // 打开保存面板,获得用户选择的路径

    string path = EditorUtility.SaveFilePanel("Save Resource", "", "New Resource", "assetbundle");

    if (path.Length != 0)

    {

    // 选择的要保存的对象

    Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);

    //打包

    BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, BuildTarget.StandaloneWindows);

    }

    }

    [MenuItem("Assets/Save Scene")]

    static void ExportScene()

    {

    // 打开保存面板,获得用户选择的路径

    string path = EditorUtility.SaveFilePanel("Save Resource", "", "New Resource", "unity3d");

    if (path.Length != 0)

    {

    // 选择的要保存的对象

    Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);

    string[] scenes = { "Assets/111.unity" };

    //打包

    BuildPipeline.BuildPlayer(scenes, path, BuildTarget.StandaloneWindows, BuildOptions.BuildAdditionalStreamedScenes);

    }

    }

    }

    ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    加载资源及场景脚本图和代码:

    using System;

    using UnityEngine;

    using System.Collections;

    public class Test : MonoBehaviour {

    private string BundleURL = "file://F:/MyWorke/MyWallDemo/Assets/Cube.assetBundle";

    private string SceneURL = "file://F:/MyWorke/MyWallDemo/Assets/111.unity3d";

    void Start()

    {

    //BundleURL = "file//"+Application.dataPath+"/cube.assetbundle";

    Debug.Log(BundleURL);

    StartCoroutine(DownloadAssetAndScene());

    }

    IEnumerator DownloadAssetAndScene()

    {

    //下载assetbundle,加载Cube

    //using (WWW asset = new WWW(BundleURL))

    //{

    //    yield return asset;

    //    AssetBundle bundle = asset.assetBundle;

    //  GameObject.Instantiate(bundle.LoadAsset("Cube"));

    //    bundle.Unload(false);

    //    yield return new WaitForSeconds(5);

    //}

    // 下载场景,加载场景

    using (WWW scene = new WWW(SceneURL))

    {

    yield return scene;

    AssetBundle bundle = scene.assetBundle;

    Application.LoadLevel("111");

    }

    }

    }

    相关文章

      网友评论

          本文标题:AssetBundle用来打包场景模型资源与加载场景模型资源

          本文链接:https://www.haomeiwen.com/subject/rysmtxtx.html