Created
November 29, 2019 13:08
-
-
Save IshidaGames/11453c74d337f0307849af5ea3c3eef0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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