using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Networking;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class LoadNight : MonoBehaviour
{
public GameObject objXMZJ_Scene2;
public GameObject objBtnReturn;
bool loading;
private void OnMouseDown()
{
if (!loading)
{
loading = true;
string sceneName = "XMZJ_Scene2_2";
string abName = Application.isEditor ? sceneName + "_win" : sceneName;
string abPath = Application.streamingAssetsPath + "/Bundles/" + abName + ".unity3d";
StartCoroutine(LoadAssetBundle_Scene(abPath, sceneName, scene2_2Loaded));
}
}
GameObject objReturn;
void scene2_2Loaded()
{
objXMZJ_Scene2.SetActive(false);
objBtnReturn.SetActive(true);
objReturn = GameObject.Find("BtnXMZJReturn");
if (objReturn != null)
{
objReturn.SetActive(false);
}
SetCameraPosition();
loading = false;
}
public void Return()
{
objXMZJ_Scene2.SetActive(true);
objBtnReturn.SetActive(false);
if (currentSceneName != null)
{
currentSceneBundle.Unload(true);
SceneManager.UnloadSceneAsync(currentSceneName);
}
if (objReturn != null)
{
objReturn.SetActive(true);
}
SetCameraPosition();
}
///
/// 加载进度条
///
public Slider sliderProgress;
///
/// 显示进度条
///
void ShowSlider()
{
if (sliderProgress.value != 1)
{
sliderProgress.gameObject.SetActive(true);
}
}
AssetBundle oldSceneBundle;
AssetBundle currentSceneBundle;
string oldSceneName;
string currentSceneName;
public IEnumerator LoadAssetBundle_Scene(string abPath, string sceneName,
UnityAction BundleLoaded = null, LoadSceneMode mode = LoadSceneMode.Additive)
{
sliderProgress.value = 0;
ShowSlider();
// Caching.ClearCache();
UnityWebRequest download = UnityWebRequestAssetBundle.GetAssetBundle(abPath);
download.SendWebRequest();
while (!download.isDone)
{
sliderProgress.value = download.downloadProgress;
yield return null;
}
yield return download;
oldSceneBundle = currentSceneBundle;
currentSceneBundle = DownloadHandlerAssetBundle.GetContent(download);
// 异步加载场景
var sceneAsync = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);
currentSceneName = sceneName;
while (!sceneAsync.isDone)
{
yield return null;
}
sliderProgress.value = 1;
sliderProgress.gameObject.SetActive(false);
if (BundleLoaded != null)
{
BundleLoaded();
}
oldSceneName = sceneName;
}
///
/// 设置摄像机在先民足迹场景中的默认位置
///
void SetCameraPosition()
{
GameObject objSP = GameObject.Find("StartPosition2");
if (objSP != null)
{
Camera cam = Camera.main;
cam.gameObject.SetActive(false);
cam.transform.position = objSP.transform.position;
cam.transform.rotation = objSP.transform.rotation;
cam.gameObject.SetActive(true);
}
}
}