유니티/쯔꾸르
[유니티 강좌] 2D RPG 쯔꾸르 제작하기 Part 8 : 오디오 매니저
아메숑
2019. 3. 14. 05:51
출처 - https://www.youtube.com/watch?v=EdsVx9yN2Cc&list=PLUZ5gNInsv_NW8RQx8__0DxiuPZn3VDzP
1. 코드
AudioManager.cs
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | using System.Collections; using System.Collections.Generic; using UnityEngine; //인스펙터 창에 강제로 띄운다. [System.Serializable] public class Sound { public string name; //사운드 이름 public AudioClip clip; //사운드 파일 private AudioSource source; // 사운드 플레이어 public float volumn; public bool loop; public void SetSource(AudioSource _source) { source = _source; source.clip = clip; source.loop = loop; } public void SetVolumn() { source.volume = volumn; } public void Play() { source.Play(); } public void Stop() { source.Stop(); } public void SetLoop() { source.loop = true; } public void SetLoopCencel() { source.loop = false; } } public class AudioManager : MonoBehaviour { static public AudioManager instance; [SerializeField] public Sound[] sounds; //start보다 더 먼저 실행되는 함수 private void Awake() { if (instance == null) { instance = this; //맵을 이동해도 카메라를 파괴시키지 않는다. DontDestroyOnLoad(this.gameObject); } else { Destroy(this.gameObject); } } // Start is called before the first frame update void Start() { for(int i = 0; i < sounds.Length; i++) { GameObject soundObject = new GameObject("사운드 파일 이름 : " + i + "=" + sounds[i].name); //추가 될 객체의 이름 sounds[i].SetSource(soundObject.AddComponent<AudioSource>()); soundObject.transform.SetParent(this.transform); //이 스크립트가 실행되는 객체안에 오디오 객체가 추가 된다. (상하관계) } } public void Play(string _name) { for(int i = 0; i < sounds.Length; i++) { if(_name == sounds[i].name) { sounds[i].Play(); return; } } } public void Stop(string _name) { for (int i = 0; i < sounds.Length; i++) { if (_name == sounds[i].name) { sounds[i].Stop(); return; } } } public void SetLoop(string _name) { for (int i = 0; i < sounds.Length; i++) { if (_name == sounds[i].name) { sounds[i].SetLoop(); return; } } } public void SetLoopCancel(string _name) { for (int i = 0; i < sounds.Length; i++) { if (_name == sounds[i].name) { sounds[i].SetLoopCencel(); return; } } } public void SetVolumn(string _name, float _Volumn) { for (int i = 0; i < sounds.Length; i++) { if (_name == sounds[i].name) { sounds[i].volumn = _Volumn; sounds[i].SetVolumn(); return; } } } // Update is called once per frame void Update() { } } | cs |
MovingObject.cs
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class MovingObject : MonoBehaviour { static public MovingObject instance; //transferMap 스크립트에 있는 trasferMapName 변수의 값을 가져온다. public string currentMapName; private BoxCollider2D boxCollider; public LayerMask layerMask; //통과가 불가능한 레이어를 설정 public float speed; private Vector3 vector; public float runSpeed; private float applyRunSpeed; private bool applyRunFlag = false; public int walkCount; private int currentWalkCount; private bool canMove = true; private Animator animator; //public AudioClip walkSound_1; //public AudioClip walkSound_2; //private AudioSource audioSource; public string walkSound_1; public string walkSound_2; public string walkSound_3; public string walkSound_4; private AudioManager theAudio; // Start is called before the first frame update void Start() { if(instance == null) { //맵을 이동해도 캐릭터를 파괴시키지 않는다. DontDestroyOnLoad(this.gameObject); boxCollider = GetComponent<BoxCollider2D>(); //audioSource = GetComponent<AudioSource>(); animator = GetComponent<Animator>(); theAudio = FindObjectOfType<AudioManager>(); instance = this; } else { Destroy(this.gameObject); } } IEnumerator MoveCoroutine() { while (Input.GetAxisRaw("Horizontal") != 0 || Input.GetAxisRaw("Vertical") != 0) { if (Input.GetKey(KeyCode.LeftShift)) { applyRunSpeed = runSpeed; applyRunFlag = true; } else { applyRunSpeed = 0; applyRunFlag = false; } vector.Set(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"), transform.position.z); if (vector.x != 0) vector.y = 0; animator.SetFloat("DirX", vector.x); animator.SetFloat("DirY", vector.y); RaycastHit2D hit; //레이저를 쏴서 방해물이 있으면 방해물을 리턴하고, 아무것도 없으면 null리턴 Vector2 start = transform.position; //캐릭터의 현재위치 //speed = 2.4 walkCount = 20 이므로 48픽셀만큼 이동 Vector2 end = start + new Vector2(vector.x * speed * walkCount, vector.y * speed * walkCount); //캐릭터가 이동하고자 하는 곳 boxCollider.enabled = false; //캐릭터 자신의 박스컬라이더를 인식못하게 해줌 hit = Physics2D.Linecast(start, end, layerMask); boxCollider.enabled = true; if (hit.transform != null) //부딪히면 다음 명령어들은 실행하지 않음 break; animator.SetBool("Walking", true); int temp = Random.Range(1, 5); switch (temp) { case 1: theAudio.Play(walkSound_1); break; case 2: theAudio.Play(walkSound_2); break; case 3: theAudio.Play(walkSound_3); break; case 4: theAudio.Play(walkSound_4); break; } while (currentWalkCount < walkCount) { if (vector.x != 0) { transform.Translate(vector.x * (speed + applyRunSpeed), 0, 0); } else if (vector.y != 0) { transform.Translate(0, vector.y * (speed + applyRunSpeed), 0); } if (applyRunFlag) currentWalkCount++; currentWalkCount++; yield return new WaitForSeconds(0.01f); /* //walkcount가 20이므로 2번 실행됨 if(currentWalkCount % 9 == 2) { int temp = Random.Range(1, 2); switch (temp) { case 1: audioSource.clip = walkSound_1; audioSource.Play(); break; case 2: audioSource.clip = walkSound_2; audioSource.Play(); break; } }*/ } currentWalkCount = 0; } animator.SetBool("Walking", false); canMove = true; } // Update is called once per frame void Update() { if(canMove) { if (Input.GetAxisRaw("Horizontal") != 0 || Input.GetAxisRaw("Vertical") != 0) { canMove = false; StartCoroutine(MoveCoroutine()); } } } } | cs |
2. 강좌
빈 객체 생성해서 a map과 b map을 구분한다.
walk sound에 알맞은 사운드를 넣고, audio source 컴포넌트를 추가해준다.
audiomanager.cs를 생성한다.
빈 객체에 생성한 스크립트를 추가한다.
오디오를 추가한다.
플레이를 하면 왼쪽에 네 개의 객체가 생성된 것을 확인할 수 있다.
플레이어에 새로 생성된 입력란에 audiomanager 객체에서 지정했던 사운드의 이름을 적어준다.