using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class LoadScene : MonoSingleton<LoadScene>
{
public Button SystemTeachingModeButton;// 系统教学按钮
public Button TeachingModeButton;// 教学模式按钮
public Button PracticeModeButton;// 练习模式按钮
public Button AssessmentModeButton;// 考核模式按钮
public Text progressText;// 进度条Text
public Image progressImage;// 进度条显示的Image
public Button close;
public bool isLoading ;
protected override void Awake()
{
base.Awake();
}
private void OnEnable()
{
if (progressImage !=null)
{
progressText.text = "";// 进度条的Text
progressImage.fillAmount = 0;
}
}
private void Start()
{
// Debug.Log("开始");
if (SystemTeachingModeButton!=null)
{
SystemTeachingModeButton.onClick.RemoveAllListeners();
SystemTeachingModeButton.onClick.AddListener(() => {
StartCoroutine(LoadSceneManager(TypeOfEnums.ScenesName.SystemTeachingModeScene.ToString()));
GlobalConfig.currentDecutionModle =TypeOfEnums.DeductionModle.systemTeaching;
});
}
if (TeachingModeButton!=null)
{
TeachingModeButton.onClick.RemoveAllListeners();
TeachingModeButton.onClick.AddListener(() => {
StartCoroutine(LoadSceneManager(TypeOfEnums.ScenesName.TeachingModeScene.ToString()));
GlobalConfig.currentDecutionModle =TypeOfEnums.DeductionModle.teaching;
});
}
if (PracticeModeButton != null)
{
PracticeModeButton.onClick.RemoveAllListeners();
PracticeModeButton.onClick.AddListener(() => {
StartCoroutine(LoadSceneManager(TypeOfEnums.ScenesName.PracticeModeScene.ToString()));
GlobalConfig.currentDecutionModle =TypeOfEnums.DeductionModle.practice;
});
}
if (AssessmentModeButton!=null)
{
AssessmentModeButton.onClick.RemoveAllListeners();
AssessmentModeButton.onClick.AddListener(() => {
StartCoroutine(LoadSceneManager(TypeOfEnums.ScenesName.AssessmentModeScene.ToString()));
GlobalConfig.currentDecutionModle =TypeOfEnums.DeductionModle.Assessment;
});
}
if (close != null) {
close.onClick.AddListener (
()=>{
Application.Quit();
}
);
}
}
private float targetValue;
AsyncOperation asyncOperation ;
float loadingSpeed =1;
/// <summary>
///
/// </summary>
public void Update(){
if (asyncOperation != null)
{
targetValue = asyncOperation.progress;
if (asyncOperation.progress >= 0.9f) {
//operation.progress的值最大为0.9
targetValue = 1.0f;
}
if (targetValue != progressImage.fillAmount) {
//插值运算
progressImage.fillAmount = Mathf.Lerp (progressImage.fillAmount, targetValue, Time.deltaTime * loadingSpeed);
if (Mathf.Abs (progressImage.fillAmount - targetValue) < 0.01f) {
progressImage.fillAmount = targetValue;
}
}
progressText.text = ((int)(progressImage.fillAmount * 100)).ToString () + "%";
if ((int)(progressImage.fillAmount * 100) == 100) {
isLoading = false;
//允许异步加载完毕后自动切换场景
asyncOperation.allowSceneActivation = true;
}
}
}
/// <summary>
/// 加载场景
/// </summary>
public IEnumerator LoadSceneManager(string sceneName)
{
if (isLoading)
yield break;
asyncOperation = SceneManager.LoadSceneAsync(sceneName);
asyncOperation.allowSceneActivation = false;
isLoading = true;
//重置静态变量
GlobalConfig.ResetAll.Reset ();
yield return asyncOperation;
}
public bool isLoadingSelect;
// <summary>
// 加载进入选择场景场景 场景小,,,,,但这样是不科学的
// </summary>
public IEnumerator LoadSelectSceneManager()
{
if (isLoadingSelect)
yield break;
float i = 0;
AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(TypeOfEnums.ScenesName.SelectScene.ToString());
asyncOperation.allowSceneActivation = false;
while (i < 100)
{
i++;
yield return new WaitForEndOfFrame();
}
yield return new WaitForSeconds(0.5f);
isLoadingSelect = false;
asyncOperation.allowSceneActivation = true;
}
}
// 方便其他场景调用
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MonoSingleton<T> : MonoBehaviour where T : MonoBehaviour
{
#region 单例
private static T instance;// 声明静态对象
// 提供单例接口
public static T Instance
{
get
{
if (instance == null)
{
// 如果场景中没有任何对象挂在了该组件,就创建一个对象
// 并把该组件挂载上去
GameObject obj = new GameObject(typeof(T).Name);
instance = obj.AddComponent<T>();
}
return instance;
}
}
#endregion
#region Unity回调方法
protected virtual void Awake()
{
instance = this as T;
}
#endregion
}