当前位置:网站首页>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 .
边栏推荐
- [算法] 剑指offer2 golang 面试题9:乘积小于k的子数组
- Comparative analysis of the execution efficiency of MySQL 5.7 statistical table records
- 基于rtklib源码进行片上移植的思路分享
- [算法] 剑指offer2 golang 面试题4:只出现一次的数字
- 记录:Navicat Premium初次无法连接数据库MySQL之解决
- Problems and solutions of robust estimation in rtklib single point location spp
- [algorithm] sword finger offer2 golang interview question 1: integer division
- How to reduce the shutdown time of InnoDB database?
- Fairygui gain buff value change display
- [dry goods] cycle slip detection of suggestions to improve the fixed rate of RTK ambiguity
猜你喜欢
[算法] 剑指offer2 golang 面试题10:和为k的子数组
[算法] 剑指offer2 golang 面试题6:排序数组中的两个数字之和
[算法] 剑指offer2 golang 面试题2:二进制加法
In 2020, the average salary of IT industry exceeded 170000, ranking first
Liste des boucles de l'interface graphique de défaillance
Office prompts that your license is not genuine pop-up box solution
Code example of MATLAB reading GNSS observation value o file
FairyGUI循环列表
Basic DOS commands
[算法] 剑指offer2 golang 面试题5:单词长度的最大乘积
随机推荐
Solution to the problem of automatic login in Yanshan University Campus Network
Combination of fairygui check box and progress bar
[rtklib] preliminary practice of using robust adaptive Kalman filter under RTK
基本Dos命令
Basic DOS commands
记录:Navicat Premium初次无法连接数据库MySQL之解决
Theoretical derivation of support vector machine
[algorithm] sword finger offer2 golang interview question 5: maximum product of word length
雇佣收银员【差分约束】
It has been solved by personal practice: MySQL row size too large (> 8126) Changing some columns to TEXT or BLOB or using ROW_ FORMAT
Sharing ideas of on-chip transplantation based on rtklib source code
Excel导入,导出功能实现
第一人称视角的角色移动
[算法] 剑指offer2 golang 面试题4:只出现一次的数字
[algorithm] sword finger offer2 golang interview question 2: binary addition
[algorithm] sword finger offer2 golang interview question 4: numbers that appear only once
Unity3D基础入门之粒子系统(属性介绍+火焰粒子系统案例制作)
微信小程序开发心得
Office prompts that your license is not genuine pop-up box solution
【GNSS】抗差估计(稳健估计)原理及程序实现