200 lines
4.6 KiB
C#
200 lines
4.6 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
namespace Michsky.LSS
|
|
{
|
|
[AddComponentMenu("Loading Screen Studio/LSS Manager")]
|
|
public class LSS_Manager : MonoBehaviour
|
|
{
|
|
public enum LoadingMode
|
|
{
|
|
Single = 0,
|
|
Additive = 1
|
|
}
|
|
|
|
public LoadingMode loadingMode;
|
|
|
|
public string presetName = "Default";
|
|
|
|
public bool enableTrigger;
|
|
|
|
public bool onTriggerExit;
|
|
|
|
public bool loadWithTag;
|
|
|
|
public bool startLoadingAtStart;
|
|
|
|
public string objectTag;
|
|
|
|
public string sceneName;
|
|
|
|
[Range(0f, 10f)]
|
|
public float audioFadeDuration = 2f;
|
|
|
|
public List<AudioSource> audioSources = new List<AudioSource>();
|
|
|
|
public Object[] loadingScreens;
|
|
|
|
public int selectedLoadingIndex;
|
|
|
|
public int selectedTagIndex;
|
|
|
|
public UnityEvent onLoadingStart;
|
|
|
|
public List<GameObject> dontDestroyOnLoad = new List<GameObject>();
|
|
|
|
[SerializeField]
|
|
private List<string> loadedScenes = new List<string>();
|
|
|
|
private void Start()
|
|
{
|
|
if (startLoadingAtStart && loadingMode == LoadingMode.Single)
|
|
{
|
|
LoadScene(sceneName);
|
|
}
|
|
else if (startLoadingAtStart && loadingMode == LoadingMode.Additive)
|
|
{
|
|
LoadSceneAdditive(sceneName);
|
|
}
|
|
}
|
|
|
|
public void SetPreset(string styleName)
|
|
{
|
|
presetName = styleName;
|
|
}
|
|
|
|
public void LoadScene(string sceneName, Sprite sceneImage = null)
|
|
{
|
|
LSS_LoadingScreen.presetName = presetName;
|
|
LSS_LoadingScreen.LoadScene(sceneName, sceneImage);
|
|
for (int i = 0; i < dontDestroyOnLoad.Count; i++)
|
|
{
|
|
Object.DontDestroyOnLoad(dontDestroyOnLoad[i]);
|
|
}
|
|
if (audioSources.Count != 0)
|
|
{
|
|
foreach (AudioSource audioSource in audioSources)
|
|
{
|
|
LSS_AudioSource lSS_AudioSource = audioSource.gameObject.AddComponent<LSS_AudioSource>();
|
|
lSS_AudioSource.audioSource = audioSource;
|
|
lSS_AudioSource.audioFadeDuration = audioFadeDuration;
|
|
lSS_AudioSource.DoFadeOut();
|
|
}
|
|
}
|
|
onLoadingStart.Invoke();
|
|
}
|
|
|
|
public void LoadSceneAdditive(string sceneName)
|
|
{
|
|
LSS_LoadingScreen.LoadSceneAdditive(sceneName, presetName);
|
|
loadedScenes.Add(SceneManager.GetSceneByName(sceneName).name);
|
|
if (audioSources.Count != 0)
|
|
{
|
|
foreach (AudioSource audioSource in audioSources)
|
|
{
|
|
if (!(audioSource == null))
|
|
{
|
|
LSS_AudioSource lSS_AudioSource = audioSource.gameObject.AddComponent<LSS_AudioSource>();
|
|
lSS_AudioSource.audioSource = audioSource;
|
|
lSS_AudioSource.audioFadeDuration = audioFadeDuration;
|
|
lSS_AudioSource.DoFadeOut();
|
|
}
|
|
}
|
|
}
|
|
onLoadingStart.Invoke();
|
|
}
|
|
|
|
public void LoadSceneAdditiveInstant(string sceneName)
|
|
{
|
|
SceneManager.LoadScene(sceneName, LoadSceneMode.Additive);
|
|
loadedScenes.Add(SceneManager.GetSceneByName(sceneName).name);
|
|
if (audioSources.Count != 0)
|
|
{
|
|
foreach (AudioSource audioSource in audioSources)
|
|
{
|
|
if (!(audioSource == null))
|
|
{
|
|
LSS_AudioSource lSS_AudioSource = audioSource.gameObject.AddComponent<LSS_AudioSource>();
|
|
lSS_AudioSource.audioSource = audioSource;
|
|
lSS_AudioSource.audioFadeDuration = audioFadeDuration;
|
|
lSS_AudioSource.DoFadeOut();
|
|
}
|
|
}
|
|
}
|
|
onLoadingStart.Invoke();
|
|
}
|
|
|
|
public void UnloadAdditiveScene(string sceneName)
|
|
{
|
|
SceneManager.UnloadSceneAsync(sceneName);
|
|
loadedScenes.Remove(sceneName);
|
|
}
|
|
|
|
public void UnloadActiveAdditiveScenes()
|
|
{
|
|
foreach (string loadedScene in loadedScenes)
|
|
{
|
|
SceneManager.UnloadSceneAsync(loadedScene);
|
|
}
|
|
loadedScenes.Clear();
|
|
}
|
|
|
|
public void SetSingle()
|
|
{
|
|
loadingMode = LoadingMode.Single;
|
|
}
|
|
|
|
public void SetAdditive()
|
|
{
|
|
loadingMode = LoadingMode.Additive;
|
|
}
|
|
|
|
private void DoTriggerActions()
|
|
{
|
|
LSS_LoadingScreen.presetName = presetName;
|
|
if (loadingMode == LoadingMode.Single)
|
|
{
|
|
LSS_LoadingScreen.LoadScene(sceneName);
|
|
}
|
|
if (loadingMode == LoadingMode.Additive)
|
|
{
|
|
LSS_LoadingScreen.LoadSceneAdditive(sceneName);
|
|
}
|
|
}
|
|
|
|
private void OnTriggerEnter(Collider other)
|
|
{
|
|
if (enableTrigger && !onTriggerExit && (!loadWithTag || !(other.gameObject.tag != objectTag)))
|
|
{
|
|
DoTriggerActions();
|
|
}
|
|
}
|
|
|
|
private void OnTriggerEnter2D(Collider2D other)
|
|
{
|
|
if (enableTrigger && !onTriggerExit && (!loadWithTag || !(other.gameObject.tag != objectTag)))
|
|
{
|
|
DoTriggerActions();
|
|
}
|
|
}
|
|
|
|
private void OnTriggerExit(Collider other)
|
|
{
|
|
if (enableTrigger && onTriggerExit && (!loadWithTag || !(other.gameObject.tag != objectTag)))
|
|
{
|
|
DoTriggerActions();
|
|
}
|
|
}
|
|
|
|
private void OnTriggerExit2D(Collider2D other)
|
|
{
|
|
if (enableTrigger && onTriggerExit && (!loadWithTag || !(other.gameObject.tag != objectTag)))
|
|
{
|
|
DoTriggerActions();
|
|
}
|
|
}
|
|
}
|
|
}
|