using FishNet.Managing.Logging; using FishNet.Managing.Scened; using FishNet.Object; using UnityEngine; namespace FishNet.Example.Scened { /// /// Unloads specified scenes when entering or exiting this trigger. /// public class SceneUnloaderExample : MonoBehaviour { /// /// Scenes to unload. /// [Tooltip("Scenes to unload.")] [SerializeField] private string[] _scenes = new string[0]; /// /// True to only unload for the connectioning causing the trigger. /// [Tooltip("True to only unload for the connectioning causing the trigger.")] [SerializeField] private bool _connectionOnly; /// /// True to unload unused scenes. /// [Tooltip("True to unload unused scenes.")] [SerializeField] private bool _unloadUnused = true; /// /// True to fire when entering the trigger. False to fire when exiting the trigger. /// [Tooltip("True to fire when entering the trigger. False to fire when exiting the trigger.")] [SerializeField] private bool _onTriggerEnter = true; [Server(Logging = LoggingType.Off)] private void OnTriggerEnter(Collider other) { if (!_onTriggerEnter) return; UnloadScenes(other.gameObject.GetComponent()); } [Server(Logging = LoggingType.Off)] private void OnTriggerExit(Collider other) { if (_onTriggerEnter) return; UnloadScenes(other.gameObject.GetComponent()); } /// /// Unload scenes. /// /// private void UnloadScenes(NetworkObject triggeringIdentity) { if (!InstanceFinder.NetworkManager.IsServer) return; //NetworkObject isn't necessarily needed but to ensure its the player only run if nob is found. if (triggeringIdentity == null) return; UnloadOptions unloadOptions = new UnloadOptions() { Mode = (_unloadUnused) ? UnloadOptions.ServerUnloadMode.UnloadUnused : UnloadOptions.ServerUnloadMode.KeepUnused }; SceneUnloadData sud = new SceneUnloadData(_scenes); sud.Options = unloadOptions; //Unload only for the triggering connection. if (_connectionOnly) InstanceFinder.SceneManager.UnloadConnectionScenes(triggeringIdentity.Owner, sud); //Unload for all players. else InstanceFinder.SceneManager.UnloadGlobalScenes(sud); } } }