142 lines
3.5 KiB
C#
142 lines
3.5 KiB
C#
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();
|
|
}
|
|
|
|
/// <summary>
|
|
/// ¼ÓÔØ½ø¶ÈÌõ
|
|
/// </summary>
|
|
public Slider sliderProgress;
|
|
|
|
/// <summary>
|
|
/// ÏÔʾ½ø¶ÈÌõ
|
|
/// </summary>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// ÉèÖÃÉãÏñ»úÔÚÏÈÃñ×ã¼£³¡¾°ÖеÄĬÈÏλÖÃ
|
|
/// </summary>
|
|
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);
|
|
}
|
|
}
|
|
}
|