枚举类型与字符串添加字典互转ConvertEnumToString
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using Enums;
public class startScene : MonoBehaviour
{
//点击事件
public void startOnClick()
{
global_Mgr.EnterNextScenes(Scenes.MainMenuScene)
}
}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using Enums;
public class global_Mgr
{
//定义全局静态变量
public static Scene NextScenesEnum = Scene.LoadScene;
//定义全局静态加载
public static void EnterNextScenes(Scenes scenesEnumName)
{
NextScenesEnum = scenesEnumName;
SceneManager.LoadScene(Config.ConvertEnumToStr(Scenes.LoadScene));
}
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace Enums
{ //枚举场景类型命名
public enum Scenes
{
StartScene,
SubmenuScene,
LoadScene,
MainMenuScene
}
//定义字典枚举转字符串类
public class Config
{
//定义字典
public static Dictionary<Scenes, string> dictionary;
//方法类型转换、参数枚举场景名、返回值场景名称字符串
public static string ConvertEnumToStr(Scenes Scenes)
{
dictionary = new Dictionary<Scenes, string>();
dictionary.Add(Scenes.StartScene, "StartScene");
dictionary.Add(Scenes.SubmenuScene, "SubmenuScene");
dictionary.Add(Scenes.LoadScene, "LoadScene");
dictionary.Add(Scenes.MainMenuScene, "MainMenuScene");
if (!dictionary.ContainsKey(Scenes))
{
Debug.Log("error");
return null;
}
return dictionary[Scenes];
}
}
}
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using Enums;
//场景读条加载(详细备注前篇:LoadScene场景异步加载(2018-09-06 13:30))
public class Load : MonoBehaviour
{
public Slider Slider;
private AsyncOperation async;
private int number = 0;
private int num = 0;
private void Start()
{
Slider.value = 0;
StartCoroutine("start");
}
void Update()
{
Slider.value = number / 100f;
}
private IEnumerator start()
{
async = SceneManager.LoadSceneAsync(Config.ConvertEnumToStr(global_Mgr.NextScenesEnum));
async.allowSceneActivation = false;
while (async.progress < 0.9f)
{
num = (int)async.progress * 100;
while (number < num)
{
++number;
yield return new WaitForSeconds(0);
}
}
num = 100;
while (number < num)
{
++number;
yield return new WaitForSeconds(0);
}
async.allowSceneActivation = true;
}
}
注:在场景或关卡多时,运用较为方便,可一个加载场景在每个关卡使用,字典方法ConvertEnumToStr可根据场景,修改添加方便使用...