From 3a37b75b01bad95149689b8904fecda9e3b7acd9 Mon Sep 17 00:00:00 2001
From: hch <305670599@qq.com>
Date: 星期二, 25 十一月 2025 16:26:54 +0800
Subject: [PATCH] 0312 防范可能启动音乐报错问题

---
 Main/Core/GameEngine/Launch/InitSettingTask.cs |    2 +-
 Main/Core/GameEngine/Launch/LaunchInHot.cs     |    1 +
 Main/System/Sound/SoundPlayer.cs               |   33 +++++++++++++++++++++++++++++++--
 3 files changed, 33 insertions(+), 3 deletions(-)

diff --git a/Main/Core/GameEngine/Launch/InitSettingTask.cs b/Main/Core/GameEngine/Launch/InitSettingTask.cs
index 8f0d469..3b2fd94 100644
--- a/Main/Core/GameEngine/Launch/InitSettingTask.cs
+++ b/Main/Core/GameEngine/Launch/InitSettingTask.cs
@@ -13,7 +13,7 @@
     public override void Begin()
     {
         ShaderUtility.InitGlobalParams();
-        SoundPlayer.CreateSoundPlayer();
+        // SoundPlayer.CreateSoundPlayer();
         //SoundPlayer.Instance.PlayLoginMusic();
 
         SystemSetting.Instance.SetSoundVolume(SystemSetting.Instance.GetSoundVolume());
diff --git a/Main/Core/GameEngine/Launch/LaunchInHot.cs b/Main/Core/GameEngine/Launch/LaunchInHot.cs
index 740a69b..b1865c5 100644
--- a/Main/Core/GameEngine/Launch/LaunchInHot.cs
+++ b/Main/Core/GameEngine/Launch/LaunchInHot.cs
@@ -25,6 +25,7 @@
         System.Net.ServicePointManager.DefaultConnectionLimit = 100;//璁剧疆http鏈�澶ц繛鎺ユ暟
         Application.backgroundLoadingPriority = ThreadPriority.High;
         Screen.sleepTimeout = SleepTimeout.NeverSleep;
+        SoundPlayer.Instance.Init();
 
         SDKUtils.Instance.Init();  //鍘焥dk鎺ュ彛
         if (!AssetSource.isUseAssetBundle)
diff --git a/Main/System/Sound/SoundPlayer.cs b/Main/System/Sound/SoundPlayer.cs
index ad7e910..85d3aee 100644
--- a/Main/System/Sound/SoundPlayer.cs
+++ b/Main/System/Sound/SoundPlayer.cs
@@ -12,17 +12,23 @@
     static readonly List<int> commonUseAudioSet = new List<int>() { defaultClickPositiveAudio, defaultClickNegativeAudio, 3, 4 };
 
     static SoundPlayer m_Instance = null;
+    private static readonly object m_Lock = new object();
     public static SoundPlayer Instance
     {
         get
         {
             if (m_Instance == null)
             {
-                CreateSoundPlayer();
+                lock (m_Lock)
+                {
+                    if (m_Instance == null)
+                    {
+                        CreateSoundPlayer();
+                    }
+                }
             }
             return m_Instance;
         }
-
     }
 
     [SerializeField] AudioSource m_MusicAudioSource;
@@ -34,6 +40,19 @@
 
     Dictionary<int, AudioClip> commonUseAudioClipDict = new Dictionary<int, AudioClip>();
     Dictionary<int, AudioClip> musicAudioClipDict = new Dictionary<int, AudioClip>();
+
+    void Awake()
+    {
+        if (m_Instance == null)
+        {
+            m_Instance = this;
+            DontDestroyOnLoad(gameObject);
+        }
+        else if (m_Instance != this)
+        {
+            Destroy(gameObject);
+        }
+    }
 
     public event Action<bool> muteEvent;
     bool m_Mute = false;
@@ -52,8 +71,18 @@
         }
     }
 
+    public void Init()
+    {
+        //鍒濆鍖栫敤
+    }
+
     public static void CreateSoundPlayer()
     {
+        if (m_Instance != null)
+        {
+            return;
+        }
+
         var gameObject = GameObject.Instantiate(BuiltInLoader.LoadPrefab("SoundPlayer"));
         m_Instance = gameObject.GetComponent<SoundPlayer>();
         m_Instance.name = "SoundPlayer";

--
Gitblit v1.8.0