Now I'm thinking about loading scenes, optimizing it, and making sure there's a gap in between...
Right now I tell the thing to fade out, and after it fades out entirely, wait for a pre-determined gap (0.25s) before letting it actually load. No gap = sometimes, when the game loads too fast, it looks jarring. There's no "expected" blackness, so the gap is intentionally introduced.
Is there a way so that it makes it so that:
1. The game loads the next scene as soon as the scene starts to fade out, instead of loading 0.25s after the scene is totally black.
2a. Let the gap between fade out and fade in if the next scene loads before the gap time is passed; after this time has passed, immediately load the scene
2b. If the next scene isn't loaded by then, continue displaying the gap until it's done, or throw up a "now loading"
*. Maybe I should spin out the fader functions so that they can be invoked to do whatever, to any colour/volume, for more flexibility in cutscene/battle use.
Just a bit curious on optimizing loading.
Code:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Collections;
public class SceneLoader : MonoBehaviour {
public LoaderManager manager;
public PlayerManager playerManager;
public AudioManager audioManager;
public FieldUIManager fieldUIManager;
bool fadeGraphicsFinished = false;
bool fadeAudioFinished = false;
string sceneToLoad = "WorldMap";
float nextSceneX = 0f;
float nextSceneY = 0f;
public float fadeDuration = 0.25f;
public float fadeGap = 0.25f;
// Shortcuts
public Image faderImage;
public CanvasGroup faderCanvas;
public PlayerScript ps;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (playerManager == null || audioManager == null || fieldUIManager == null)
{
// Something isn't loaded yet
if (manager == null)
{
manager = LoaderManager.Manager;
manager.LoaderExists();
}
else if (playerManager == null)
{
playerManager = PlayerManager.Manager;
ps = playerManager.player.GetComponent<PlayerScript>();
}
else if (audioManager == null)
{
audioManager = AudioManager.Manager;
}
else if (fieldUIManager == null)
{
fieldUIManager = FieldUIManager.Manager;
faderImage = fieldUIManager.ui.GetComponent<FieldUIScript>().Fader.GetComponent<Image>();
faderCanvas = fieldUIManager.ui.GetComponent<FieldUIScript>().Fader.GetComponent<CanvasGroup>();
}
}
else
{
}
}
public void PrepareLoadScene(string sceneName, float x, float y, Color c, bool SkipAudioFade = false)
{
sceneToLoad = sceneName;
nextSceneX = x;
nextSceneY = y;
faderImage.color = c;
if (!SkipAudioFade)
{
StartCoroutine(FadeAudio());
audioManager.keepPlayingThroughSceneLoad = false;
}
else
{
audioManager.keepPlayingThroughSceneLoad = true;
}
StartCoroutine(FadeDisplayOut());
StartCoroutine(CheckIfReady());
}
IEnumerator LoadScene()
{
float time = 0;
playerManager.player.transform.position = new Vector3(nextSceneX, nextSceneY, 0f);
for (; time < fadeGap; time += Time.deltaTime)
{
yield return null;
}
fadeGraphicsFinished = false;
fadeAudioFinished = false;
SceneManager.LoadScene(sceneToLoad);
}
IEnumerator FadeAudio()
{
float time = 0;
for (; time < fadeDuration + fadeGap; time += Time.deltaTime)
{
AudioListener.volume = ps.Data.Settings.masterVol - (time / (fadeDuration + fadeGap)) * ps.Data.Settings.masterVol;
yield return null;// new WaitForSeconds(0.75f);
}
audioManager.Stop();
fadeAudioFinished = true;
}
IEnumerator FadeDisplayIn()
{
float time = 0;
for (; time < fadeDuration; time += Time.deltaTime)
{
faderCanvas.alpha = 1 - time / fadeDuration;
yield return null;// new WaitForSeconds(0.75f);
}
}
IEnumerator FadeDisplayOut()
{
float time = 0;
for (; time < fadeDuration; time += Time.deltaTime)
{
faderCanvas.alpha = time/fadeDuration;
yield return null;// new WaitForSeconds(0.75f);
}
fadeGraphicsFinished = true;
}
IEnumerator CheckIfReady()
{
while (!fadeAudioFinished || !fadeGraphicsFinished)
{
yield return null;
}
StartCoroutine(LoadScene());
}
void OnLevelWasLoaded()
{
StartCoroutine(FadeDisplayIn());
AudioListener.volume = ps.Data.Settings.masterVol;
}
}
As for NPC and object interaction, I suppose it's just a matter of ray-casting from the player to nearby objects in front on detection of the "Submit" button being pressed, no?
Still working on polishing the UI. Gave it distinct headers, complete with their own colours. Also it doesn't have broken keyboard controls now! Now to make sure it can, you know, actually start invoking keyboard focus when I toggle the menu in with Escape/controller X... right now no part of the UI has focus when you press Escape/button X, and the keyboard/controller input only works when you have moused over a UI element at least once. (And it starts interfering with normal controls once the system menu is closed, so also need a way to defocus it.)