当前位置:网站首页>音乐播放(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、完成。
边栏推荐
- Force buckle 1189 Maximum number of "balloons"
- [Offer18]删除链表的节点
- (1) Introduction Guide to R language - the first step of data analysis
- Classification, understanding and application of common methods of JS array
- 基于Redis的分布式ID生成器
- Lock wait timeout exceeded try restarting transaction
- Unity3d, Alibaba cloud server, platform configuration
- FairyGUI简单背包的制作
- Symbolic representation of functions in deep learning papers
- HCIP Day 12
猜你喜欢
Lock wait timeout exceeded try restarting transaction
2021.11.10汇编考试
Fabrication of fairygui simple Backpack
Unity3d, Alibaba cloud server, platform configuration
Basic operations of databases and tables ----- classification of data
Unity3D,阿里云服务器,平台配置
(三)R语言的生物信息学入门——Function, data.frame, 简单DNA读取与分析
Fabrication d'un sac à dos simple fairygui
PR 2021 quick start tutorial, first understanding the Premiere Pro working interface
Gravure sans fil Bluetooth sur micro - ordinateur à puce unique
随机推荐
Redis cache update strategy, cache penetration, avalanche, breakdown problems
Page performance optimization of video scene
1041 be unique (20 points (s)) (hash: find the first number that occurs once)
Acwing-116 pilot brother
编译原理:源程序的预处理及词法分析程序的设计与实现(含代码)
[Clickhouse kernel principle graphic explanation] about the collaborative work of partitioning, indexing, marking and compressed data
It has been solved by personal practice: MySQL row size too large (> 8126) Changing some columns to TEXT or BLOB or using ROW_ FORMAT
FairyGUI增益BUFF数值改变的显示
[offer78]合并多个有序链表
SSD technical features
VLSM variable length subnet mask partition tips
Unity3D基础入门之粒子系统(属性介绍+火焰粒子系统案例制作)
Redis based distributed ID generator
FairyGUI增益BUFF數值改變的顯示
JUC forkjoin and completable future
Basic operations of databases and tables ----- classification of data
(课设第一套)1-5 317号子任务 (100 分)(Dijkstra:重边自环)
关于Gateway中使用@Controller的问题
(3) Introduction to bioinformatics of R language - function, data Frame, simple DNA reading and analysis
Basic operations of databases and tables ----- creating data tables