KDT Unity 5주차 팀 프로젝트
2024-01-15 TIL
사운드 시스템 만들기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
public class SoundManager
{
private AudioSource bgmSource;
private AudioSource sfxSource;
public AudioSource BGM { get { return bgmSource; } set { bgmSource = value; } }
public AudioSource SFX { get { return sfxSource; } set { sfxSource = value; } }
public readonly Dictionary<string, AudioClip> bgmBox = new ();
public readonly Dictionary<string, AudioClip> clipBox = new ();
public void Initialized()
{
GameObject SoundManagerBox = GameObject.Find("@SoundManager");
if(SoundManagerBox == null)
{
SoundManagerBox = new GameObject { name = "@SoundManager" };
GameObject main = GameObject.Find("@Main");
SoundManagerBox.transform.parent = main.transform;
bgmSource = SoundManagerBox.AddComponent<AudioSource>();
sfxSource = SoundManagerBox.AddComponent<AudioSource>();
Main.Resource.LoadAudioClipsWithLabel("BGM", Main.Sound.bgmBox);
Main.Resource.LoadAudioClipsWithLabel("Clip", Main.Sound.clipBox);
}
}
// BGM 재생
public void PlayBGM(string bgm)
{
bgmSource.clip = bgmBox[bgm];
bgmSource.loop = true;
bgmSource.Play();
}
// 효과음 재생
public void PlayClip(string clip)
{
sfxSource.PlayOneShot(clipBox[clip]);
}
}
This post is licensed under CC BY 4.0 by the author.
