diff --git a/UnityProject/Assets/Dependencies/JEngine/Core/Manager/ClassBindMgr.cs b/UnityProject/Assets/Dependencies/JEngine/Core/Manager/ClassBindMgr.cs index 1b3ed401..bbe4139b 100644 --- a/UnityProject/Assets/Dependencies/JEngine/Core/Manager/ClassBindMgr.cs +++ b/UnityProject/Assets/Dependencies/JEngine/Core/Manager/ClassBindMgr.cs @@ -42,7 +42,7 @@ public static void Instantiate() } private static ClassBindMgr _instance; - public static readonly HashSet LoadedScenes = new HashSet() {SceneManager.GetActiveScene()}; + public static readonly HashSet LoadedScenes = new HashSet() { }; private static readonly List Cbs = new List(30); private void Awake() @@ -52,20 +52,35 @@ private void Awake() DestroyImmediate(this); } - SceneManager.sceneLoaded += (scene, mode) => - { - LoadedScenes.Add(scene); - DoBind(); - }; - - SceneManager.sceneUnloaded+=scene => - { - LoadedScenes.Remove(scene); - }; - + SceneManager.sceneLoaded += OnSceneLoaded; + SceneManager.sceneUnloaded += OnSceneUnloaded; + LoadedScenes.Add(SceneManager.GetActiveScene()); + DoBind(); + } + + private void OnSceneLoaded(Scene scene, LoadSceneMode mode) + { + LoadedScenes.Add(scene); DoBind(); } + private void OnSceneUnloaded(Scene scene) + { + LoadedScenes.Remove(scene); + } + + private void OnDestroy() + { + if (_instance == this) + { + _instance = null; + SceneManager.sceneLoaded -= OnSceneLoaded; + SceneManager.sceneUnloaded -= OnSceneUnloaded; + LoadedScenes.Clear(); + Cbs.Clear(); + } + } + public static void DoBind(List cbs) { foreach (var cb in cbs) @@ -91,32 +106,32 @@ public static void DoBind(List cbs) { continue; } - + cb.SetVal(data); } } - + //激活 foreach (var cb in cbs) { foreach (ClassData data in cb.scriptsToBind) { - if (data == null ||data.Activated) + if (data == null || data.Activated) { continue; } - + cb.Active(data); } } } - + public static void DoBind(ClassBind cb) { if (Cbs.Contains(cb)) return; - DoBind(new List{cb}); + DoBind(new List { cb }); } - + public static void DoBind() { var c = Tools.FindObjectsOfTypeAll(); diff --git a/UnityProject/Assets/Dependencies/JEngine/Core/Tool/GameObjectTools.cs b/UnityProject/Assets/Dependencies/JEngine/Core/Tool/GameObjectTools.cs index daeb5118..d7c19e5e 100644 --- a/UnityProject/Assets/Dependencies/JEngine/Core/Tool/GameObjectTools.cs +++ b/UnityProject/Assets/Dependencies/JEngine/Core/Tool/GameObjectTools.cs @@ -103,14 +103,15 @@ public static List FindObjectsOfTypeAll() scene.GetRootGameObjects(temp); all.AddRange(temp); } - ObjectPool>.Return(temp); if (all == null) { return new List(); } + temp.Clear(); + ObjectPool>.Return(temp); List lst = ObjectPool>.Peak() != null ? ObjectPool>.Request() : new List(all.Count); - lst.Clear(); List tempT = ObjectPool>.Peak() != null ? ObjectPool>.Request() : new List(all.Count); + lst.Clear(); tempT.Clear(); foreach (var gameObject in all) { @@ -118,6 +119,8 @@ public static List FindObjectsOfTypeAll() lst.AddRange(tempT); tempT.Clear(); } + all.Clear(); + ObjectPool>.Return(all); ObjectPool>.Return(tempT); return lst; #endif