User Interface & Others

MainAudio.cs

attached to the main audio source object which remains through whole scenes

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class MainAudio : MonoBehaviour {
	private AudioSource audioSource;
	void Awake () {
		audioSource = this.GetComponent<AudioSource> ();
	}

	void Start(){
		DontDestroyOnLoad (this);
	}

	public void Sound(AudioClip audioClip){
		if (audioClip) {
			audioSource.clip = audioClip;
			audioSource.Play ();
		} else {
		}
	}
}

SoundEffects.cs

sounds audio clips received from other scripts

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SoundEffect : MonoBehaviour {

	private AudioSource audioSource;

	void Awake () {
		audioSource = this.GetComponent<AudioSource> ();
	}

	public void Sound(AudioClip audioClip){
		if (audioClip) {
			audioSource.clip = audioClip;
			audioSource.Play ();
		} else {
		}
	}
}

VideoEnds.cs

waits for the video ends or skipped and moves to next scene

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;

public class VideoEnds : MonoBehaviour {

	public string nextScene;
	private VideoPlayer video;
	private SceneTrans st;

	void Awake () {
		video = gameObject.GetComponent<VideoPlayer> ();
		st = GameObject.Find ("trans").GetComponent<SceneTrans> ();
		Debug.Log ("Press S to skip the scene.");
	}

	void Update (){
		if (Input.GetKeyDown (KeyCode.S)) {
			st.Fade (nextScene);
		}
	}

	void LateUpdate () {
		if (video.isPlaying == false) {
			st.Fade (nextScene);
		}
	}

}

SceneTrans

controls fading in/out, scene transitions and background music

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class SceneTrans : MonoBehaviour {
	
	public enum FadeIn
	{
		WHITE,
		BLACK,
		NONE
	};

	public FadeIn fadeOut;
	public FadeIn fadeIn;
	public AudioClip backgroundMusic;

	private Image trans;
	private float alpha, newalpha;
	private float red,green,blue;
	private bool fading;
	private string next;
	private bool load;

	private MainAudio ma;

	void Awake () {
		alpha = 1f;
		newalpha = -1f;
		trans = this.GetComponent<Image>();
		if (fadeOut == FadeIn.WHITE) {
			red = green = blue = 255f;
		} else if (fadeOut == FadeIn.BLACK) {
			red = green = blue = 0f;
		} else {
			red = green = blue = 0f;
			alpha = 0f;
		}
		trans.color = new Color (red, green, blue, alpha);
		ma = GameObject.Find ("MainAudioSource").GetComponent<MainAudio> ();
	}

	void Start(){
		fading = true;
	}

	public void Fade (string n){
		if (fadeIn == FadeIn.WHITE) {
			red = green = blue = 255f;
		} else if (fadeIn == FadeIn.BLACK) {
			red = green = blue = 0f;
		} else {
			newalpha = 0f;
		}
		next = n;
		trans.color = new Color (red,green,blue,alpha);
		fading = true;
	}

	public void Dead(){
		red = green = blue = 0f;
		next = "start";
		trans.color = new Color (red,green,blue,alpha);
		fading = true;
	}

	void FixedUpdate(){
		if (fading == true && newalpha == -1f) {
			alpha = alpha + newalpha * Time.deltaTime;
			var color = trans.color;
			color.a = alpha;
			trans.color = color;

			if (alpha <= 0f) {
				fading = false;
				if (backgroundMusic) {
					ma.Sound (backgroundMusic);
				}
				newalpha = 1f;
			}
		} else if (fading == true && newalpha == 1f) {
			alpha = alpha + newalpha * Time.deltaTime;
			var color = trans.color;
			color.a = alpha;
			trans.color = color;

			if (alpha >= 1f) {
				fading = false;
				SceneManager.LoadScene (next, LoadSceneMode.Single);
			}
		} else if (fading == true && newalpha == 0f) {
			fading = false;
			SceneManager.LoadScene (next, LoadSceneMode.Single);
		}
	}
}

Advertisements