当前位置:网站首页>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 .
边栏推荐
- [algorithm] sword finger offer2 golang interview question 5: maximum product of word length
- 平衡二叉树详解 通俗易懂
- Itext 7 生成PDF总结
- [算法] 剑指offer2 golang 面试题6:排序数组中的两个数字之和
- The master of double non planning left the real estate company and became a programmer with an annual salary of 25W. There are too many life choices at the age of 25
- [algorithm] sword finger offer2 golang interview question 6: sum of two numbers in the sorting array
- GNSS positioning accuracy index calculation
- FairyGUI摇杆
- Agile development helps me
- 染色法判定二分图
猜你喜欢
C programming exercise
Teach you to release a DeNO module hand in hand
Unity场景跳转及退出
Fairygui joystick
[algorithm] sword finger offer2 golang interview question 3: the number of 1 in the binary form of the first n numbers
[算法] 剑指offer2 golang 面试题12:左右两边子数组的和相等
Theoretical derivation of support vector machine
Code example of MATLAB reading GNSS observation value o file
Guided package method in idea
[dry goods] cycle slip detection of suggestions to improve the fixed rate of RTK ambiguity
随机推荐
使用rtknavi进行RT-PPP测试
Unity3d, Alibaba cloud server, platform configuration
[dry goods] cycle slip detection of suggestions to improve the fixed rate of RTK ambiguity
Fairygui joystick
Compile GDAL source code with nmake (win10, vs2022)
【RTKLIB 2.4.3 b34 】版本更新简介一
PRIDE-PPPAR源码解析
Liste des boucles de l'interface graphique de défaillance
微信小程序开发心得
[GNSS] robust estimation (robust estimation) principle and program implementation
Force buckle 1189 Maximum number of "balloons"
【rtklib】在rtk下使用抗差自适应卡尔曼滤波初步实践
编辑距离(多源BFS)
C code implementation of robust estimation in rtklib's pntpos function (standard single point positioning spp)
【干货】提升RTK模糊度固定率的建议之周跳探测
[rtklib] preliminary practice of using robust adaptive Kalman filter under RTK
[algorithm] sword finger offer2 golang interview question 3: the number of 1 in the binary form of the first n numbers
IText 7 generate PDF summary
地球围绕太阳转
Meanings and differences of PV, UV, IP, VV, CV