Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.