Audio MixerとSliderの連携の仕組みを完成させたが、Sliderを手動で操作できないように設定しているので、
今日はSliderを操作するボタンを作り込んでいく。
Audio MixerとSliderが連携しているので、Slider.valueを変えればボリュームが変わる。
なので、こんな感じのメソッドを作って、各ボタンにそれぞれ割り当ててやれば良いはず。
public void IncreaseBGMVolume() //BGMボリューム増 { BGMSlider.value+=1; } public void ReduceBGMVolume() //BGMボリューム減 { BGMSlider.value-=1; } public void IncreaseSEVolume() //SEボリューム増 { SESlider.value+=1; } public void ReduceSEVolume() //SEボリューム減 { SESlider.value-=1; }
これで完成としても良いけれど、このゲームはキーボードやゲームパッドでも操作できる様にしているので、
次は、ボタンを選択しているカーソルを作ってやり、キーボードやゲームパッドでカーソルの移動と、
決定キー押下でカーソル位置の処理を行う様にしていきたい。
まずは、各ボタンの位置に、矢印の様なimageでカーソルを作り、そのカーソル群を配列変数に格納する。
そしてint変数、CusorIndexを用意して、カーソルの現在位置を格納してやれば、タイトル画面のメニュー選択の方法と同じ仕組みでいけるはず。
最終的なスクリプトは、こんな感じ。
using TMPro; using UnityEngine; using UnityEngine.Audio; using UnityEngine.UI; public class SettingMenu : MonoBehaviour { [SerializeField] AudioMixer audioMixer; [SerializeField] Slider BGMSlider; [SerializeField] Slider SESlider; [SerializeField] AudioSource BGMaudio; [SerializeField] AudioSource SEaudio; [SerializeField] Button button; //VT Controllerのボタンのimage変更用 [SerializeField] TMP_Text tMPtext; //VT ControllerのボタンのText変更用 [SerializeField] Sprite Onimage; //VT Controllerのボタンのimage変更用 [SerializeField] Sprite Offimage; //VT Controllerのボタンのimage変更用 float bgmVolume; float seVolume; [SerializeField] private GameObject[] targetCursor; //カーソル格納用の配列変数 [SerializeField] int CursorIndex=0; //カーソル位置を記録する変数 private InputSystemSC inputSystemSC; float inputTimer=0; //InputSystemの連打防止用のタイマー void Start() { inputSystemSC=new InputSystemSC(); inputSystemSC.Enable(); if(Parameters.instance.VtController==true) { button.image.sprite=Onimage; tMPtext.text="ON"; } else { button.image.sprite=Offimage; tMPtext.text="OFF"; } //AudioMixerのBGMボリュームをBGMスライダーに取り込み。 audioMixer.GetFloat("BGM", out float bgmVolume); BGMSlider.value = Mathf.Round(bgmVolume/10); //9段階になるように四捨五入 //AudioMixerのSEボリュームをSEスライダーに取り込み。 audioMixer.GetFloat("SE", out float seVolume); SESlider.value = Mathf.Round(seVolume / 10); //9段階になるように四捨五入 //スライダーを動かした時の処理を登録 BGMSlider.onValueChanged.AddListener(SetAudioMixerBGM); SESlider.onValueChanged.AddListener(SetAudioMixerSE); } private void Update() { inputTimer+=Time.deltaTime; //inputTimer2+=Time.deltaTime; if(inputTimer>0.25f) { MenuMove(); Menuaction(); } } void MenuMove() { Vector2 LeftDirction=inputSystemSC.SelectedMenu.Move.ReadValue<Vector2>(); var LeftDirction2 =LeftDirction.y; if(LeftDirction2<0) { SEaudio.PlayOneShot(AudioSO.Entity.MoveSE); CursorIndex++; inputTimer=0; } if(LeftDirction2>0) { SEaudio.PlayOneShot(AudioSO.Entity.MoveSE); CursorIndex--; inputTimer=0; } Debug.Log(CursorIndex); if(CursorIndex==targetCursor.Length) { CursorIndex=0; } if(CursorIndex==-1) { CursorIndex=targetCursor.Length-1; } for (int i = 0; i < targetCursor.Length; i++) { if(CursorIndex==i) { targetCursor[i].SetActive(true); } else { targetCursor[i].SetActive(false); } } } void Menuaction() { if(inputSystemSC.SelectedMenu.Action.triggered) { inputTimer=0; switch (CursorIndex) //カーソルの位置の処理を実行 { case 0: ReduceBGMVolume(); break; case 1: IncreaseBGMVolume(); break; case 2: ReduceSEVolume(); break; case 3: IncreaseSEVolume(); break; case 4: VtControllerSwitch(); break; case 5: Exit(); break; } } } public void SetAudioMixerBGM(float value) { BGMaudio.Stop(); var volume = value*10;//スライダーの値を10倍にして audioMixer.SetFloat("BGM", volume);//AudioMixerのBGMのボリュームに設定 BGMaudio.Play(); } //SE public void SetAudioMixerSE(float value) { var volume = value * 10;//スライダーの値を10倍にして audioMixer.SetFloat("SE", volume);//AudioMixerのSEのボリュームに設定 SEaudio.PlayOneShot(AudioSO.Entity.TestSE);//テストのため、効果音を鳴らす。 } public void IncreaseBGMVolume() { BGMSlider.value+=1; CursorIndex=1; //ボタンの位置にカーソルを移動 } public void ReduceBGMVolume() { BGMSlider.value-=1; CursorIndex=0; //ボタンの位置にカーソルを移動 } public void IncreaseSEVolume() { SESlider.value+=1; CursorIndex=3; //ボタンの位置にカーソルを移動 } public void ReduceSEVolume() { SESlider.value-=1; CursorIndex=2; //ボタンの位置にカーソルを移動 } public void VtControllerSwitch() { CursorIndex=4; //ボタンの位置にカーソルを移動 if(Parameters.instance.VtController!=true) { button.image.sprite=Onimage; //ボタンのimageを変更 tMPtext.text="ON"; //ボタンのテキストを変更 Parameters.instance.VtController=true; } else { button.image.sprite=Offimage; //ボタンのimageを変更 tMPtext.text="OFF"; //ボタンのテキストを変更 Parameters.instance.VtController=false; } } public void Exit() { CursorIndex=0; //ボタンの位置にカーソルを移動 this.gameObject.SetActive(false); } }
カーソルの位置で、そのボタンの処理するために、CursorIndexをSwitch〜caseで条件分岐させている。
各ボタンのメソッドにCursorIndex=*で、そのボタンをクリックで操作したときに、カーソルをボタンの位置に移動するようにしている。
概ねイメージしている通りの動きになったと思う。
さて、いよいよ次は操作説明の画面だな〜