当前位置:网站首页>音乐播放(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、完成。
边栏推荐
- (3) Introduction to bioinformatics of R language - function, data Frame, simple DNA reading and analysis
- (一)R语言入门指南——数据分析的第一步
- JUC forkjoin and completable future
- 如何给Arduino项目添加音乐播放功能
- 341. Flatten nested list iterator
- [Nodejs] 20. Koa2 onion ring model ----- code demonstration
- Vulnhub target: hacknos_ PLAYER V1.1
- Programming homework: educational administration management system (C language)
- JS regular expression basic knowledge learning
- [Leetcode15]三数之和
猜你喜欢
[Red Treasure Book Notes simplified version] Chapter 12 BOM
Fabrication of fairygui simple Backpack
FairyGUI摇杆
Naive Bayesian theory derivation
[golang] leetcode intermediate - fill in the next right node pointer of each node & the k-smallest element in the binary search tree
Vulnhub target: hacknos_ PLAYER V1.1
JS variable types and common type conversions
Pat 1097 duplication on a linked list (25 points)
Database course design: college educational administration management system (including code)
Single chip Bluetooth wireless burning
随机推荐
Minio file download problem - inputstream:closed
Detailed explanation of truncate usage
(3) Introduction to bioinformatics of R language - function, data Frame, simple DNA reading and analysis
Servlet
Générateur d'identification distribué basé sur redis
Mysql database reports an error: row size too large (> 8126) Changing some columns to TEXT or BLOB or using ROW_ FORMAT=DY
2021.11.10汇编考试
Redis based distributed ID generator
CUDA C programming authoritative guide Grossman Chapter 4 global memory
Conditional probability
Page performance optimization of video scene
Design and implementation of general interface open platform - (39) simple and crude implementation of API services
Pat 1097 duplication on a linked list (25 points)
Special palindromes of daily practice of Blue Bridge Cup
C programming exercise
Unity3D,阿里云服务器,平台配置
Fabrication of fairygui simple Backpack
Redis cache update strategy, cache penetration, avalanche, breakdown problems
JS variable types and common type conversions
(一)R语言入门指南——数据分析的第一步