当前位置:网站首页>音乐播放(Toggle && PlayerPrefs)
音乐播放(Toggle && PlayerPrefs)
2022-07-06 09:18:00 【SQ刘】
Toggle控制音乐播放,并利用PlayerPrefs保存上次是否勾选时的记录
1、新建工程。
2、拖入事先准备好的音乐(只要是音乐都行)。
3、随便添加一个物体(我这里以Cube为例),给它添加Audio Source组件,在AudioClip中拖入音乐素材,勾选上Play On Awake和Loop,如下图所示。
4、添加UI | Panel,并在Panel下面添加Toggle单选框,如下图所示。
5、添加Music脚本。
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()
{
//通过判断单选框是否被勾选上,从而来决定是否播放背景音乐
if (musicToggle.isOn == false)
{
musicAudio.enabled = false;
//保存音乐开关的状态,0代表关闭,1代表开启
PlayerPrefs.SetInt("MusicOn", 0);
}
else
{
musicAudio.enabled = true;
PlayerPrefs.SetInt("MusicOn", 1);
}
PlayerPrefs.Save(); //保存上次是否勾选时的记录
}
}
6、回到Unity3D,拖入Music Toggle 和 Music Audio。
7、给开关注册事件。
8、完成。
边栏推荐
- SSD technical features
- Get the position of the nth occurrence of the string
- Postman 中级使用教程【环境变量、测试脚本、断言、接口文档等】
- Conditional probability
- Game 280 weekly
- Page performance optimization of video scene
- dosbox第一次使用
- Fairygui joystick
- NRF24L01 troubleshooting
- Redis based distributed locks and ultra detailed improvement ideas
猜你喜欢

idea问题记录

Lock wait timeout exceeded try restarting transaction

Unity3d, Alibaba cloud server, platform configuration

(四)R语言的数据可视化——矩阵图、柱状图、饼图、散点图与线性回归、带状图

Single chip Bluetooth wireless burning

Esp8266 connect onenet (old mqtt mode)

Unity3d makes the registration login interface and realizes the scene jump

Fairygui gain buff value change display

level16

记一次云服务器被密码爆破的经历——关小黑屋、改密码、改端口
随机推荐
Combination of fairygui check box and progress bar
Mysql database index
Unity3d, Alibaba cloud server, platform configuration
First use of dosbox
Pat 1097 duplication on a linked list (25 points)
Gravure sans fil Bluetooth sur micro - ordinateur à puce unique
1041 be unique (20 points (s)) (hash: find the first number that occurs once)
1081 rational sum (20 points) points add up to total points
2022.2.12 resumption
数据库课程设计:高校教务管理系统(含代码)
Office提示您的许可证不是正版弹框解决
Postman 中级使用教程【环境变量、测试脚本、断言、接口文档等】
(1) Introduction Guide to R language - the first step of data analysis
Learning notes of JS variable scope and function
(课设第一套)1-4 消息传递接口 (100 分)(模拟:线程)
[Offer18]删除链表的节点
JS function promotion and declaration promotion of VaR variable
Symbolic representation of functions in deep learning papers
Unity scene jump and exit
[offer78]合并多个有序链表