Skip to content

Instantly share code, notes, and snippets.

@IshidaGames
Created November 29, 2019 13:08
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 IshidaGames/11453c74d337f0307849af5ea3c3eef0 to your computer and use it in GitHub Desktop.
Save IshidaGames/11453c74d337f0307849af5ea3c3eef0 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//TextなどのUIを使うのに必要
using UnityEngine.UI;
//オブジェクトにAudioSourceコンポーネントを追加
[RequireComponent(typeof(AudioSource))]
public class BGM : MonoBehaviour
{
//AudioSourceコンポーネントを入れる変数
AudioSource audioSource;
//右クリックでオンオフ切り替えるための変数
public bool pause = true;
//音量をInspector上で表示するための変数
public float scroll = 0;
//Inspector上から「BGM中断」のTextのUIを入れる
public Text text;
void Start()
{
//AudioSourceコンポーネントを取得
audioSource = GetComponent<AudioSource>();
//曲を流す
audioSource.Play();
//曲をループさせる
audioSource.loop = true;
//曲の音量、0~1まで
audioSource.volume = 0.5f;
//このオブジェクトがシーン遷移しても
//破棄されないようにする
DontDestroyOnLoad(this.gameObject);
//Textを非表示にする
text.enabled = false;
}
void Update()
{
//右クリックで起動
if (Input.GetMouseButtonDown(1))
{
//右クリックごとにBool変数が切り替わる
if (pause) {
//曲を一時中断する
audioSource.Pause();
pause = false;
//Textを表示する
text.enabled = true;
}
else
{
//曲を再開する
audioSource.UnPause();
pause = true;
//Textを非表示にする
text.enabled = false;
}
}
//音量にマウススクロールの値を加える
audioSource.volume += Input.GetAxis("Mouse ScrollWheel");
//Inspector上に音量を表示する
scroll = audioSource.volume;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment