Unity3d资产绑定加载场景
我想将整个场景加载到我的unity项目中。 我已经创建了包含3个场景的资源包(场景01、场景02、场景03) 资产绑定导出如下所示Unity3d资产绑定加载场景,unity3d,Unity3d,我想将整个场景加载到我的unity项目中。 我已经创建了包含3个场景的资源包(场景01、场景02、场景03) 资产绑定导出如下所示 using UnityEngine; using UnityEditor; using System.Collections.Generic; public class CreateAssetBundle : EditorWindow { public const string bundlePath = "AssetBundle.unity3D"; [Menu
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
public class CreateAssetBundle : EditorWindow {
public const string bundlePath = "AssetBundle.unity3D";
[MenuItem("Bundle/Create")]
static void Open()
{
var w = EditorWindow.GetWindow <CreateAssetBundle>("Create bundle");
w.MyInit();
w.Show();
}
private Dictionary<string, bool> ScenesSelection;
void MyInit()
{
Debug.Log("Init window");
this.ScenesSelection = new Dictionary<string, bool>();
foreach (var scene in EditorBuildSettings.scenes)
{
Debug.Log("Add scene : " + scene.path);
this.ScenesSelection.Add(scene.path, false);
}
}
void OnGUI()
{
if (this.ScenesSelection == null)
{
this.MyInit();
}
foreach (var scene in EditorBuildSettings.scenes)
{
if (this.ScenesSelection.ContainsKey(scene.path))
{
this.ScenesSelection[scene.path] = EditorGUILayout.Toggle(scene.path, this.ScenesSelection[scene.path]);
}
}
if (GUILayout.Button("Create bundle"))
{
List<string> selectedScenes = new List<string>();
foreach (var scene in EditorBuildSettings.scenes)
{
if (this.ScenesSelection[scene.path])
{
selectedScenes.Add(scene.path);
}
}
BuildPipeline.PushAssetDependencies();
BuildPipeline.BuildPlayer(selectedScenes.ToArray(), bundlePath, BuildTarget.iPhone,
BuildOptions.UncompressedAssetBundle | BuildOptions.BuildAdditionalStreamedScenes
);
BuildPipeline.PopAssetDependencies();
}
}
上次调试返回“false”。Unity表示“无法加载级别'scene01'(-1),因为它尚未添加到构建设置中。”
我做错了什么。我需要这个在ios和android设备上工作。
有什么想法吗?在Unity 5之前,您应该在尝试加载场景之前致电:
此后,该函数已被弃用,必须改用。它使用 因此,我通过这个小代码创建资产包
@MenuItem ("Build/BuildWebplayerStreamed")
static function MyBuild(){
var levels : String[] = ["Assets/Level1.unity"];
BuildPipeline.BuildStreamedSceneAssetBundle( levels, "Streamed-Level1.unity3d", BuildTarget.iPhone);
}
之后,我通过以下方式加载场景:
IEnumerator Start ()
{
// Wait for the Caching system to be ready
while (!Caching.ready)
yield return null;
// Load the AssetBundle file from Cache if it exists with the same version or download and store it in the cache
using(WWW www = WWW.LoadFromCacheOrDownload (url, 1)){
yield return www;
if (www.error != null)
throw new Exception("WWW download had an error:" + www.error);
AssetBundle bundle = www.assetBundle;
bundle.LoadAll();
}
Application.LoadLevel ("scene01");
}
就这样。我想补充的是一个进度条。但是当我使用www.bytesdownloadded时,我得到一个错误,即“只能使用assetBundle属性访问WWWCached数据!”
也许有人知道这个问题的答案?谢谢你的回答。但是当我通过assetbundle加载场景时,它不能已经在构建设置中。因为这样我就不需要assetbundle了,对吗?用不同的答案编辑;在调试之前。它仍然给我同样的错误。调试返回“false”和缺少的场景。这可能是我试图在iphone和android上使用它的问题吗?你已经仔细检查了场景是否确实在资产包中,是吗?事实上,它是针对iOS或Android的,在这个例子中没有区别。事实上,我不知道如何测试它。这是我第一次尝试使用资产捆绑包。我使用上面的脚本来创建它。但我不确定现在是否所有东西都在那包里。
IEnumerator Start ()
{
// Wait for the Caching system to be ready
while (!Caching.ready)
yield return null;
// Load the AssetBundle file from Cache if it exists with the same version or download and store it in the cache
using(WWW www = WWW.LoadFromCacheOrDownload (url, 1)){
yield return www;
if (www.error != null)
throw new Exception("WWW download had an error:" + www.error);
AssetBundle bundle = www.assetBundle;
bundle.LoadAll();
}
Application.LoadLevel ("scene01");
}