unity加载场景和切换场景

因为公司没什么大牛,所以很多东西只能自己摸索。

好像网上加载场景和切换场景有2种主流的方式

1)用Scenemanager加载和切换scene

2)将scene做成prefab,加载和切换prefab

我用的是第二种方式

1)将preafab打包成assetbundle

代码如下 :

// Builds an asset bundle from the selected objects in the project view.
// to build the Asset Bundle

using UnityEngine;
using UnityEditor;



public class ExportAssetBundles {

	[MenuItem("Assets/Build AssetBundle From Selection - Track dependencies -Android")]
	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,BuildTarget.Android);
			Selection.objects = selection;
		}
	}
}

当然也可以用添加AssetBundle Name 的方式来统一打包。

不建议用第一种方式,好像会把依赖的资源全打进去,而且官方已经放弃

2)加载和切换

代码如下:
  

  IEnumerator CoChangeScene(GameScene _scene )
    {
        //销毁当前场景
        for(int i=0;i

这就是我的切换逻辑了,很简陋,但也勉强能用,

你可能感兴趣的:(unity)