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<sce_scene.Length;i++)
        {
            
            if(sce_scene[i]!=null&&i!=(int)_scene)
            {
                
                DestroyImmediate(sce_scene[i]);
                sce_scene[i]=null;

            }
        }

        Resources.UnloadUnusedAssets();
        GC.Collect();
        if ((pf_scene [(int)_scene]) != null) {
            
                sce_scene [(int)_scene] = Instantiate (pf_scene [(int)_scene]) as GameObject;

        } else {

            yield return  StartCoroutine (StResource.GetInstance ().CoLoadScene (_scene));
    

            sce_scene [(int)_scene] = Instantiate (StResource.GetInstance ().GetScene (_scene)) as GameObject;


        }

        sce_scene[(int)_scene].transform.SetParent(this.transform.parent.transform);
        sce_scene[(int)_scene].transform.localPosition=new Vector3(0,0,0);
        sce_scene[(int)_scene].transform.localScale=new Vector3(1,1,1);
        this.transform.SetAsLastSibling();


        StResource.GetInstance().UnLoadScene( _scene,false);


        yield return null;
        
    }

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

Unity场景跳转是一个常见的功能需求,主要用于切换游戏的不同部分或阶段。以下是关于 Unity 场景跳转的基本介绍: ### 使用 `SceneManager` 实现场景跳转 从 Unity 5 开始,推荐使用 `UnityEngine.SceneManagement.SceneManager` 来管理场景加载跳转操作。 #### 基本步骤: 1. **引入命名空间** 首先需要导入 Scene Management 的命名空间: ```csharp using UnityEngine; using UnityEngine.SceneManagement; // 引入此命名空间用于场景管理 ``` 2. **创建方法实现跳转** 可通过下面几种方式之一完成场景跳转: - 按照索引值加载场景: 索引值对应于编辑器内 “Build Settings” 设置里各个场景的位置编号。 ```csharp SceneManager.LoadScene(0); // 加载第一个场景(假设其索引为 0) ``` - 根据名称加载场景: 如果已知目标场景的确切名字,则可以直接指定它进行加载。 ```csharp SceneManager.LoadScene("SceneName"); // 替换 "SceneName" 成实际的场景名 ``` 3. **异步加载提高性能** 对大型复杂项目来说,同步阻塞式加载可能会影响用户体验,此时可以采用异步加载的方式优化流程。 ```csharp AsyncOperation asyncLoad = SceneManager.LoadSceneAsync("NextLevel"); while (!asyncLoad.isDone) { float progress = Mathf.Clamp01(asyncLoad.progress / 0.9f); Debug.Log($"Loading Progress: {progress * 100:F2}%"); yield return null; } ``` 4. **附加选项控制加载模式** 当调用 LoadScene 方法时还可以传递第二个参数来决定如何处理新旧场景之间的数据清理等问题,默认行为会卸载当前活动场景的所有内容;如果想要保留现有状态并叠加新增元素的话可以选择 Additive 参数。 ```csharp SceneManager.LoadScene(sceneIndex, LoadSceneMode.Additive); ``` 以上便是基于 C# 脚本在 Unity 编程环境中实现简单以及进阶版场景切换的主要思路技术点概述啦! ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值