当前位置:网站首页>Music playback (toggle & playerprefs)
Music playback (toggle & playerprefs)
2022-07-06 12:57:00 【SQ Liu】
Toggle Control music playback , And make use of PlayerPrefs Save the record when last checked
1、 New project .
2、 Drag in the prepared music ( As long as it's music ).
3、 Add an object randomly ( Here I am. Cube For example ), To add it Audio Source Components , stay AudioClip Drag music material into , Check on Play On Awake and Loop, As shown in the figure below .
4、 add to UI | Panel, And in Panel Add... Below Toggle Radio buttons , As shown in the figure below .
5、 add to Music Script .
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Music : MonoBehaviour
{
public Toggle musicToggle;
public AudioSource musicAudio;
private void Awake()
{
if (PlayerPrefs.HasKey("MusicOn"))
{
if (PlayerPrefs.GetInt("MusicOn") == 1)
{
musicToggle.isOn = true;
musicAudio.enabled = true;
}
else
{
musicToggle.isOn = false;
musicAudio.enabled = false;
}
}
else
{
musicToggle.isOn = true;
musicAudio.enabled = true;
}
}
public void MusicSwitch()
{
// By judging whether the radio box is checked , To decide whether to play background music
if (musicToggle.isOn == false)
{
musicAudio.enabled = false;
// Save the state of the music switch ,0 On behalf of closed ,1 Representative opens
PlayerPrefs.SetInt("MusicOn", 0);
}
else
{
musicAudio.enabled = true;
PlayerPrefs.SetInt("MusicOn", 1);
}
PlayerPrefs.Save(); // Save the record when last checked
}
}
6、 go back to Unity3D, Drag in Music Toggle and Music Audio.
7、 Register events for switches .
8、 complete .
边栏推荐
- The service robots that have been hyped by capital and the Winter Olympics are not just a flash in the pan
- FairyGUI人物状态弹窗
- 音乐播放(Toggle && PlayerPrefs)
- FairyGUI增益BUFF數值改變的顯示
- KF UD分解之UD分解基础篇【1】
- FairyGUI复选框与进度条的组合使用
- Unity scene jump and exit
- MySQL error warning: a long semaphore wait
- 最短Hamilton路径 (状压DP)
- [算法] 剑指offer2 golang 面试题10:和为k的子数组
猜你喜欢
随机推荐
Unity3d, Alibaba cloud server, platform configuration
Fabrication d'un sac à dos simple fairygui
GNSS positioning accuracy index calculation
Mixed use of fairygui button dynamics
(the first set of course design) 1-4 message passing interface (100 points) (simulation: thread)
Usage differences between isempty and isblank
Introduction to the daily practice column of the Blue Bridge Cup
Game 280 weekly
FairyGUI条子家族(滚动条,滑动条,进度条)
Lean product development - Lean Software Development & lean product development
[rtklib 2.4.3 B34] version update introduction I
KF UD decomposition pseudo code implementation advanced [2]
编辑距离(多源BFS)
Fairygui loop list
FairyGUI复选框与进度条的组合使用
Knowledge system of digital IT practitioners | software development methods -- agile
记录:初次cmd启动MySQL拒接访问之解决
服务未正常关闭导致端口被占用
MySQL shutdown is slow
Rt-ppp test using rtknavi