unity——Prejct3——背景音乐

1.音乐数据结构类

cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//音乐数据结构类
public class MusicData 
{
    //背景音乐 音效 开关
    public bool musicOpen = true;
    public bool soundOpen = true;

    //背景音乐 音效 大小
    public float musicValue = 0.2f;
    public float soundValue = 0.2f;
}

2.数据管理类

cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// 数据管理类
public class GameDataMgr
{
    private static GameDataMgr instance = new GameDataMgr();

    public static GameDataMgr Instance => instance;

    //音乐 音效 相关数据
    public MusicData musicData;

    private GameDataMgr()
    {
        //初始化默认数据 如果第一次进入游戏 Json会直接默认数据
        musicData = JsonMgr.Instance.LoadData<MusicData>("MusicData");
    }

    /// <summary>
    /// 存储音乐 音效数据
    /// </summary>
    public void SaveMusicData()
    {
        JsonMgr.Instance.SaveData(musicData, "MusicData");
    }
}

3.真实背景音乐类

cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BKMusic : MonoBehaviour
{
    private static BKMusic instance;
    public static BKMusic Instance => instance;

    private AudioSource bkSource;

    // Start is called before the first frame update
    void Awake()
    {
        instance = this;
        bkSource = this.GetComponent<AudioSource>();

        //通过真正的音乐数据来设置背景音乐的数据
        MusicData data = JsonMgr.Instance.LoadData<MusicData>("MusicData");

        SetIsOpen(data.musicOpen);
        ChangeValue(data.musicValue);
    }

    //开关背景音乐
    public void SetIsOpen(bool isOpen)
    {
        bkSource.mute = !isOpen;
    }

    //调整背景音乐大小
    public void ChangeValue(float v)
    {
        bkSource.volume = v;
    }
}
相关推荐
vortex51 分钟前
探索 Shell:选择适合你的命令行利器 bash, zsh, fish, dash, sh...
linux·开发语言·bash·shell·dash
zzc9214 分钟前
MATLAB仿真生成无线通信网络拓扑推理数据集
开发语言·网络·数据库·人工智能·python·深度学习·matlab
周某某~6 分钟前
四.抽象工厂模式
java·设计模式·抽象工厂模式
HUN金克斯12 分钟前
C++/C函数
c语言·开发语言·c++
慢半拍iii13 分钟前
数据结构——F/图
c语言·开发语言·数据结构·c++
钢铁男儿15 分钟前
C# 表达式和运算符(表达式和字面量)
开发语言·c#
编程有点难18 分钟前
Python训练打卡Day43
开发语言·python·深度学习
m0_6371469324 分钟前
零基础入门 C 语言基础知识(含面试题):结构体、联合体、枚举、链表、环形队列、指针全解析!
c语言·开发语言·链表
LjQ204033 分钟前
网络爬虫一课一得
开发语言·数据库·python·网络爬虫