设定
场景A->场景C,但是场景C资源太大了,直接加载跳转时会出现跳转场景C的时候出现卡顿,因此在场景加载的时候,做一个场景B(黑幕效果)的过渡,不至于卡顿的太尴尬,于是需要实现场景A->场景B->场景C。
1、选择场景界面
其中ButtonState脚本ButtonState.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class ButtonState : MonoBehaviour
{
[SerializeField]
private float localScale;
[SerializeField]
private float PointScale;
public void OnPointerEnter(PointerEventData eventData)//当鼠标进入UI后执行的事件执行的
??? {
transform.localScale = new Vector3(PointScale, PointScale, PointScale);
}
public void OnPointerExit(PointerEventData eventData)//当鼠标离开UI后执行的事件执行的
??? {
transform.localScale = new Vector3(localScale, localScale, localScale);
}
public void OnDisable()
{
transform.localScale = new Vector3(localScale, localScale, localScale);
}
}
二、GameObject上面挂的脚本:GameController.cs
?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameController : MonoBehaviour
{
public static List<LanguageScence> dic;
//public AudioSource AudioEffect;
public GameObject AudioBg;
public static int M;
private static bool Isplay = false;
private GameObject go;
private void Awake()
{
if (!Isplay)
{
go = Instantiate(AudioBg);//播放背景音乐
}
}
//选择语言后,设置变量值,更换场景
public void SelectLanguage(string name)
{
switch (name)
{
case "Chinese":
M = 1;
SceneLoad();
break;
case "English":
M = 2;
SceneLoad();
break;
case "Finnish":
M = 3;
SceneLoad();
break;
}
}
//场景转换
void SceneLoad()
{
if (!Isplay)
{
DontDestroyOnLoad(go);
Isplay = true;
}
Debug.Log("先去黑球的场景");
SceneManager.LoadScene("loadScene");
}
}
三、需要支持的脚本
①LanguageScence.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LanguageScence : MonoBehaviour
{
public List<string> ELanguageText = new List<string>();
}
②LanguageState.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//public class LanguageState : MonoBehaviour
//{
//}
public enum LanguageState//2更改class为enum枚举值
{
Chinese,
English,
Finnish
}
四、三个跳转场景
五、 黑幕场景:loadScene
黑幕场景脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class AsnyScene : MonoBehaviour
{
AsyncOperation async = null;
void Awake()
{
StartCoroutine(asynEvent(GameController.M));//异步加载场景
}
void Update()
{
}
IEnumerator asynEvent(int level)
{
Debug.Log("转场黑幕出现");
yield return null;
AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(level);//场景不进行叠加
asyncOperation.allowSceneActivation = true;//加载完成之后是否允许跳转
}
}
六、总结
场景A->场景B->场景C时,因为场景B(黑幕场景)为资源不大的场景,所以可以直接进行跳转,可通过直接加载的方式,但是场景B->场景C时,场景C的资源文件多,导致加载慢,所以在黑幕场景下采用异步加载的方式,因此,场景A->场景B(黑幕场景)中使用:
SceneManager.LoadScene("黑幕场景名或者索引");
就可以,而在场景B->场景C时,将黑幕场景转换到场景C时,使用异步加载:
AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(“场景名字或者场景索引”);//场景不进行叠加
? ? ? ? asyncOperation.allowSceneActivation = true;//加载完成之后是否允许跳转
即在未加载出场景C时,一直停留在场景B中,处在黑幕场景下,解决加载时的尴尬。
|