unity场景打包及动态加载

       我的项目需求会根据不同的业务环境,选择进入不同的3D场景进行作业,客户的需求变动,随时会需要增加新的3D场景,因此采用将场景整体打包成bundle的方式,程序框架通过配置表获取场景地址,动态加载并更新。

1、Scene打包

	// unity Scene Packer
	[MenuItem("Tool/SceneBunlde Builder")]
	private static void BuildSceneBundle(){
		BuildPlayerOptions _buildOptions=new BuildPlayerOptions(){
			scenes = new string[]{"Assets/Scene/Test.unity"},//打包场景地址
			locationPathName = Application.streamingAssetsPath+ "/WindowsTestScene",//ab包资源地址
			target = BuildTarget.StandaloneWindows,//目标平台
			options = BuildOptions.BuildAdditionalStreamedScenes//打包选项
		};

		BuildPipeline.BuildPlayer (_buildOptions);
	
	}

2、场景读取与加载

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class LoadSceneData : MonoBehaviour {

	// Use this for initialization
	void Start () {
		string _path = "file://"+Application.streamingAssetsPath + "/WindowsTestScene";
		StartCoroutine (LoadSceneFromBundle (_path));
	}

	private IEnumerator LoadSceneFromBundle(string _path)
	{
		WWW www = new WWW (_path);
		yield return www;
		if (www.error == null) {
			AssetBundle _bundle = www.assetBundle;
			if (_bundle != null) {
			    SceneManager.LoadSceneAsync ("Test");
                            _bundle.Unload (false);
			}
			
		}
		www.Dispose ();
	}

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

 

你可能感兴趣的:(unity)