demo多场景运行切换工具

本文介绍了一个Unity编辑器下的场景切换工具——SceneSwitcher类。该工具允许开发者在运行时加载不同的场景,并支持加成加载模式。通过简单的GUI界面,用户可以方便地选择并切换到所需的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

curvy插件中的,很好用

这里写图片描述

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using FluffyUnderware.DevTools.Extensions;
using System.Collections.Generic;

#if UNITY_EDITOR
using UnityEditor;
#endif

public class SceneSwitcher : MonoBehaviour {

    
    bool mChkAdditive;
    Vector2 sv = Vector2.zero;
    Rect mDropdownRect;
    string[] mItems;
    bool mShow;

#if UNITY_EDITOR
    void Start ()
    {
        
    }
#endif

    int CurrentLevel
    {
        get
        {
#if UNITY_5_0 || UNITY_5_1 || UNITY_5_2
            return Application.loadedLevel;
#else
            return UnityEngine.SceneManagement.SceneManager.GetActiveScene().buildIndex;
#endif
        }
        set
        {
            if (CurrentLevel != value)
            {
#if UNITY_5_0 || UNITY_5_1 || UNITY_5_2
                if (mChkAdditive)
                    Application.LoadLevelAdditive(value);
                else
                    Application.LoadLevel(value);
#else
                if (mChkAdditive)
                    UnityEngine.SceneManagement.SceneManager.LoadScene(value, UnityEngine.SceneManagement.LoadSceneMode.Additive);
                else
                    UnityEngine.SceneManagement.SceneManager.LoadScene(value,UnityEngine.SceneManagement.LoadSceneMode.Single);
#endif
            }
        }
    }

    void getScenes()
    {
        var items = new List<string>();
#if UNITY_EDITOR
        foreach (var lvl in EditorBuildSettings.scenes)
        {
            if (lvl.enabled)
            {
                var path = lvl.path.Split('/');
                items.Add(path[path.Length - 1].TrimEnd(".unity"));
            }
        }
#endif
        mItems = items.ToArray();
    }


    void onSelect(object index)
    {
        CurrentLevel = (int)index;
    }

    void OnGUI()
    {
        if (mItems == null)
        {
            mDropdownRect = new Rect(Screen.width - 200, 0, 295, 300);
            getScenes();
        }
        
        if (mItems.Length == 0 || CurrentLevel<0)
        {
            GUI.Label(new Rect(Screen.width-480, mDropdownRect.y, 460, 40),"Add scenes to the build settings to enable the scene switcher!");
        }
        else {
            mChkAdditive=GUI.Toggle(new Rect(mDropdownRect.x - 200, mDropdownRect.y, 80, 20), mChkAdditive, "Additive");
            if (GUI.Button(new Rect((mDropdownRect.x - 100), mDropdownRect.y, mDropdownRect.width, 25), ""))
            {
                if (!mShow)
                {
                    mShow = true;
                }
                else
                {
                    mShow = false;
                }
            }

            if (mShow)
            {
                sv = GUI.BeginScrollView(new Rect((mDropdownRect.x - 100), (mDropdownRect.y + 25), mDropdownRect.width, mDropdownRect.height), sv, new Rect(0, 0, mDropdownRect.width, Mathf.Max(mDropdownRect.height, (mItems.Length * 25))));

                GUI.Box(new Rect(0, 0, mDropdownRect.width, Mathf.Max(mDropdownRect.height, (mItems.Length * 25))), "");

                for (int index = 0; index < mItems.Length; index++)
                {

                    if (GUI.Button(new Rect(0, (index * 25), mDropdownRect.height, 25), ""))
                    {
                        mShow = false;
                        CurrentLevel = index;
                    }

                    GUI.Label(new Rect(5, (index * 25), mDropdownRect.height, 25), mItems[index]);

                }

                GUI.EndScrollView();
            }
            else if (CurrentLevel>=0 && CurrentLevel<mItems.Length)
            {
                GUI.Label(new Rect((mDropdownRect.x - 95), mDropdownRect.y, 300, 25), mItems[CurrentLevel]);
            }
        }

    }


}

#Editor模式下可以不添加场景实现场景切换

    private void Start()
    {
        StartCoroutine(LoadSceneCoroutine("Assets/Arts/px_scene/enginetest/role/ModelPlayer.unity"));
    }


    private IEnumerator LoadSceneCoroutine(string strResourcePath)
    {
        //ResourceManager.Instance.SetSwitchingLevel(true);

        //var async = UnityEditor.EditorApplication.LoadLevelAsyncInPlayMode(strResourcePath);
        //yield return async;
        yield return null;
        OpenUI();
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值