当前位置:网站首页>音乐播放(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、完成。
边栏推荐
- ESP8266连接onenet(旧版MQTT方式)
- Naive Bayesian theory derivation
- MySQL performance tuning - dirty page refresh
- (课设第一套)1-4 消息传递接口 (100 分)(模拟:线程)
- Detailed explanation of truncate usage
- [leetcode622]设计循环队列
- Mysql database reports an error: row size too large (> 8126) Changing some columns to TEXT or BLOB or using ROW_ FORMAT=DY
- idea问题记录
- (the first set of course design) sub task 1-5 317 (100 points) (dijkstra: heavy edge self loop)
- 1081 rational sum (20 points) points add up to total points
猜你喜欢

(core focus of software engineering review) Chapter V detailed design exercises

Unity3d, Alibaba cloud server, platform configuration

Redis based distributed ID generator

Pat 1097 duplication on a linked list (25 points)
![[Clickhouse kernel principle graphic explanation] about the collaborative work of partitioning, indexing, marking and compressed data](/img/28/221b0a51ef5f2e8ed5aeca2de8f463.jpg)
[Clickhouse kernel principle graphic explanation] about the collaborative work of partitioning, indexing, marking and compressed data

Affichage du changement de valeur du Buff de gain de l'interface graphique de défaillance

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

JS function promotion and declaration promotion of VaR variable

Page performance optimization of video scene

(五)R语言入门生物信息学——ORF和序列分析
随机推荐
Lock wait timeout exceeded try restarting transaction
Special palindromes of daily practice of Blue Bridge Cup
Whistle+switchyomega configure web proxy
Teach you to release a DeNO module hand in hand
level16
[Red Treasure Book Notes simplified version] Chapter 12 BOM
Types de variables JS et transformations de type communes
341. Flatten nested list iterator
idea中好用的快捷键
Derivation of logistic regression theory
[leetcode622] design circular queue
Fairygui joystick
[leetcode15] sum of three numbers
Unity scene jump and exit
Conditional probability
Single chip Bluetooth wireless burning
By v$rman_ backup_ job_ Oracle "bug" caused by details
(4) Data visualization of R language -- matrix chart, histogram, pie chart, scatter chart, linear regression and strip chart
Easy to use shortcut keys in idea
[offer29] sorted circular linked list