오디오 매니저
오디오를 한번에 괸리해 줄거다!
플레이어가 걷는 소리 -> 플레이어에 붙어있는 스크립트로 이동
audio clip 과 audio source 사용
이렇게 소리를 선택할 수 있다
선택해주고 오디오 소스 컴포넌트를 부착해준다.
오디오 소스를 가져온다.
걷는 while문 안에서 걷는 소리를 재상한다. 다양성을 위해 걷는 소리 1과 2를 나누어서 재생한다.
근데 이렇게 하는건 좀 부담스러운 방법이고.. 다른 방법으로 시도해본다..
오디오 매니저 객체를 생성하고 오디오 매니징을 위한 스크립트를 작성하고 이를 부착한다.
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()
{
}
}
player maneger
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());
}
}
}
}
객체에서 오디오 클립들을 추가하고 사용할 수 있다.