当前位置:网站首页>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 .
边栏推荐
- Fabrication of fairygui simple Backpack
- [algorithm] sword finger offer2 golang interview question 5: maximum product of word length
- Implementation of Excel import and export functions
- [algorithm] sword finger offer2 golang interview question 1: integer division
- Sharing ideas of on-chip transplantation based on rtklib source code
- Wechat applet development experience
- Rt-ppp test using rtknavi
- [algorithm] sword finger offer2 golang interview question 2: binary addition
- [算法] 剑指offer2 golang 面试题9:乘积小于k的子数组
- (the first set of course design) sub task 1-5 317 (100 points) (dijkstra: heavy edge self loop)
猜你喜欢

Mysql database index

There is no red exclamation mark after SVN update

Database course design: college educational administration management system (including code)

What are the advantages of using SQL in Excel VBA

Easy to use shortcut keys in idea

RTKLIB: demo5 b34f. 1 vs b33

【GNSS数据处理】赫尔默特(helmert)方差分量估计解析及代码实现

FairyGUI增益BUFF數值改變的顯示
![Fundamentals of UD decomposition of KF UD decomposition [1]](/img/e9/564e0163c3756c0ba886913f1cfaef.jpg)
Fundamentals of UD decomposition of KF UD decomposition [1]

rtklib单点定位spp使用抗差估计遇到的问题及解决
随机推荐
FairyGUI按钮动效的混用
[算法] 剑指offer2 golang 面试题4:只出现一次的数字
[算法] 剑指offer2 golang 面试题1:整数除法
平衡二叉树详解 通俗易懂
IText 7 generate PDF summary
编辑距离(多源BFS)
FairyGUI条子家族(滚动条,滑动条,进度条)
[untitled]
Database table splitting strategy
FairyGUI复选框与进度条的组合使用
FairyGUI增益BUFF数值改变的显示
Derivation of logistic regression theory
[算法] 剑指offer2 golang 面试题10:和为k的子数组
服务未正常关闭导致端口被占用
[Yu Yue education] guide business reference materials of Wuxi Vocational and Technical College of Commerce
Naive Bayesian theory derivation
GPS高程拟合抗差中误差的求取代码实现
Agile development helps me
微信小程序开发心得
【干货】提升RTK模糊度固定率的建议之周跳探测