当前位置:网站首页>音乐播放(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、完成。
边栏推荐
- Custom view puzzle getcolor r.color The color obtained by colorprimary is incorrect
- Get the position of the nth occurrence of the string
- Database table splitting strategy
- MySQL performance tuning - dirty page refresh
- JUC forkjoin and completable future
- [899] ordered queue
- Meanings and differences of PV, UV, IP, VV, CV
- [offer78] merge multiple ordered linked lists
- idea中导包方法
- (1) Introduction Guide to R language - the first step of data analysis
猜你喜欢
FairyGUI按钮动效的混用
Particle system for introduction to unity3d Foundation (attribute introduction + case production of flame particle system)
FairyGUI简单背包的制作
Redis 缓存更新策略,缓存穿透、雪崩、击穿问题
Types de variables JS et transformations de type communes
ESP8266连接onenet(旧版MQTT方式)
编译原理:源程序的预处理及词法分析程序的设计与实现(含代码)
單片機藍牙無線燒錄
程序设计大作业:教务管理系统(C语言)
Whistle+switchyomega configure web proxy
随机推荐
2021.11.10 compilation examination
Gateway fails to route according to the service name, and reports an error service unavailable, status=503
First use of dosbox
Easy to use shortcut keys in idea
Latex learning
Redis based distributed ID generator
燕山大学校园网自动登录问题解决方案
(core focus of software engineering review) Chapter V detailed design exercises
Générateur d'identification distribué basé sur redis
Symbolic representation of functions in deep learning papers
Database course design: college educational administration management system (including code)
1041 Be Unique (20 point(s))(哈希:找第一个出现一次的数)
Acwing-116 pilot brother
Stm32f1+bc20+mqtt+freertos system is connected to Alibaba cloud to transmit temperature and humidity and control LED lights
Basic operations of databases and tables ----- creating data tables
Minio file download problem - inputstream:closed
Whistle+switchyomega configure web proxy
Pat 1097 duplication on a linked list (25 points)
Affichage du changement de valeur du Buff de gain de l'interface graphique de défaillance
Learning notes of JS variable scope and function