/// <summary> /// Unload the level. This attempts to fully unload any objects that were loaded by this level. It also unregister's the /// level data Torque Folder created in the Load() function, if any, which will unregister any objects that were registered since /// the level was loaded (unless a different folder was set to be the current folder after the level was loaded). /// </summary> public void Unload() { Assert.Fatal(_loaded, "TorqueSceneData.Unload - Level not loaded, Load it first."); if (!_loaded) return; _loaded = false; // we fire the callback first so that the recipient can unhook itself from content if (this.OnUnloaded != null) this.OnUnloaded(); if (_levelFolder != null && _levelFolder.IsRegistered) { // unregister the torque folder. This should unregister everything in it, which will include all objects that were registered // since the time the level was created. TorqueObjectDatabase.Instance.Unregister(_levelFolder); _levelFolder = null; // we null things out for garbage collector } // now walk our lists and Unload stuff _UnloadObjects(this.SceneData); _UnloadObjects(this.Materials); _UnloadObjects(this.Objects); this.SceneData = null; this.Materials = null; this.Objects = null; }
/// <summary> /// Load the named level. By default, this will also create an new Torque Folder for the level and install it as the current folder. /// When you Unload the level, that torque folder, and everything in it, will be unregistered. Set CreateLevelFolder to false to /// disable this behavior. /// </summary> /// <param name="filename">The filename to load</param> /// <param name="extraAssemblies">List of assemblies to add to the deserializer's assembly list, for finding new types. Usually can be null.</param> public TorqueSceneData Load(string filename, List<Assembly> extraAssemblies) { Assert.Fatal(_allowReload || !_loaded, "TorqueSceneData.Load - Level already loaded, Unload it first."); if (_loaded) { if (_allowReload) Reset(); else return null; } _loaded = true; if (filename == null || filename == String.Empty) throw new Exception("Invalid level file name: " + filename); if (_createLevelFolder) // JMQtodo: move this into scene loader? { // create a folder for game objects that are created after this level is loaded _levelFolder = new TorqueFolder(); // we'll give it a helpful name for debugging _levelFolder.Name = "_autoname_TorqueSceneDataFolder" + this.GetHashCode(); TorqueObjectDatabase.Instance.Register(_levelFolder); TorqueObjectDatabase.Instance.CurrentFolder = _levelFolder; } // deserialize the xml TorqueXmlDeserializer d = Deserializer; //d.LoadTypesFromAssemblies = true; d.DefaultTypeMap = this.DefaultTypeMap; List<Assembly> searchAssemblies = d.Assemblies; if (extraAssemblies != null) foreach (Assembly a in extraAssemblies) if (!searchAssemblies.Contains(a)) searchAssemblies.Add(a); // deserialize into this instance d.Process(filename, this); // check version if (this.Version != RequiredVersion) throw new Exception("Cannot load this level version:" + this.Version); _LoadObjects(this.SceneData); _LoadObjects(this.Materials); _LoadObjects(this.Objects); if (_preloadMaterials) { // helps to reduce startup hitching MaterialManager.PreloadMaterials(null); } if (this.OnLoaded != null) this.OnLoaded(); return this; }