//called by Groups manager public IEnumerator CheckGroupLoadStates() { if (!mInitialized) { yield break; } if (RootGroup == null || RootGroup.IsDestroyed) { Debug.Log("ROOT GROUP was null or destroyed in unloader, we're finished"); yield break; } //we may have to backtrack if new groups were added if (NotPreparedToUnload.Count > 0) { LoadState = WIGroupLoadState.Loaded; } else if (PreparingToUnload.Count > 0) { LoadState = WIGroupLoadState.PreparingToUnload; } else if (Unloading.Count > 0) { LoadState = WIGroupLoadState.Unloading; } switch (LoadState) { case WIGroupLoadState.Loaded: //tell everyone in the Loaded list to prepare to unload //then move them all into preparing to unload //---TRANSITION TO PREPARED TO UNLOAD---// for (int i = NotPreparedToUnload.LastIndex(); i >= 0; i--) { if (NotPreparedToUnload [i].PrepareToUnload()) { PreparingToUnload.Add(NotPreparedToUnload [i]); NotPreparedToUnload.RemoveAt(i); } yield return(null); } if (NotPreparedToUnload.Count == 0 && RootGroup.PrepareToUnload()) { LoadState = WIGroupLoadState.PreparingToUnload; //RootGroup.LoadState = WIGroupLoadState.PreparingToUnload; } yield return(null); break; case WIGroupLoadState.PreparingToUnload: //go through each child group and ask if it's ready to unload //this will force the group to ask each of its child items //if it's prepared we move it to the prepared to unload group for (int i = PreparingToUnload.LastIndex(); i >= 0; i--) { WIGroup loadedGroup = PreparingToUnload [i]; if (loadedGroup.ReadyToUnload) { PreparingToUnload.RemoveAt(i); ReadyToUnload.Add(loadedGroup); } yield return(null); //yield return null; } //---TRANSITION TO UNLOADING---// //if all are ready to unload and there are no more not prepared to unload //then begin unload - there's no turning back at this point! if (PreparingToUnload.Count == 0 && RootGroup.ReadyToUnload) { Unloading.AddRange(ReadyToUnload); ReadyToUnload.Clear(); for (int i = 0; i < Unloading.Count; i++) { Unloading [i].BeginUnload(); } RootGroup.BeginUnload(); LoadState = WIGroupLoadState.Unloading; //RootGroup.LoadState = WIGroupLoadState.Unloading; } yield return(null); break; case WIGroupLoadState.Unloading: if (Unloading.Count > 0) { for (int i = Unloading.LastIndex(); i >= 0; i--) { if (Unloading [i].FinishedUnloading) { FinishedUnloading.Add(Unloading [i]); Unloading.RemoveAt(i); //this generates a huge amount of garbage yield break; } #if UNITY_EDITOR else { LastHoldout = Unloading [i].FileName + " " + Unloading [i].HoldoutChildItem; } #endif yield return(null); } } else { if (FinishedUnloading.Count > 0) { for (int i = FinishedUnloading.LastIndex(); i >= 0; i--) { if (FinishedUnloading [i] == null || FinishedUnloading [i].IsDestroyed) { FinishedUnloading.RemoveAt(i); } else { //we see if it's ready to actually be destroyed //no groups greater than [x] depth that have not been destroyed #if UNITY_EDITOR LastHoldout = FinishedUnloading [i].FileName + " " + FinishedUnloading [i].HoldoutChildItem; #endif if (!FinishedUnloading [i].HasChildGroups) { ReadyToDestroy.Add(FinishedUnloading [i]); FinishedUnloading.RemoveAt(i); } } yield return(null); } } else { } } yield return(null); //---TRANSITION TO UNLOADED---// //if we have more than just the root group then we wait until the root group is finished unloading to kill everything if (FinishedUnloading.Count == 0 && ReadyToDestroy.Count == 0) { if (!RootGroup.FinishedUnloading) { //now it will be detsroyed by WIGroups #if UNITY_EDITOR LastHoldout = RootGroup.FileName + " " + RootGroup.HoldoutChildItem; #endif if (RootGroup.PrepareToUnload()) { RootGroup.BeginUnload(); } } else { LoadState = WIGroupLoadState.Unloaded; } } break; case WIGroupLoadState.Unloaded: //nothing left to do break; default: Debug.Log("Weird load state in WIGroupUnloader: " + LoadState.ToString()); break; } yield break; }