
* 겪고 있는 문제 상황을 최대한 자세하게 작성해주세요.
* 문제 해결을 위해 어떤 시도를 해보았는지 구체적으로 함께 알려주세요.
점수=1000점 달성 시 왕관(crown)을 rtan이 쓰고 이벤트 음악이 나오고 배경음악은 안나오게 하고싶은데요. 같이 겹쳐서 나옵니다.
배경음악=bgm, 1000점 달성 시 이벤트 음악=Cgm 입니다.
작성한 코드 및 에러 메세지
오using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class audioManager : MonoBehaviour
{
public static audioManager I;
public AudioSource bgm;
public AudioSource cgm;
public AudioSource jump;
public AudioSource audioSource;
public float bgmVolume = 1f;
public AudioClip Sfx1;
public AudioClip Sfx2;
public float SfxVolume = 0.8f; // define the volume for Sfx1 and Sfx2
public AudioClip Cgm;
public float CgmVolume = 1f; // define the volume for Cgm
private AudioSource bgmSource;
private AudioSource Sfx1Source; // to store the audio source for Sfx1
private AudioSource Sfx2Source; // to store the audio source for Sfx2
private float prevSfx1Volume; // to store the previous volume of Sfx1
private float prevSfx2Volume; // to store the previous volume of Sfx2
private int score;
private bool isCgmPlayed = false;
private AudioSource CgmSource;
// bgmSource를 AudioSource 타입으로 선언합니다.
private bool StopCgm;
private void Awake()
{
I = this;
}
private void Start()
{
audioSource = GetComponent<AudioSource>();
bgmSource = GetComponent<AudioSource>();
Sfx1Source = gameObject.AddComponent<AudioSource>();
Sfx1Source.clip = Sfx1;
Sfx1Source.volume = SfxVolume;
Sfx2Source = gameObject.AddComponent<AudioSource>();
Sfx2Source.clip = Sfx2;
Sfx2Source.volume = SfxVolume;
}
private void Update()
{
if (CgmSource.isPlaying)
{
Sfx1Source.volume = 0.6f;
Sfx2Source.volume = 0.6f;
bgmSource.Stop();
}
else
{
// If the congratulatory music is not playing, set the volume of Sfx1 and Sfx2 to their previous values
Sfx1Source.volume = prevSfx1Volume;
Sfx2Source.volume = prevSfx2Volume;
bgmSource.Play();
}
}
public void Sfx1PlayOneShot()
{
Sfx1Source.PlayOneShot(Sfx1);
}
public void Sfx2PlayOneShot()
{
Sfx2Source.PlayOneShot(Sfx2);
}
public void CgmPlayOneShot()
{
audioSource.clip = Cgm;
audioSource.PlayOneShot(Cgm);
if (score >= 1500)
{
audioSource.PlayOneShot(Cgm);
Sfx1Source.volume = 0.6f;
Sfx2Source.volume = 0.6f;
bgmSource.Stop();
AudioSource[] allAudioSources = FindObjectsOfType(typeof(AudioSource)) as AudioSource[];
foreach (AudioSource audioS in allAudioSources)
{
if (audioS != audioSource && audioS != bgmSource && audioS != CgmSource && audioS != Sfx1Source && audioS != Sfx2Source)
audioS.Stop();
}
// Reset the score to 0
score = 0;
}
else
{
// Play the Sfx1 and Sfx2 sound effect
Sfx1PlayOneShot();
Sfx2PlayOneShot();
// If the congratulatory music is not playing, play the bgm
if (!isCgmPlayed)
{
// Play bgm
bgmSource.Play();
isCgmPlayed = true;
}
}
}
public void bgmPlay()
{
bgm.Play();
}
public void bgmStop()
{
bgm.Stop();
}
public void PlayCgmOneShot()
{
cgm.PlayOneShot(cgm.clip);
}
public void JumpPlay()
{
jump.Play();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System;
using Random = System.Random;
using System.IO;
using UnityEngine.Networking;
public class gameManager : MonoBehaviour
{
public GameObject rain;
public GameObject redrain;
public Text scoreText;
public Text timeText;
public GameObject star;
public GameObject crown;
public GameObject panel;
public static gameManager I;
private int totalScore = 0;
private float limit = 250.0f;
private bool crownAndStarEnabled = false;
private bool starsCreated = false;
private bool crownCreated = false;
private bool starEnabled = false;
public GameObject starPrefab;
public float starLifetime = 5f;
private Vector3 randomPos;
public GameObject[] backgroundImages;
private int backgroundIndex = 0;
private List<GameObject> stars = new List<GameObject>();
public int numStars = 55;
public Vector2 screenBounds = new Vector2(5, 5);
public float minScale = 0.08f;
public float maxScale = 0.25f;
public Color minColor = Color.white;
public Color maxColor = Color.yellow;
public float starSpeed = 5f;
public float starCreationInterval = 0.1f;
public float backgroundChangeInterval = 10f;
private float lastBackgroundChangeTime = 0f;
public GameObject backimage1;
public GameObject backimage2;
public GameObject backimage3;
public GameObject backimage4;
private bool crownEnabled;
private Func<IEnumerator> GetCreateStarsCoroutine;
private object go;
private void Awake()
{
I = this;
}
private void Start()
{
InvokeRepeating("makeRain", 0.0f, 0.5f);
InvokeRepeating("makeRedrain", 0.0f, 0.5f);
InitGame();
LoadBackgroundImages();
StartCoroutine(CreateStarsCoroutine1);
IEnumerator LoadBackgroundImages()
{
for (int i = 1; i <= 4; i++)
{
string imageName = "backimage" + i.ToString();
string imagePath = "BackgroundImages/" + imageName;
ResourceRequest request = Resources.LoadAsync<Sprite>(imagePath);
yield return request;
Sprite sprite = request.asset as Sprite;
}
}
}
private void InitGame()
{
throw new NotImplementedException();
}
private void initGame()
{
Time.timeScale = 1.0f;
limit = 250.0f;
totalScore = 0;
crownCreated = false;
starEnabled = false;
crownAndStarEnabled = false;
}
private void makeRain()
{
Instantiate(rain);
}
private void makeRedrain()
{
Instantiate(redrain);
}
private void Update()
{
limit -= Time.deltaTime;
if (limit < 0)
{
limit = 0.0f;
panel.SetActive(true);
Time.timeScale = 0.0f;
}
timeText.text = limit.ToString("N2");
if (totalScore >= 800 && !starsCreated)
{
starsCreated = true;
Coroutine coroutine = StartCoroutine(CreateStarsCoroutine1);
starEnabled = true;
}
if (totalScore >= 1000 && !crownEnabled)
{
audioManager.I.CgmPlayOneShot();
if (audioManager.I.bgm.isPlaying)
{
audioManager.I.bgm.Stop();
}
GameObject character = GameObject.Find("rtan");
GameObject crownInstance = Instantiate(crown, character.transform.position + Vector3.up * 1.5f, Quaternion.identity);
crownInstance.transform.parent = character.transform;
crownInstance.transform.localPosition = new Vector3(0.0f, 0.68f, 0.0f);
crownEnabled = true;
StartCoroutine(GetDestroyCrownCoroutine());
}
UpdateBackgroundImages();
// Move the stars
for (int i = 0; i < stars.Count; i++)
{
stars[i].transform.position += new Vector3(0, -starSpeed * Time.deltaTime, 0);
// Destroy stars that go below the screen
if (stars[i].transform.position.y < -screenBounds.y - 1f)
{
Destroy(stars[i]);
stars.RemoveAt(i);
i--;
}
}
// Change the background
if (backgroundChangeInterval > 0 && Time.time - lastBackgroundChangeTime > backgroundChangeInterval)
{
lastBackgroundChangeTime = Time.time;
switch (backgroundIndex)
{
case 1:
backimage1.SetActive(false);
backimage2.SetActive(true);
backgroundIndex = 2;
break;
case 2:
backimage2.SetActive(false);
backimage3.SetActive(true);
backgroundIndex = 3;
break;
case 3:
backimage3.SetActive(false);
backimage4.SetActive(true);
backgroundIndex = 4;
break;
case 4:
backimage4.SetActive(false);
backimage1.SetActive(true);
backgroundIndex = 1;
break;
}
}
}
private void LoadBackgroundImages()
{
for (int i = 0; i < backgroundImages.Length; i++)
{
StartCoroutine(LoadImage("background" + (i + 1).ToString() + ".jpg", backgroundImages[i]));
}
}
private IEnumerator LoadImage(string url, GameObject target)
{
string path = System.IO.Path.Combine(Application.streamingAssetsPath, url);
UnityWebRequest www = UnityWebRequestTexture.GetTexture(path);
yield return www.SendWebRequest();
if (www.result != UnityWebRequest.Result.Success)
{
Debug.Log(www.error);
}
}
private IEnumerator CreateStarsCoroutine1
{
get
{
while (true)
{
if (starEnabled)
{
if (stars.Count < numStars)
{
GameObject newStar = Instantiate(starPrefab);
newStar.transform.position = new Vector3(UnityEngine.Random.Range(-screenBounds.x, screenBounds.x), UnityEngine.Random.Range(-screenBounds.y, screenBounds.y), 0);
newStar.transform.localScale = new Vector3(UnityEngine.Random.Range(minScale, maxScale), UnityEngine.Random.Range(minScale, maxScale), 1);
newStar.GetComponent<SpriteRenderer>().color = Color.Lerp(minColor, maxColor, UnityEngine.Random.Range(0f, 1f));
stars.Add(newStar);
}
yield return new WaitForSeconds(starCreationInterval);
}
else
{
yield return null;
}
}
}
}
void UpdateBackgroundImages()
{
if (Time.frameCount % 3000 == 0)
{
switch (backgroundIndex)
{
case 1:
backimage1.SetActive(false);
backimage2.SetActive(true);
backgroundIndex = 2;
break;
case 2:
backimage2.SetActive(false);
backimage3.SetActive(true);
backgroundIndex = 3;
break;
case 3:
backimage3.SetActive(false);
backimage4.SetActive(true);
backgroundIndex = 4;
break;
case 4:
backimage4.SetActive(false);
backimage1.SetActive(true);
backgroundIndex = 1;
break;
}
}
}
private string CreateAndDestroyStars()
{
throw new NotImplementedException();
}
internal int getTotalScore()
{
throw new NotImplementedException();
}
public void SpawnStar()
{
// Generate a random position for the star
// Instantiate a new star at the random position
GameObject newStar = Instantiate(starPrefab, randomPos, Quaternion.identity);
// Set the parent of the new star to the GameManager object in the scene hierarchy
newStar.transform.SetParent(transform);
// Schedule the star to be destroyed after some time
Destroy(newStar, starLifetime);
}
private IEnumerator CreateStarsCoroutine
{
get
{
for (int i = 0; i < 55; i++)
{
// Generate a random position for the star
Vector3 randomPos = new Vector3(UnityEngine.Random.Range(-2.0f, 2.0f), UnityEngine.Random.Range(3.0f, 6.0f), 0.0f);
// Instantiate a new star at the random position
GameObject starInstance = Instantiate(star, randomPos, Quaternion.identity);
// Schedule the star to be destroyed after some time
StartCoroutine(DestroyStarCoroutine(starInstance));
yield return new WaitForSeconds(1.0f);
}
}
}
private string DestroyStarCoroutine(GameObject starInstance)
{
throw new NotImplementedException();
}
private IEnumerator GetDestroyCrownCoroutine()
{
yield return new WaitForSeconds(5f);
crownEnabled = false;
crownCreated = false;
}
private void Destroy(object starInstance)
{
throw new NotImplementedException();
}
private IEnumerator DestroyCrownCoroutine
{
get
{
yield return new WaitForSeconds(80.0f);
GameObject character = GameObject.Find("rtan");
Destroy(character.transform.Find("crown(Clone)").gameObject);
}
}
public void addScore(int score)
{
totalScore += score;
scoreText.text = totalScore.ToString();
}
public void retry()
{
SceneManager.LoadScene("MainScene");
crownAndStarEnabled = false;
}
}
