Skip to content

Commit

Permalink
fixed reload bug in ClassBindMgr + optimized FindObjectsOfType perfor…
Browse files Browse the repository at this point in the history
…mance
  • Loading branch information
JasonXuDeveloper committed Sep 26, 2022
1 parent 044c8f6 commit 6aeb6dd
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static void Instantiate()
}

private static ClassBindMgr _instance;
public static readonly HashSet<Scene> LoadedScenes = new HashSet<Scene>() {SceneManager.GetActiveScene()};
public static readonly HashSet<Scene> LoadedScenes = new HashSet<Scene>() { };
private static readonly List<ClassBind> Cbs = new List<ClassBind>(30);

private void Awake()
Expand All @@ -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<ClassBind> cbs)
{
foreach (var cb in cbs)
Expand All @@ -91,32 +106,32 @@ public static void DoBind(List<ClassBind> 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<ClassBind>{cb});
DoBind(new List<ClassBind> { cb });
}

public static void DoBind()
{
var c = Tools.FindObjectsOfTypeAll<ClassBind>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,21 +103,24 @@ public static List<T> FindObjectsOfTypeAll<T>()
scene.GetRootGameObjects(temp);
all.AddRange(temp);
}
ObjectPool<List<GameObject>>.Return(temp);
if (all == null)
{
return new List<T>();
}
temp.Clear();
ObjectPool<List<GameObject>>.Return(temp);
List<T> lst = ObjectPool<List<T>>.Peak() != null ? ObjectPool<List<T>>.Request() : new List<T>(all.Count);
lst.Clear();
List<T> tempT = ObjectPool<List<T>>.Peak() != null ? ObjectPool<List<T>>.Request() : new List<T>(all.Count);
lst.Clear();
tempT.Clear();
foreach (var gameObject in all)
{
gameObject.GetComponentsInChildren(true, tempT);
lst.AddRange(tempT);
tempT.Clear();
}
all.Clear();
ObjectPool<List<GameObject>>.Return(all);
ObjectPool<List<T>>.Return(tempT);
return lst;
#endif
Expand Down

0 comments on commit 6aeb6dd

Please sign in to comment.