当前位置:网站首页>Music playback (toggle & playerprefs)
Music playback (toggle & playerprefs)
2022-07-06 12:57:00 【SQ Liu】
Toggle Control music playback , And make use of PlayerPrefs Save the record when last checked
1、 New project .
2、 Drag in the prepared music ( As long as it's music ).
3、 Add an object randomly ( Here I am. Cube For example ), To add it Audio Source Components , stay AudioClip Drag music material into , Check on Play On Awake and Loop, As shown in the figure below .
4、 add to UI | Panel, And in Panel Add... Below Toggle Radio buttons , As shown in the figure below .
5、 add to Music Script .
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()
{
// By judging whether the radio box is checked , To decide whether to play background music
if (musicToggle.isOn == false)
{
musicAudio.enabled = false;
// Save the state of the music switch ,0 On behalf of closed ,1 Representative opens
PlayerPrefs.SetInt("MusicOn", 0);
}
else
{
musicAudio.enabled = true;
PlayerPrefs.SetInt("MusicOn", 1);
}
PlayerPrefs.Save(); // Save the record when last checked
}
}
6、 go back to Unity3D, Drag in Music Toggle and Music Audio.
7、 Register events for switches .
8、 complete .
边栏推荐
猜你喜欢

In 2020, the average salary of IT industry exceeded 170000, ranking first

PR 2021 quick start tutorial, first understanding the Premiere Pro working interface

堆排序【手写小根堆】

记录:初次cmd启动MySQL拒接访问之解决
![[算法] 剑指offer2 golang 面试题6:排序数组中的两个数字之和](/img/d5/4bda133498f71ae9fd7a64c6cba8f0.png)
[算法] 剑指offer2 golang 面试题6:排序数组中的两个数字之和

Particle system for introduction to unity3d Foundation (attribute introduction + case production of flame particle system)

Lock wait timeout exceeded try restarting transaction
![[algorithm] sword finger offer2 golang interview question 9: subarray with product less than k](/img/65/fc3fb5a217a3b44f506b695af53e2c.png)
[algorithm] sword finger offer2 golang interview question 9: subarray with product less than k
![[算法] 剑指offer2 golang 面试题2:二进制加法](/img/c2/6f6c3bd4d70252ba73addad6a3a9c1.png)
[算法] 剑指offer2 golang 面试题2:二进制加法

服务未正常关闭导致端口被占用
随机推荐
[algorithm] sword finger offer2 golang interview question 6: sum of two numbers in the sorting array
wsl常用命令
FairyGUI摇杆
3月15号 Go 1.18 正式版发布 了解最新特色以及使用方法
MySQL error warning: a long semaphore wait
What are the advantages of using SQL in Excel VBA
PRIDE-PPPAR源码解析
PR 2021 quick start tutorial, first understanding the Premiere Pro working interface
Unity3D制作注册登录界面,并实现场景跳转
WSL common commands
Wechat applet development experience
FGUI工程打包发布&导入Unity&将UI显示出来的方式
Naive Bayesian theory derivation
Solution to the problem of automatic login in Yanshan University Campus Network
Particle system for introduction to unity3d Foundation (attribute introduction + case production of flame particle system)
Problems and solutions of robust estimation in rtklib single point location spp
[algorithm] sword finger offer2 golang interview question 4: numbers that appear only once
RTKLIB: demo5 b34f. 1 vs b33
异常:IOException:Stream Closed
Code example of MATLAB reading GNSS observation value o file