当前位置:网站首页>音乐播放(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、完成。
边栏推荐
- Game 280 weekly
- dosbox第一次使用
- JS Title: input array, exchange the largest with the first element, exchange the smallest with the last element, and output array.
- idea中好用的快捷键
- 编译原理:源程序的预处理及词法分析程序的设计与实现(含代码)
- 1041 be unique (20 points (s)) (hash: find the first number that occurs once)
- (5) Introduction to R language bioinformatics -- ORF and sequence analysis
- How to add music playback function to Arduino project
- There is no red exclamation mark after SVN update
- Postman 中级使用教程【环境变量、测试脚本、断言、接口文档等】
猜你喜欢

Combination of fairygui check box and progress bar

FairyGUI按钮动效的混用

Compilation principle: preprocessing of source program and design and implementation of lexical analysis program (including code)

Stm32f1+bc20+mqtt+freertos system is connected to Alibaba cloud to transmit temperature and humidity and control LED lights

單片機藍牙無線燒錄

Pat 1097 duplication on a linked list (25 points)

MySQL takes up too much memory solution

程序设计大作业:教务管理系统(C语言)

JS variable types and common type conversions

dosbox第一次使用
随机推荐
Fabrication d'un sac à dos simple fairygui
Unity3D摄像机,键盘控制前后左右上下移动,鼠标控制旋转、放缩
FairyGUI按钮动效的混用
Intermediate use tutorial of postman [environment variables, test scripts, assertions, interface documents, etc.]
[Offer18]删除链表的节点
Game 280 weekly
Introduction to the daily practice column of the Blue Bridge Cup
About using @controller in gateway
Get the position of the nth occurrence of the string
1041 Be Unique (20 point(s))(哈希:找第一个出现一次的数)
ESP8266连接onenet(旧版MQTT方式)
Talking about the startup of Oracle Database
(课设第一套)1-4 消息传递接口 (100 分)(模拟:线程)
Remember an experience of ECS being blown up by passwords - closing a small black house, changing passwords, and changing ports
Acwing-116 pilot brother
JS function promotion and declaration promotion of VaR variable
Pytorch: tensor operation (I) contiguous
Esp8266 connect onenet (old mqtt mode)
In 2020, the average salary of IT industry exceeded 170000, ranking first
MySQL时间、时区、自动填充0的问题