如何在加载场景时重现声音效果

发布于 2025-02-02 12:45:14 字数 2037 浏览 3 评论 0原文

如何在加载场景时重现声音效果? 我想在进入下一个场景时发挥声音效果,但它没有播放。我已经尝试使用method dontdestroyonload,但它不起作用,我会收到此错误:nullreference exception:对象引用未设置为对象的实例 levelloader.loadnextlevel() 这是我管理声音的脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SoundManagerScript : MonoBehaviour
{
    public static AudioClip clikSound, shootSound;
    static AudioSource audioScr;
    // Start is called before the first frame update
    void Start()
    {
        
        clikSound = Resources.Load<AudioClip> ("clik");
        shootSound = Resources.Load<AudioClip>("shoot");
        audioScr = GetComponent<AudioSource> ();
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    public static void PlaySound(string clip)
    {
        switch (clip)
        {
            case "clik":
                audioScr.PlayOneShot(clikSound);
                    break;

            case "shoot":
                audioScr.PlayOneShot(shootSound);
                break;
        }
    }
}

这是在场景之间切换的脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class LevelLoader : MonoBehaviour
{
    public Animator transition;

    public float transitionTime = 1f;

    
    static AudioSource audioScr;
    public void LoadNextLevel()
    {
        DontDestroyOnLoad(audioScr.gameObject);
        SoundManagerScript.PlaySound("shoot");
        StartCoroutine(LoadLevel(SceneManager.GetActiveScene().buildIndex + 1));
        
        

    }
    
    
    IEnumerator LoadLevel(int LevelIndex)
    {


        
        transition.SetTrigger("Start");
        yield return new WaitForSeconds(transitionTime);
        SceneManager.LoadScene(LevelIndex);
        
        
        
        

    }
   


}

how to reproduce a sound effect while loading a scene?
I would like to play a sound effect when moving to the next scene, but it does not play. I already tried with method DontDestroyOnLoad but it doesn't work and I get this error: NullReferenceException: Object reference not set to an instance of an object
LevelLoader.LoadNextLevel ()
this is my script to manage sounds:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SoundManagerScript : MonoBehaviour
{
    public static AudioClip clikSound, shootSound;
    static AudioSource audioScr;
    // Start is called before the first frame update
    void Start()
    {
        
        clikSound = Resources.Load<AudioClip> ("clik");
        shootSound = Resources.Load<AudioClip>("shoot");
        audioScr = GetComponent<AudioSource> ();
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    public static void PlaySound(string clip)
    {
        switch (clip)
        {
            case "clik":
                audioScr.PlayOneShot(clikSound);
                    break;

            case "shoot":
                audioScr.PlayOneShot(shootSound);
                break;
        }
    }
}

and this is the script for switching between scenes:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class LevelLoader : MonoBehaviour
{
    public Animator transition;

    public float transitionTime = 1f;

    
    static AudioSource audioScr;
    public void LoadNextLevel()
    {
        DontDestroyOnLoad(audioScr.gameObject);
        SoundManagerScript.PlaySound("shoot");
        StartCoroutine(LoadLevel(SceneManager.GetActiveScene().buildIndex + 1));
        
        

    }
    
    
    IEnumerator LoadLevel(int LevelIndex)
    {


        
        transition.SetTrigger("Start");
        yield return new WaitForSeconds(transitionTime);
        SceneManager.LoadScene(LevelIndex);
        
        
        
        

    }
   


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水染的天色ゝ 2025-02-09 12:45:14

因为您需要的游戏对象被破坏了。 Audiosource本身还不够。也许您的AudioListener被摧毁了?

安排这些东西。我建议您有多个场景。例如,持久场景,菜单,级别1等,您可以将音频放在持久的场景中。

您可以通过sceemanager.loadscene(“其他CeneName”,LoadSceneMode.Addivity);拥有多个场景。那是多个场景获得力量的地方。

可能很难,但我建议您使用用于处理诸如音频之类的场景和资产。

Becasuse you're required gameobjects are destroyed. AudioSource by itself not enough. Maybe your AudioListener destroyed?

To arrange those things. I recommend you to have multiple scenes. in example, Persistent scene, MenuScene, Level1 etc, and you can put your audios in Persistent scene.

You can have more than one scene via SceneManager.LoadScene("OtherSceneName", LoadSceneMode.Additive);. That's where multiple scenes gets it's power.

It may be hard but i recommend you to use Addressables for handling scenes and assets like audios.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文