Skip to content

Instantly share code, notes, and snippets.

@urahimono
Created April 6, 2017 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save urahimono/2bdd35d98591cea6fa40a1c9759071cc to your computer and use it in GitHub Desktop.
Save urahimono/2bdd35d98591cea6fa40a1c9759071cc to your computer and use it in GitHub Desktop.
GGJ2017で使ったシーン管理システム
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
public class SceneController : MonoBehaviour
{
private AudioSource m_bgmAudio = null;
private FadeController m_fadeController = null;
private string m_beingChangedSceneName = null;
public static SceneController Instance
{
get;
private set;
}
private void Awake()
{
if( Instance != null )
{
Destroy( this );
return;
}
Instance = this;
m_fadeController = gameObject.AddComponent<FadeController>();
m_fadeController.transform.SetParent( transform, false );
m_bgmAudio = gameObject.AddComponent<AudioSource>();
m_bgmAudio.loop = true;
gameObject.AddComponent<AudioListener>();
}
public void ChangeScene( EScene i_scene )
{
var sceneData = SceneTable.Instance.GetSceneData( i_scene );
ChangeScene( sceneData.m_name );
}
public void ChangeScene( string i_sceneName )
{
Debug.AssertFormat( !string.IsNullOrEmpty( i_sceneName ), "Invalid i_sceneName!" );
if( !string.IsNullOrEmpty( m_beingChangedSceneName ) )
{
Debug.LogWarningFormat( "It is currently being changed scene to {0}. failed scene = {1}", m_beingChangedSceneName, i_sceneName );
return;
}
StartCoroutine( ChangeSceneProcess( i_sceneName ) );
}
private IEnumerator ChangeSceneProcess( string i_sceneName )
{
m_beingChangedSceneName = i_sceneName;
{
m_fadeController.FadeOut();
yield return new WaitWhile( () => m_fadeController.State == FadeController.EFadeState.Process );
}
StopBGM();
SceneManager.LoadScene( i_sceneName );
PlayBGM( i_sceneName );
{
m_fadeController.FadeOut();
yield return new WaitWhile( () => m_fadeController.State == FadeController.EFadeState.Process );
}
m_beingChangedSceneName = null;
}
private void PlayBGM( string i_scene )
{
var audio = SceneTable.Instance.GetSceneData( i_scene ).m_bgm;
if( audio != null )
{
m_bgmAudio.clip = audio;
m_bgmAudio.Play();
}
else
{
StopBGM();
}
}
private void StopBGM()
{
m_bgmAudio.Stop();
m_bgmAudio.clip = null;
}
} // class SceneController
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment