新增FishNet文档
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
---
|
||||
description: >-
|
||||
Fish-Networking comes with a powerful scene manager tool that enables you to
|
||||
synchronize networked scenes with minimal effort, while also exposing a lot of
|
||||
powerful options.
|
||||
---
|
||||
|
||||
# Scene Management
|
||||
|
||||
## General
|
||||
|
||||
The [**SceneManager**](../../../fishnet-building-blocks/components/managers/scenemanager.md) Component has many scene features to support your multiplayer needs. The links below will take you to the different guides for each feature that the [**SceneManager**](../../../fishnet-building-blocks/components/managers/scenemanager.md) has to offer.
|
||||
|
||||
{% hint style="success" %}
|
||||
Visit the [**API**](https://fish-networking.com/FishNet/api/api/FishNet.Managing.Scened.SceneManager.html) to see the public items exposed to the user in the SceneManager.
|
||||
{% endhint %}
|
||||
|
||||
## Sub pages
|
||||
|
||||
### [Automatic Online and Offline Scenes](loading-scenes/automatic-online-and-offline-scenes.md)
|
||||
|
||||
If you simply want an online scene to be loaded when the network starts and a different one to be loaded when it stops, then you can do that with a single component.
|
||||
|
||||
### [Scene Events](scene-events.md)
|
||||
|
||||
"Scene Events" are the Invoked Events that happen along the loading and unloading method.
|
||||
|
||||
### [Scene Data](scene-data/)
|
||||
|
||||
The Data Classes that are needed for the various features to function.
|
||||
|
||||
### [Loading Scenes](loading-scenes/)
|
||||
|
||||
Information on how to "load" scenes and the options available to the user while loading.
|
||||
|
||||
### [Unloading Scenes](unloading-scenes.md)
|
||||
|
||||
Information on how to "unload" scenes and the options available to the user while Unloading.
|
||||
|
||||
### [Scene Stacking](scene-stacking.md)
|
||||
|
||||
"Scene Stacking" is the ability for server or host to load multiple instances of a scene at once, usually with different observers in each scene.
|
||||
|
||||
### [Scene Caching](scene-caching.md)
|
||||
|
||||
"Scene Caching" is the ability for the Server to keep a scene loaded when either all clients have unloaded that scene, or stopped observing that scene.
|
||||
|
||||
### [Scene Visibility](scene-visibility.md)
|
||||
|
||||
"Scene Visibility" guide offers details of using the "Scene Condition" with the [**ObserverManager**](../../../fishnet-building-blocks/components/managers/observermanager/), and how to manage Observers in a Scene.
|
||||
|
||||
### [Persisting NetworkObjects](persisting-networkobjects.md)
|
||||
|
||||
"Persisting NetworkObjects" is the ability to keep a network object's state when loading and unloading scenes. As well as moving an object between scenes.
|
||||
|
||||
### [Custom Scene Processors](custom-scene-processors/)
|
||||
|
||||
Fishnet has the ability for users to create their own Custom Scene Processor for loading and unloading scenes. Using Addressables for example, would need a Custom Scene Processor created.
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
---
|
||||
description: >-
|
||||
You can create a custom scene processor to handle how a scene is
|
||||
loaded/unloaded.
|
||||
---
|
||||
|
||||
# Custom Scene Processors
|
||||
|
||||
FishNet's SceneManager uses Unity's methods under the hood, you can override its functionality to add or change how it functions. This can be useful when using addressables or for making custom loading screens.
|
||||
|
||||
To start, simply create a script that inherits from the [DefaultSceneProcessor](https://fish-networking.com/FishNet/api/api/FishNet.Managing.Scened.DefaultSceneProcessor.html), add it to your network manager object, and then in the [SceneManager](../../../../fishnet-building-blocks/components/managers/scenemanager.md) component on the network manager, assign it as the [Scene Processor](../../../../fishnet-building-blocks/components/managers/scenemanager.md#scene-processor).
|
||||
|
||||
{% hint style="success" %}
|
||||
You are encouraged to check out its full API [here](https://fish-networking.com/FishNet/api/api/FishNet.Managing.Scened.DefaultSceneProcessor.html).
|
||||
{% endhint %}
|
||||
|
||||
### Addressables
|
||||
|
||||
You will need to make a custom scene processor if you want your game to make use of addressable networked scenes. Follow [this guide](addressables.md) to learn a little more.
|
||||
|
||||
### Loading Screen
|
||||
|
||||
You can use a custom scene processor to easily hook up your loading screen into FishNet's networked scene loading. There is a complete tutorial for that [here](../../../../tutorials/simple/making-a-loading-screen.md).
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
# Addressables
|
||||
|
||||
This guide assumes you know how to load and unload addressable scenes without Fish-Networking. Below describes how to use your addressables scene logic with the Fish-Networking SceneManager.
|
||||
|
||||
There are a variety of ways to load and unload addressable scenes; because of this you are provided an easy way to create your own logic for accessing addressables. To begin using addressables with the SceneManager create a new script, and inherit from **DefaultSceneProcessor**.
|
||||
|
||||
{% hint style="info" %}
|
||||
LoadOptions and UnloadOptions have an addressables boolean you may utilize to know if your scene change is using addressables.
|
||||
{% endhint %}
|
||||
|
||||
The DefaultSceneProcessor script inherits from SceneProcessorBase to implement the default scene changing functionality. Your custom script inherits from DefaultSceneProcessor because often only some of the logic needs to be changed, and this allows you to only override the differences.
|
||||
|
||||
As mentioned what you need to modify may vary, but most developers find themselves overriding the following methods.
|
||||
|
||||
```csharp
|
||||
public abstract void BeginLoadAsync(string sceneName, LoadSceneParameters parameters);
|
||||
public abstract void BeginUnloadAsync(Scene scene);
|
||||
public abstract bool IsPercentComplete();
|
||||
public abstract float GetPercentComplete();
|
||||
public abstract IEnumerator AsyncsIsDone();
|
||||
```
|
||||
|
||||
Each method contains XML documentation to provide you a better description.
|
||||
|
||||
After you have completed your implementation of DefaultSceneProcessor add your newly created component to your NetworkManager object.
|
||||
|
||||
If the _SceneManager_ component is not added to the NetworkManager object yet, add that as well. Drag your DefaultSceneProcessor implementation into the _SceneProcessor_ field on the SceneManager.
|
||||
|
||||
You are all set now! Whenever Fish-Networking loads or unloads a scene your processor will be used and you can control the logic.
|
||||
+255
@@ -0,0 +1,255 @@
|
||||
---
|
||||
description: >-
|
||||
Instructions for loading networked scenes with FishNet, both on the server and
|
||||
on clients.
|
||||
---
|
||||
|
||||
# Loading Scenes
|
||||
|
||||
{% embed url="https://www.youtube.com/watch?index=3&list=PLkx8oFug638qBthd3n_F98zAtOdI8o_Gb&v=PhWKZsL0Za4" %}
|
||||
Loading scenes video guide
|
||||
{% endembed %}
|
||||
|
||||
## General
|
||||
|
||||
This guide will go over how to setup and load new scenes, how to load into existing scenes, how to replace scenes, and advanced info on what happens behind the "scenes" during a load.
|
||||
|
||||
{% hint style="success" %}
|
||||
Having a simple online and offline scene is very to do through the built-in [DefaultScene ](../../../../fishnet-building-blocks/components/utilities/defaultscene.md)component. You can read more about using it [here](automatic-online-and-offline-scenes.md).
|
||||
{% endhint %}
|
||||
|
||||
{% hint style="info" %}
|
||||
Loading Scenes globally or by connection will add the specified clients connection as an [**Observer** ](../../observers/)to the scenes if utilizing the [**ObserverManagers**](../../../../fishnet-building-blocks/components/managers/observermanager/) Scene Condition, so globally will add all clients as Observers, and by connection will only add the connections specified. - See [**Scene Visibility**](../scene-visibility.md) for more info.
|
||||
{% endhint %}
|
||||
|
||||
{% hint style="warning" %}
|
||||
In the Examples below, all SceneManager calls are done inside a NetworkBehaviour class, that is why you can get reference to SceneManager by doing base.SceneManager.\
|
||||
If you would like a reference outside of a NetworkBehaviour consider using FishNet's [**InstanceFinder**](../../instancefinder-guides.md).SceneManager.
|
||||
{% endhint %}
|
||||
|
||||
<details>
|
||||
|
||||
<summary>Setup</summary>
|
||||
|
||||
Before calling the SceneManager's Load Scene functions you will need to setup the load data to tell the [**SceneManager**](../../../../fishnet-building-blocks/components/managers/scenemanager.md) how you want it to handle the scene load.
|
||||
|
||||
**SceneLookupData**
|
||||
|
||||
[**SceneLookupData**](../scene-data/scenelookupdata.md) is the class used to specify what scene you want the [**SceneManager**](../../../../fishnet-building-blocks/components/managers/scenemanager.md) to load. You do not need to create the lookup data manually but can instead use the SceneLoadData constructors which will create the SceneLookupData automatically.
|
||||
|
||||
**SceneLoadData**
|
||||
|
||||
When loading a scene in any way, you must pass in an instance of a [**SceneLoadData**](../../../../fishnet-building-blocks/components/managers/scenemanager.md)[ ](../scene-data/sceneloaddata.md)class into the load methods. This class provides the scene manager all of the info it needs to load the scene or scenes properly.
|
||||
|
||||
The constructors available for [**SceneLoadData**](../scene-data/sceneloaddata.md) will automatically create the [**SceneLookupData**](../scene-data/scenelookupdata.md) needed for the SceneManager to handle if you are loading a new scene, or an existing instance of one.
|
||||
|
||||
</details>
|
||||
|
||||
<details>
|
||||
|
||||
<summary>Loading New Scenes</summary>
|
||||
|
||||
Scenes can be loaded globally or for any number of specified clients, including none (which will only load the scene on the server).
|
||||
|
||||
{% hint style="info" %}
|
||||
Loading new Scenes can only be done by Name, you **cannot** use Handles or Scene References.
|
||||
{% endhint %}
|
||||
|
||||
**Global Scenes**
|
||||
|
||||
* Global Scenes can be loaded by calling `LoadGlobalScenes()` in the SceneManager.
|
||||
* When loaded globally, scenes will be loaded for all current, and future clients.
|
||||
|
||||
```csharp
|
||||
SceneLoadData sld = new SceneLoadData("Town");
|
||||
base.SceneManager.LoadGlobalScenes(sld);
|
||||
```
|
||||
|
||||
**Connection Scenes**
|
||||
|
||||
Connection Scenes follow the same principle but have a few method overloads.
|
||||
|
||||
* You can load scenes for a single connection, multiple connections at once, or load scenes only on the server in preparation for connections.
|
||||
* When loading by connection only the connections specified will load the scenes.
|
||||
* You can add additional connections into a scene at any time.
|
||||
|
||||
```csharp
|
||||
SceneLoadData sld = new SceneLoadData("Main");
|
||||
|
||||
// Load scenes for a single connection.
|
||||
NetworkConnection conn = base.Owner;
|
||||
base.SceneManager.LoadConnectionScenes(conn, sld);
|
||||
|
||||
// Load scenes for several connections at once.
|
||||
NetworkConnection[] conns = new NetworkConnection[] { connA, connB };
|
||||
base.SceneManager.LoadConnectionScenes(conns, sld);
|
||||
|
||||
// Load scenes only on the server. This can be used to preload scenes
|
||||
// that you don't want all players in.
|
||||
base.SceneManager.LoadConnectionScenes(sld);
|
||||
```
|
||||
|
||||
**Loading Multiple Scenes**
|
||||
|
||||
* Whether loading globally or by connection, you can load more than one scene in a single method call.
|
||||
* When loading multiple scenes in one call, the NetworkObjects you put into [**Moved NetworkObjects**](../scene-data/sceneloaddata.md#movednetworkobjects) will be moved to the first valid scene in the list of scenes you tried to load. See Persisting NetworkObjects for more info about keeping NetworkObjects across scenes.
|
||||
|
||||
```csharp
|
||||
// Loading Multiple Connections into Multiple Scenes
|
||||
string[] scenesToLoad = new string[] {"Main", "Additive"};
|
||||
NetworkConnection[] conns = new NetworkConnection[] {connA, connB,connC}
|
||||
|
||||
SceneLoadData sld = new SceneLoadData(scenesToLoad);
|
||||
base.SceneManager.LoadConnectionScenes(conns, sld);
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
<details>
|
||||
|
||||
<summary>Loading Existing Scenes</summary>
|
||||
|
||||
If the scene is already loaded on the server, and you want to load clients into that instance of the scene. Most likely you will want to lookup that scene by scene reference, or handle to make sure you are getting the exact scene you need.
|
||||
|
||||
If you load the scene by name, it will load the connections into the first scene found with that name. If you are utilizing [**Scene Stacking**](../scene-stacking.md), then there may be multiple scenes loaded with the same name. So be alert when loading into existing scenes by name.
|
||||
|
||||
You can load clients into scenes that have no other clients in them if you are utilizing [**Scene Caching**](../scene-caching.md) **-** the ability to keep a scene loaded with its current state on the server when all clients leave the scene.
|
||||
|
||||
**Getting References to a Loaded Scene**
|
||||
|
||||
Here are a few ways to get reference to the scenes that you already loaded using FishNet's **SceneManager**.
|
||||
|
||||
**By Event:**
|
||||
|
||||
```csharp
|
||||
// Manage your own collection of SceneReferences/Handles
|
||||
// Customize how you want to manage you scene references so it's easy
|
||||
// for you to find them later.
|
||||
List<Scene> ScenesLoaded = new();
|
||||
|
||||
public void OnEnable()
|
||||
{
|
||||
InstanceFinder.SceneManager.OnLoadEnd += RegisterScenes;
|
||||
}
|
||||
|
||||
public void RegisterScenes(SceneLoadEndEventArgs args)
|
||||
{
|
||||
// Only register on the server
|
||||
if (!args.QueueData.AsServer) return;
|
||||
|
||||
// if you know you only loaded one scene you could just grab index [0]
|
||||
foreach(var scene in args.loadedScenes)
|
||||
ScenesLoaded.Add(scene);
|
||||
}
|
||||
|
||||
public void OnDisable()
|
||||
{
|
||||
InstanceFinder.SceneManager.OnLoadEnd -= RegisterScene;
|
||||
}
|
||||
```
|
||||
|
||||
**By Connection:**
|
||||
|
||||
```csharp
|
||||
// NetworkConnections have a list of Scenes they are currently in.
|
||||
int clientToLookup;
|
||||
InstanceFinder.ServerManger.Clients[clientToLookup].Scenes;
|
||||
```
|
||||
|
||||
**By SceneManager.SceneConnnections:**
|
||||
|
||||
```csharp
|
||||
// SceneManager keeps a Dictionary of all connection scenes as the key
|
||||
// and the client connections that are in that scene as the value.
|
||||
NetworkConnection conn;
|
||||
Scene sceneNeeded;
|
||||
|
||||
// Get the scene you need with foreach or use LINQ to filter your conditions.
|
||||
foreach(var pair in SceneManager.SceneConnections)
|
||||
{
|
||||
if (pair.Value.Contains(conn))
|
||||
sceneNeeded = pair.Key;
|
||||
}
|
||||
```
|
||||
|
||||
**Using Reference to Load Into Existing Instance**
|
||||
|
||||
Use the methods above to get the reference or handle of a scene, and use that reference or handle to load a client into an existing scene.
|
||||
|
||||
```csharp
|
||||
scene sceneReference;
|
||||
NetworkConnection[] conns = new() {connA,connB};
|
||||
|
||||
// by reference
|
||||
SceneLoadData sld = new(sceneReference);
|
||||
base.SceneManager.LoadConnectionScenes(conns, sld);
|
||||
|
||||
// by handle
|
||||
SceneLoadData sld = new(sceneReference.handle);
|
||||
base.SceneManager.LoadConnectionScenes(conns, sld);
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
<details>
|
||||
|
||||
<summary>Replacing Scenes</summary>
|
||||
|
||||
Fishnet gives the ability to replace scenes that are already loaded on the clients with the new requested scenes to load.
|
||||
|
||||
To Replace Scenes, you will set the ReplaceScene Option in the SceneLoadData
|
||||
|
||||
Replaced scenes will be unloaded before the new scenes are loaded.
|
||||
|
||||
Replacing Scenes by Default will replace scenes on both the server and clients. If you would like the server to keep the scene loaded and only replace the scene on the clients - see [**Scene Caching**](../scene-caching.md) for more details.
|
||||
|
||||
**Replace None:**
|
||||
|
||||
This is the default method when loading, it will ignore the replace options and load the scene in normally.
|
||||
|
||||
**Replace All:**
|
||||
|
||||
This will replace all scenes currently loaded in unity, even ones not managed by FishNet's SceneManager.
|
||||
|
||||
```csharp
|
||||
// Replace All Option.
|
||||
SceneLoadData sld = new SceneLoadData("DungeonScene");
|
||||
sld.ReplaceScenes = ReplaceOption.All;
|
||||
|
||||
// This will replace all Scenes loaded by FishNet or outside of FishNet like Unity,
|
||||
// and load "DungeonScene"
|
||||
SceneManager.LoadGlobalScenes(sld);
|
||||
```
|
||||
|
||||
**Replace Online Only:**
|
||||
|
||||
This will replace only scenes managed by the SceneManager in FishNet.
|
||||
|
||||
```csharp
|
||||
// Replace Online Only Option.
|
||||
SceneLoadData sld = new SceneLoadData("DungeonScene");
|
||||
sld.ReplaceScenes = ReplaceOption.OnlineOnly;
|
||||
|
||||
// This will replace only scenes managed by the SceneManager in FishNet.
|
||||
SceneManager.LoadGlobalScenes(sld);
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
<details>
|
||||
|
||||
<summary>Advanced Info</summary>
|
||||
|
||||
**Behind the "Scenes"**
|
||||
|
||||
The [**SceneManager**](../../../../fishnet-building-blocks/components/managers/scenemanager.md) Class has very detailed XML comments on how the load process works in detail, if you need to troubleshoot the scene load process, these comments will help you understand the flow of how a scene loads.
|
||||
|
||||
**Events**
|
||||
|
||||
Make sure to check out the [**Scene Events**](../scene-events.md) that you can subscribe to in order to have better control over your game.
|
||||
|
||||
**More Control**
|
||||
|
||||
If you want more control over how FishNet loads and unloads scenes, you can create a custom scene processor to override FishNet's functionality. Learn more about that [here](../custom-scene-processors/).
|
||||
|
||||
</details>
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
---
|
||||
description: Using the DefaultScene component to automatically manage simple scene setups.
|
||||
---
|
||||
|
||||
# Automatic Online and Offline Scenes
|
||||
|
||||
If you're looking for a simple way to manage scene transitions when your network starts or stops, FishNet has an easy way to manage it without any custom code. The [**DefaultScene**](../../../../fishnet-building-blocks/components/utilities/defaultscene.md) component is a ready-made solution that automatically loads your designated **online scene** when the network starts, and your **offline scene** when the network shuts down.
|
||||
|
||||
This is perfect for developers who want a plug-and-play setup without writing custom scene logic.
|
||||
|
||||
### Setup instructions
|
||||
|
||||
To get started:
|
||||
|
||||
1. Add the `DefaultScene` component to your **NetworkManager** GameObject.
|
||||
2. In the Inspector, assign your **Offline Scene** and **Online Scene**.
|
||||
3. Make sure the two scenes are different — using the same scene for both will prevent the component from functioning properly.
|
||||
|
||||
{% hint style="success" %}
|
||||
Read more about the component here: [defaultscene.md](../../../../fishnet-building-blocks/components/utilities/defaultscene.md "mention")
|
||||
{% endhint %}
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
---
|
||||
description: >-
|
||||
This page will go over details on the options available to users for
|
||||
persisting NetworkObjects across scene loading and unloading
|
||||
---
|
||||
|
||||
# Persisting NetworkObjects
|
||||
|
||||
## General
|
||||
|
||||
The options available to users to keep [**NetworkObjects**](../networked-gameobjects-and-scripts/networkobjects/) persisting across scenes depends on the type of NetworkObject.
|
||||
|
||||
## Spawned NetworkObjects
|
||||
|
||||
[**Spawned NetworkObjects**](../networked-gameobjects-and-scripts/networkobjects/#spawnednetworkobject) that do not fall into the other categories below can persist between scenes by moving them while loading into the next scene or moving them manually on the server and then updating the observers.
|
||||
|
||||
#### Moving objects when loading the scene
|
||||
|
||||
The [**SceneLoadData**](scene-data/sceneloaddata.md) that is passed into the Load Method of the SceneManager has an array that you can populate with all of the Spawned NetworkObjects you want to send to the new loaded scene.
|
||||
|
||||
The SceneManager will handle the objects correctly for you and also flag a Debug if you try to send a GameObject that is not allowed.
|
||||
|
||||
{% hint style="warning" %}
|
||||
If you load Multiple Scenes in one method call, and you are moving network objects using SceneLoadData. The moved networkobjects will move into the first valid scene requested.
|
||||
{% endhint %}
|
||||
|
||||
#### Example
|
||||
|
||||
```csharp
|
||||
// Just create an array of NetworkObjects
|
||||
// that are not a Scene, Global or Nested NetworkObjects.
|
||||
NetworkObject[] objectsToMove = new NetworkObject[] { object1, object2, object3 }
|
||||
|
||||
// Assign this array to the SceneLoadData before you Load a Scene.
|
||||
SceneLoadData sld = new SceneLoadData("NewScene");
|
||||
sld.MovedNetworkObjects = objectsToMove;
|
||||
|
||||
// FishNet will handle the rest after loading!
|
||||
SceneManager.LoadGlobalScenes(sld);
|
||||
```
|
||||
|
||||
#### Moving objects manually on the server
|
||||
|
||||
You can also manually move such an object by using Unity's `SceneManager.MoveGameObjectToScene` method and then rebuilding FishNet's observers `ServerManager.Objects.RebuildObservers`. 
|
||||
|
||||
Both of these should only be done on the server.
|
||||
|
||||
#### Example
|
||||
|
||||
```csharp
|
||||
// Use Unity's SceneManager to move the specific object to a specifc scene.
|
||||
UnityEngine.SceneManagement.SceneManager.MoveGameObjectToScene(theGameObject, theScene);
|
||||
// Then you can tell FishNet to rebuild the observers.
|
||||
InstanceFinder.ServerManager.Objects.RebuildObservers();
|
||||
```
|
||||
|
||||
## Scene NetworkObjects
|
||||
|
||||
[**Scene NetworkObjects**](../networked-gameobjects-and-scripts/networkobjects/#scenenetworkobject) currently cannot persist across scenes, it is a [**limitation**](../../troubleshooting/technical-limitations.md) with the way Unity and FishNet was designed. You cannot mark them as IsGlobal, or put them into the "DontDestroyOnLoad" scene. If you would like a Scene NetworkObject to persist across scenes it is recommended to remove them and use the other options available on this page.
|
||||
|
||||
## Global NetworkObjects
|
||||
|
||||
<mark style="color:blue;">**G**</mark>[<mark style="color:blue;">**lobal NetworkObjects**</mark>](../networked-gameobjects-and-scripts/networkobjects/#globalnetworkobject) work similar to how a normal GameObject would when put into the "DontDestroyOnLoad" (DDOL) scene.\
|
||||
\
|
||||
When loading and unloading scenes, **Global NetworkObjects** will stay in the (DDOL) scene on both the server and client persisting their state. No extra steps needed.
|
||||
|
||||
To make a NetworkObject global just mark the IsGlobal Boolean "true" on the NetworkObject component.
|
||||
|
||||
{% hint style="info" %}
|
||||
Clients typically are always an observer of the DDOL scene, so global objects may make more sense for Manager type GameObjects, instead of gameobjects that have meshes, but you are not limited to this.
|
||||
{% endhint %}
|
||||
|
||||
## Nested NetworkObjects
|
||||
|
||||
Unity will not allow [**Nested GameObjects**](../networked-gameobjects-and-scripts/networkobjects/nested-networkobjects.md) to be moved into other scenes.\
|
||||
\
|
||||
However! Fishnet will automatically detect if you are trying to send a NestedNetworkObject and send the root of the object instead!
|
||||
@@ -0,0 +1,41 @@
|
||||
---
|
||||
description: >-
|
||||
Scene Caching is the ability for the Server to keep a scene loaded when either
|
||||
all clients have unloaded that scene, or stopped observing that scene.
|
||||
---
|
||||
|
||||
# Scene Caching
|
||||
|
||||
## General
|
||||
|
||||
When loading a scene you can specify in the SceneLoadData whether to AutomaticallyUnload the scene when all Clients unload or leave the scene as an observer.\
|
||||
\
|
||||
When Unloading the Scene you can override the AutomaticallyUnload options by setting the ServerUnloadMode.
|
||||
|
||||
## Examples
|
||||
|
||||
```csharp
|
||||
// When Loading Scenes.
|
||||
SceneLoadData sld = new SceneLoadData("MainScene");
|
||||
sld.Options.AutomaticallyUnload = false;
|
||||
|
||||
// When Manually Unloading Scenes.
|
||||
// Whatever you set the ServerUnloadMode to here will override the AutomaticallyUnload
|
||||
// setting you used when loading the scene earlier.
|
||||
SceneUnloadData sud = new SceneUnloadData("MainScene");
|
||||
sud.Options.Mode = ServerUnloadMode.KeepUnused;
|
||||
```
|
||||
|
||||
## Host behaviour
|
||||
|
||||
In situations where the Hosts Server needs to keep a scene loaded, but the Hosts Client was requested to unload that scene. Instead of unloading, the Hosts Client will be removed from the scene using the observer system updating the Clients [**Scene Visibility**](scene-visibility.md).\
|
||||
\
|
||||
As Host, the Server and Client share the same instance of loaded Scenes and Game Objects. If it were to actually unload a scene from the Host Client, it would also unload on the server.
|
||||
|
||||
{% hint style="danger" %}
|
||||
A **Scene Condition** must be set in the **ObserverManager** for you to utilize the ability to have the server keep a scene loaded, and the hosts client not see the objects in that scene.
|
||||
{% endhint %}
|
||||
|
||||
{% hint style="warning" %}
|
||||
This usually means that every GameObject with a mesh will have to have a NetworkObject attached if you want the Host Client not to visualize the scene.
|
||||
{% endhint %}
|
||||
@@ -0,0 +1,19 @@
|
||||
---
|
||||
description: >-
|
||||
Scene Data is the data types used that the user will interface with when using
|
||||
the SceneManager.
|
||||
---
|
||||
|
||||
# Scene Data
|
||||
|
||||
{% embed url="https://www.youtube.com/watch?index=2&list=PLkx8oFug638qBthd3n_F98zAtOdI8o_Gb&v=T23OiTKy500" %}
|
||||
SceneLoadData video guide
|
||||
{% endembed %}
|
||||
|
||||
## General
|
||||
|
||||
See Sub-Pages below for the different Scene Data Classes.
|
||||
|
||||
* [**SceneLookupData**](scenelookupdata.md)
|
||||
* [**SceneLoadData**](sceneloaddata.md)
|
||||
* [**SceneUnloadData**](sceneunloaddata.md)
|
||||
+125
@@ -0,0 +1,125 @@
|
||||
---
|
||||
description: >-
|
||||
The Data Class needed for the SceneManager to know how to handle loading a
|
||||
scene.
|
||||
---
|
||||
|
||||
# SceneLoadData
|
||||
|
||||
## General
|
||||
|
||||
Loading scenes of all types depend upon SceneLoadData. The SceneLoadData class contains information about what scenes to load, how to load them, which objects to move to new scenes, and more. You can view the SceneLoadData API [here](https://fish-networking.com/FishNet/api/api/FishNet.Managing.Scened.SceneLoadData.html).
|
||||
|
||||
## Default values
|
||||
|
||||
```csharp
|
||||
SceneLoadData sld = new SceneLoadData()
|
||||
{
|
||||
PreferredActiveScene = null,
|
||||
SceneLookupDatas = new SceneLookupData[0],
|
||||
MovedNetworkObjects = new NetworkObject[0],
|
||||
ReplaceScenes = ReplaceOption.None,
|
||||
Params = new LoadParams()
|
||||
{
|
||||
ServerParams = new object[0],
|
||||
ClientParams = new byte[0]
|
||||
},
|
||||
Options = new LoadOptions()
|
||||
{
|
||||
AutomaticallyUnload = true,
|
||||
AllowStacking = false,
|
||||
LocalPhysics = LocalPhysicsMode.None,
|
||||
ReloadScenes = false, // Obsolete, Coming Soon.
|
||||
Addressables = false
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
<details>
|
||||
|
||||
<summary>PreferredActiveScene</summary>
|
||||
|
||||
Preferred Active Scene will allow you to choose what scene will be active on the server and/or client. You can choose to have the chosen SceneLookupData apply to both the server and client or separate ones for each.
|
||||
|
||||
If left with the default value of null, the first valid scene loaded will become the ActiveScene.
|
||||
|
||||
</details>
|
||||
|
||||
<details>
|
||||
|
||||
<summary>SceneLookupDatas</summary>
|
||||
|
||||
This Array is populated with scenes you would like to load, depending on the parameters you pass into the SceneLoadData when constructed.
|
||||
|
||||
See [**Loading Scenes**](../loading-scenes/) for examples.
|
||||
|
||||
</details>
|
||||
|
||||
<details>
|
||||
|
||||
<summary>MovedNetworkObjects</summary>
|
||||
|
||||
Instantiated NetworkObjects can be moved when loading new scenes, such as if you want to move a player to a different scene as you load the new scene. You may include an array of NetworkObjects to move to the new scenes. NetworkObjects within this array will be moved to the first scene specified in SceneLookupData.\
|
||||
\
|
||||
See [**Persisting NetworkObjects**](../persisting-networkobjects.md) for more details on what type of NetworkObjects you are allowed to move.
|
||||
|
||||
</details>
|
||||
|
||||
<details>
|
||||
|
||||
<summary>ReplaceScenes</summary>
|
||||
|
||||
Like the Unity SceneManager when loading a single scene, ReplaceScenes allows you to replace currently loaded scenes with new ones. There are a variety of options to use.\
|
||||
\
|
||||
See [**Replacing Scenes**](../loading-scenes/#replacing-scenes) Section of Loading Scenes guide for more details and examples.
|
||||
|
||||
</details>
|
||||
|
||||
<details>
|
||||
|
||||
<summary>Params</summary>
|
||||
|
||||
Params are an optional way to assign data to your scene loads/unloads. This data will be available within[ **Scene Events**](../scene-events.md), Information used in Params can be useful for storing information about the scene load/unload and referencing it later when the scene load/unload completes.
|
||||
|
||||
**ServerParams**
|
||||
|
||||
_ServerParams_ are only included on the server side, and are not networked. It is an array of objects, meaning you can send anything you want. However when accessing the Params through event args, you will have to cast the object to the data you want.
|
||||
|
||||
**ClientParams**
|
||||
|
||||
_ClientParams_ is a byte array which may contain anything, and will be sent to clients when they receive the load scene instructions. Clients can access the _ClientParams_ within the scene change events.
|
||||
|
||||
</details>
|
||||
|
||||
<details>
|
||||
|
||||
<summary>Options</summary>
|
||||
|
||||
You may further enhance how you load/unload scenes with Options.
|
||||
|
||||
**AutomaticallyUnload**
|
||||
|
||||
* When _set to_ true scenes will be unloaded automatically on the server when there are no more connections present. This is the default behaviour.
|
||||
* When set to false the scene will remain if connections leave the scene unexpected, such as being disconnected.
|
||||
* However, discussed in UnloadSceneData, this behavior can be overriden using the UnloadOptions of UnloadSceneData.
|
||||
* Only scenes loaded for connections will be automatically unloaded when emptied.
|
||||
* Global scenes can only be unloaded using ReplaceScenes or by calling unload on them.
|
||||
|
||||
**AllowStacking**
|
||||
|
||||
* When _AllowStacking_ remains false the SceneManager will not stack scenes in your SceneLoadDatas.
|
||||
* If true, then scenes can be stacked (loaded multiple times).
|
||||
* In the SceneLookupData section it was mentioned that if a Scene reference or handle is specified then the SceneManager will favor loading a scene using a scene handle. When you would like to load connections into the same stacked scene over multiple load calls, you will populate your SceneLookupDatas by Scene reference or handle.
|
||||
* See [**Scene Stacking**](../scene-stacking.md) for more detail and examples
|
||||
|
||||
**LocalPhysics**
|
||||
|
||||
* [_LocalPhysics_](https://docs.unity3d.com/ScriptReference/SceneManagement.LocalPhysicsMode.html) is a Unity property that lets you determine how physics are simulated in your scenes.
|
||||
* Generally, if you are stacking scenes you will want to set a LocalPhysics mode so that stacked scenes do not collide with each other.
|
||||
|
||||
**Addressables**
|
||||
|
||||
* _Addressables_ is only used as a reference and performs no additional functionality.
|
||||
* You may set this value to know if a scene is loading using addressables, without having to create Params.
|
||||
|
||||
</details>
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
---
|
||||
description: >-
|
||||
SceneLookupData is how the server determines to load clients into a new
|
||||
instance of a Scene, or load a client into a scene that the server already has
|
||||
loaded.
|
||||
---
|
||||
|
||||
# SceneLookupData
|
||||
|
||||
## General
|
||||
|
||||
{% hint style="warning" %}
|
||||
When Creating [**SceneLoadData**](sceneloaddata.md) or [**SceneUnloadData**](sceneunloaddata.md) there are provided constructors that automatically create SceneLookupData for you. Most likely you will be using these constructors and will not be creating a separate SceneLookupData.
|
||||
{% endhint %}
|
||||
|
||||
When you specify a scene by reference, or handle, the SceneManager will prefer to lookup that scene using the scene handle. This is important information when [**Scene Stacking**](../scene-stacking.md). Looking up a scene by handle or object reference will place connections in the scene specified, but when using scene names, the server will create a new scene instance for the specified connections and place them into that scene. The described behavior only applies when loading scenes over multiple load calls; such as if you call LoadConnectionScene twice, each with its own connection.
|
||||
|
||||
## Default values
|
||||
|
||||
{% hint style="info" %}
|
||||
A Scene Handle is an integer that Unity generates for a scene instance when it is loaded. You can use it to specify an instance of a scene that is already loaded, but not for loading a new instance of that scene. Read more about it here: [https://docs.unity3d.com/ScriptReference/SceneManagement.Scene-handle.html](https://docs.unity3d.com/ScriptReference/SceneManagement.Scene-handle.html)
|
||||
{% endhint %}
|
||||
|
||||
```csharp
|
||||
// SceneLookupData default values
|
||||
SceneLookupData slud = new SceneLookupData()
|
||||
{
|
||||
// If Handle is greater than 0 then it will ignore Name and use Handle
|
||||
// to look up the scene.
|
||||
Handle = 0,
|
||||
// If Handle is set to 0, then Name is used to lookup the scene instead.
|
||||
Name = null
|
||||
};
|
||||
```
|
||||
|
||||
See[ **Loading Scenes**](../loading-scenes/) and [**Unloading Scenes**](../unloading-scenes.md) for implementation.
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
---
|
||||
description: >-
|
||||
The Data Class needed for the SceneManager to know how to handle unloading a
|
||||
scene.
|
||||
---
|
||||
|
||||
# SceneUnloadData
|
||||
|
||||
## General
|
||||
|
||||
When unloading scenes information on what to unload is constructed within the SceneUnloadData class. SceneUnloadData is very similar to SceneLoadData. The API for SceneUnloadData can be found [here](https://fish-networking.com/FishNet/api/api/FishNet.Managing.Scened.SceneUnloadData.html).
|
||||
|
||||
## Default values
|
||||
|
||||
```csharp
|
||||
SceneUnloadData sud = new SceneUnloadData()
|
||||
{
|
||||
SceneLookupDatas = new SceneLookupData[0],
|
||||
Params = new UnloadParams()
|
||||
{
|
||||
ServerParams = new object[0],
|
||||
ClientParams = new byte[0]
|
||||
},
|
||||
Options = new UnloadOptions()
|
||||
{
|
||||
Mode = ServerUnloadMode.UnloadUnused,
|
||||
Addressables = false
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
<details>
|
||||
|
||||
<summary>SceneLookupDatas</summary>
|
||||
|
||||
This Array is populated with the scenes you want to unload, depending on the parameters you pass into the SceneUnloadData when constructed.
|
||||
|
||||
See [**Unloading Scenes**](../unloading-scenes.md) for examples.
|
||||
|
||||
</details>
|
||||
|
||||
<details>
|
||||
|
||||
<summary>Params</summary>
|
||||
|
||||
Params are an optional way to assign data to your scene loads/unloads. This data will be available within[ **Scene Events**](../scene-events.md), Information used in Params can be useful for storing information about the scene load/unload and referencing it later when the scene load/unload completes.
|
||||
|
||||
**ServerParams**
|
||||
|
||||
_ServerParams_ are only included on the server side and are not networked. It is an array of objects, meaning you can send anything you want. However when accessing the Params through event args, you will have to cast the object to the data you want.
|
||||
|
||||
**ClientParams**
|
||||
|
||||
_ClientParams_ is a byte array which may contain anything. It will be sent to clients when they receive the load scene instructions. Clients can access the _ClientParams_ within the scene change events.
|
||||
|
||||
</details>
|
||||
|
||||
<details>
|
||||
|
||||
<summary>Options</summary>
|
||||
|
||||
Like with Options in loading, the UnloadOptions offer additional settings when unloading.
|
||||
|
||||
**Mode**
|
||||
|
||||
These values will override the AutomaticallyUnload Option that was used when loaded the scene. If you set _AutomaticallyUnload_ to false but specified _ServerUnloadModes.UnloadUnused_ then the scene would be unloaded when emptied.
|
||||
|
||||
**ServerUnloadModes.UnloadUnused**
|
||||
|
||||
* This is the default setting which will only unload a scene which is no longer used.
|
||||
|
||||
**ServerUnloadModes.KeepUnused**
|
||||
|
||||
* This option will keep the scene loaded on the server if all clients have been removed. See [**Scene Caching**](../scene-caching.md) for more details
|
||||
|
||||
</details>
|
||||
@@ -0,0 +1,63 @@
|
||||
---
|
||||
description: >-
|
||||
Information about FishNet's many available scene events, the order they are
|
||||
invoked in, and how to use them.
|
||||
---
|
||||
|
||||
# Scene Events
|
||||
|
||||
{% embed url="https://www.youtube.com/watch?index=1&list=PLkx8oFug638qBthd3n_F98zAtOdI8o_Gb&v=c_kdZELyJcQ" %}
|
||||
Scene events video guide
|
||||
{% endembed %}
|
||||
|
||||
## General
|
||||
|
||||
There are a variety of events within SceneManager to help with your development. The order in which events are invoked is reliable.
|
||||
|
||||
This diagram represents which order events can be expected to run. You may also find this diagram within your Fish-Networking import under Example/All/SceneManager.
|
||||
|
||||

|
||||
|
||||
{% hint style="info" %}
|
||||
Notice that client and server process queues and events exactly the same. OnClientPresence change is a special exception, as discussed below.
|
||||
{% endhint %}
|
||||
|
||||
## Events
|
||||
|
||||
### OnClientLoadedStartScenes
|
||||
|
||||
This event calls every time a client loads the starting scenes for the first time. The start scenes would be any global scenes you have set, or if no global scenes are set then the scene you are entering play into. The PlayerSpawner.cs example demonstrates usage of the **OnClientLoadedStartScenes** event. Included is the connecion, or client which loaded the scenes, and a boolean indicating true if the callback is on the server side or false if on the client side.
|
||||
|
||||
The NetworkConnection class also has a similar event **OnLoadedStartScenes** if you'd like to know when only a specific connection has loaded the start scenes.
|
||||
|
||||
### OnQueueStart, OnQueueEnd
|
||||
|
||||
**OnQueueStart** and **OnQueueEnd** are called when a scene change queue occurs. **Start** will only call if a scene has succesfully begun to load or unload. The queue may process any number of scene events. For example: if a scene is told to unload while a load is still in progress, then the unload will be placed in the queue. **OnQueueEnd** will invoke after both the load and unload have completed.
|
||||
|
||||
### OnLoadStart, OnUnloadStart
|
||||
|
||||
**OnLoadStart** and **OnUnloadStart** occur before a queue entry is processed. These will call for every entry in a queue. For example: if you call SceneManager.LoadGlobalScenes() twice in a row, **OnQueueStart** will invoke once, and OnLoadStart will invoke twice. **OnLoadStart** will be called when a scene load begins, and **OnUnloadStart** when an unload begins.
|
||||
|
||||
Both the Load and Unload events contain a structure which has a field labeled _QueueData._ _QueueData_ holds information about the queue entry. The provided data is exposed for your convenience.
|
||||
|
||||
### OnLoadPercentChange
|
||||
|
||||
Only available when loading scenes is the **OnLoadPercentChange** event. This event contains information about what percentage of your scenes have loaded for the queue entry. Like the Start events, _QueueData_ is provided. You will also be supplied a float _Percent_, indicating the total progress of your scene load. This can be useful to show loading screens, or perform a variety of initialization tasks.
|
||||
|
||||
### OnLoadEnd
|
||||
|
||||
The **OnLoadEnd** event is called after all scenes for the queue entry have been loaded. This event will only invoke after the scenes have fully loaded, and after the active scene has been set if applicable. Like the **OnLoadStart** event _QueueData_ is provided. Additionally, _LoadedScenes_ and _SkippedSceneNames. LoadedScenes_ provides Scene references to which scenes were loaded. _SkippedSceneNames_ contains strings of scenes which were not loaded; this generally occurs if the scene is already loaded.
|
||||
|
||||
{% hint style="warning" %}
|
||||
You may want to spawn objects for players once a scene is loaded, but it's best to do that once the specific client has started observing the scene to prevent race conditions. Thus it's better to use one of the OnClientPresenceChange events or OnSpawnServer from a NetworkBehaviour in the scene.
|
||||
{% endhint %}
|
||||
|
||||
### OnUnloadEnd
|
||||
|
||||
After scene unloading has occurred **OnUnloadEnd** is invoked. This event contains _QueueData_, and _UnloadedSceneHandles_. As before _QueueData_ contains information about the queue entry. _UnloadSceneHandles_ is a collection occupied with the handles of which scenes were unloaded.
|
||||
|
||||
### OnClientPresenceChangeStart, OnClientPresenceChangeEnd
|
||||
|
||||
These two events are only available on the server and indicate when a client is being added to a scene, or being removed from a scene. Both events are only invoked after the client has fully loaded or unloaded the scene. The **Start** variant is invoked before the [observer](../../../fishnet-building-blocks/components/network-observer.md) status has been updated for the client, and **End** after being updated. To clarify, if a client joins a scene **OnClientPresentStart** will call before the client has visibility of any networked content in the scene, and **OnClientPresentEnd** after the client has gained visibility.
|
||||
|
||||
Both events contain the same structure which has the following information: _Scene_, _Connection,_ and _Added._ _Scene_ is which scene the client is being added or removed from. _Connection_ is the NetworkConnection for the client. Lastly, _Added_ will be true if being added to the scene, or false is being removed.
|
||||
@@ -0,0 +1,140 @@
|
||||
---
|
||||
description: >-
|
||||
Scene Stacking is the ability for server or host to load multiple instances of
|
||||
the same scene at once, usually with different clients/observers in each
|
||||
scene.
|
||||
---
|
||||
|
||||
# Scene Stacking
|
||||
|
||||
## General
|
||||
|
||||
A good example of stacking scenes is having dungeon instances on a single server.\
|
||||
\
|
||||
If two clients went into the same dungeon, each client will load their own copy of that dungeon and have individual GameObjects, and state for those scenes. The Server however has two instances of that scene loaded at once. The Server having those two instances of the same scene loaded is called "Scene Stacking"\
|
||||
\
|
||||
Stacked scenes typically have different Clients observing each instance of the scene.
|
||||
|
||||
## Stacking scenes
|
||||
|
||||
### Loading into new stacked scene
|
||||
|
||||
* To stack scenes you must set _AllowStacking_ to true in your SceneLoadData's [**Options**](scene-data/sceneloaddata.md#options-1).
|
||||
* To create a new instance of a stacked scene the SceneLookupData must be populated by using the scene name. There is more information on this in the [SceneLookupData](scene-data/scenelookupdata.md) section.
|
||||
* Global Scenes cannot be stacked!
|
||||
|
||||
```csharp
|
||||
// Stacking a new Scene
|
||||
// Select Connections to load into new stacked scene.
|
||||
NetworkConnection[] conns = new NetworkConnection[]{connA,ConnB};
|
||||
|
||||
// You must use the scene name to stack scenes!
|
||||
SceneLoadData sld = new SceneLoadData("DungeonScene");
|
||||
|
||||
// Set AllowStacking Option to true.
|
||||
sld.Options.AllowStacking = true;
|
||||
|
||||
// Decide if you want seperate Physics for the scene.
|
||||
sld.Options.LocalPhysics = LocalPhysicsMode.Physics3D;
|
||||
|
||||
// load the Scene via connections, you cannot stack global scenes.
|
||||
base.SceneManager.LoadConnectionScene(conns,sld);
|
||||
```
|
||||
|
||||
### Loading into existing stacked scene
|
||||
|
||||
* If you were to load two connections into a scene by Scene reference or handle they will be added to the same scene, regardless if _AllowStacking_ is true or not.
|
||||
|
||||
This is identical to the examples given in the Loading Scenes Guide, on how to load into existing scenes. Review it [**here**](loading-scenes/#loading-existing-scenes).
|
||||
|
||||
## Separating physics
|
||||
|
||||
You may want to separate physics while stacking scenes. This ensures that the stacked scenes physics do not interact with each other. You will want to set the LocalPhysics option in the [**SceneLoadData**](scene-data/sceneloaddata.md)**.**\\
|
||||
|
||||
#### **LocalPhysicsMode.None**
|
||||
|
||||
This is the Default Option, Scene Physics will collide with other scenes in this state.
|
||||
|
||||
#### **LocalPhysicsMode.Physics2D**
|
||||
|
||||
A local 2D physics Scene will be created and owned by the Scene.
|
||||
|
||||
#### LocalPhysicsMode.Physics3D
|
||||
|
||||
A local 3D physics Scene will be created and owned by the Scene.
|
||||
|
||||
If you are to use separate physics scenes and you want to also simulate physics within them you must do so manually; this is intentional design.
|
||||
|
||||
Below is a script which you can place in your stacked physics scenes to also simulate physics along with the default physics scenes.
|
||||
|
||||
```csharp
|
||||
using FishNet.Object;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// Synchronizes scene physics if not the default physics scene.
|
||||
/// </summary>
|
||||
public class PhysicsSceneSync : NetworkBehaviour
|
||||
{
|
||||
/// <summary>
|
||||
/// True to synchronize physics 2d.
|
||||
/// </summary>
|
||||
[SerializeField]
|
||||
private bool _synchronizePhysics2D;
|
||||
/// <summary>
|
||||
/// True to synchronize physics 3d.
|
||||
/// </summary>
|
||||
[SerializeField]
|
||||
private bool _synchronizePhysics;
|
||||
/// <summary>
|
||||
/// Scenes which have physics handled by this script.
|
||||
/// </summary>
|
||||
private static HashSet<int> _synchronizedScenes = new HashSet<int>();
|
||||
|
||||
public override void OnStartNetwork()
|
||||
{
|
||||
/* If scene is already synchronized do not take action.
|
||||
* This means the script was added twice to the same scene. */
|
||||
int sceneHandle = gameObject.scene.handle;
|
||||
if (_synchronizedScenes.Contains(sceneHandle))
|
||||
return;
|
||||
|
||||
/* Set to synchronize the scene if either 2d or 3d
|
||||
* physics scene differ from the defaults. */
|
||||
_synchronizePhysics = (gameObject.scene.GetPhysicsScene() != Physics.defaultPhysicsScene);
|
||||
_synchronizePhysics2D = (gameObject.scene.GetPhysicsScene2D() != Physics2D.defaultPhysicsScene);
|
||||
|
||||
/* If to synchronize 2d or 3d manually then
|
||||
* register to pre physics simulation. */
|
||||
if (_synchronizePhysics || _synchronizePhysics2D)
|
||||
{
|
||||
_synchronizedScenes.Add(sceneHandle);
|
||||
base.TimeManager.OnPrePhysicsSimulation += TimeManager_OnPrePhysicsSimulation;
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnStopNetwork()
|
||||
{
|
||||
// Check to unsubscribe.
|
||||
if (_synchronizePhysics || _synchronizePhysics2D)
|
||||
{
|
||||
_synchronizedScenes.Remove(gameObject.scene.handle);
|
||||
base.TimeManager.OnPrePhysicsSimulation -= TimeManager_OnPrePhysicsSimulation;
|
||||
}
|
||||
}
|
||||
|
||||
private void TimeManager_OnPrePhysicsSimulation(float delta)
|
||||
{
|
||||
/* If to simulate physics then do so on this objects
|
||||
* physics scene. If you know the object is not going to change
|
||||
* scenes you can cache the physics scenes
|
||||
* rather than look them up each time. */
|
||||
if (_synchronizePhysics)
|
||||
gameObject.scene.GetPhysicsScene().Simulate(delta);
|
||||
if (_synchronizePhysics2D)
|
||||
gameObject.scene.GetPhysicsScene2D().Simulate(delta);
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,45 @@
|
||||
---
|
||||
description: >-
|
||||
Scene Visibility offers details of using the "Scene Condition" with the
|
||||
ObserverManager, and how to manage Observers in a Scene.
|
||||
---
|
||||
|
||||
# Scene Visibility
|
||||
|
||||
## General
|
||||
|
||||
You can control if clients become an [**Observer**](../observers/) of gameobjects in a scene that they are in. This is possible if you set your [**ObserverManager**](../../../fishnet-building-blocks/components/managers/observermanager/) to include a **Scene Condition**.\
|
||||
\
|
||||
The **Scene Condition** ensures that[ **NetworkObjects**](../networked-gameobjects-and-scripts/networkobjects/) both scene and spawned are only visible to players in the same scene as the object. In most cases you will want to add a [NetworkObserver](../../../fishnet-building-blocks/components/network-observer.md) component, with a scene condition to your networked objects.
|
||||
|
||||
{% hint style="info" %}
|
||||
When encountering an error about being unable to find a NetworkObject or RPCLink during a scene change, you likely forgot to add the scene condition.
|
||||
{% endhint %}
|
||||
|
||||
## Managing visibility
|
||||
|
||||
### Initial scene load
|
||||
|
||||
When a client is loading into the game for the first time, the first scene/offline scene loaded is done so by the Unity Scene Manager. This means that clients were not automatically added to the scene when connected. Which also means they do not have visibility of that scene.
|
||||
|
||||
Once client connects to a host or server, If you use the **PlayerSpawner** component FishNet provided on the NetworkManager, there is logic in that component that will add the client to the default scene on the Player Objects spawn.
|
||||
|
||||
{% hint style="danger" %}
|
||||
If you decided not to use the **PlayerSpawner** Component provided on the NetworkManger in Fishnet, you will have to either have to load a client into a scene using FishNets SceneManager, or call SceneManger.AddOwnerToDefaultScene() on a Object that you gave that client ownership to.
|
||||
{% endhint %}
|
||||
|
||||
### Adding client connections to scenes
|
||||
|
||||
* When Loading a scene globally or by connection, the SceneManager will automatically place that connection into the scene.
|
||||
* Once added that client will be able to view all NetworkObjects that are apart of that scene.
|
||||
* You can add Client Connections to Scenes Manually if the scene is already loaded on the client, by using SceneManager.AddConnectionsToScene();
|
||||
|
||||
{% hint style="warning" %}
|
||||
Manually adding and removing client connections is recommended for Power Users only.
|
||||
{% endhint %}
|
||||
|
||||
### Removing client connections from scenes
|
||||
|
||||
* When Unloading a scene from a client, the server will automatically remove the client connection from the scene.
|
||||
* If you are the host, and you unload the hosts client from a scene, it will only remove the host from the scene and the hosts client will lose visibility. See Host Behaviour for [**Scene Caching**](scene-caching.md) for more details.
|
||||
* You can remove a client manually from a loaded scene, by using SceneManager.RemoveConnectionsFromScene();
|
||||
@@ -0,0 +1,55 @@
|
||||
---
|
||||
description: Instructions for how to unload networked scenes in FishNet.
|
||||
---
|
||||
|
||||
# Unloading Scenes
|
||||
|
||||
## General
|
||||
|
||||
Unloading scenes is the same as loading scenes, except to call Unload rather than Load. Scenes can be unloaded by connection, or globally. When unloaded globally scenes will be unloaded for all players. When unloading by connection only the connections specified will unload the scenes.
|
||||
|
||||
## Unloading scenes
|
||||
|
||||
### Global scenes
|
||||
|
||||
* **Global Scenes** can be unloaded by calling UnloadGlobalScenes() in the SceneManager.
|
||||
* You cannot unload a global scene with UnloadConnectionScenes() method.
|
||||
|
||||
```csharp
|
||||
SceneUnloadData sud = new SceneUnloadData("Town");
|
||||
base.NetworkManager.SceneManager.UnloadGlobalScenes(sud);
|
||||
```
|
||||
|
||||
### Connection scenes
|
||||
|
||||
**Connection Scenes** follow the same principle, but has a few method overloads. You can unload scenes for a single connection, multiple connections at once, or unload scenes on the server.
|
||||
|
||||
* The Server will only Unload a connection scene on itself, if all connections have been unloaded from that scene.
|
||||
* If you wish to unload a scene and all connections, get all the connections in the scene and call unload with those connections. SceneManager.SceneConnections holds all online scenes and the connections in them.
|
||||
* If you wish to keep a scene loaded on server when unloading all Connections See [**Scene Caching**](scene-caching.md).
|
||||
|
||||
```csharp
|
||||
SceneUnloadData sud = new SceneUnloadData(new string[] { "Main", "Additive") });
|
||||
|
||||
// Unload scenes for a single connection.
|
||||
NetworkConnection conn = base.Owner;
|
||||
base.NetworkManager.SceneManager.UnloadConnectionScenes(conn, sud);
|
||||
|
||||
// Unload scenes for several connections at once.
|
||||
NetworkConnection[] conns = new NetworkConnection[] { connA, connB };
|
||||
base.NetworkManager.SceneManager.UnloadConnectionScenes(conns, sud);
|
||||
|
||||
// Unload scenes only on the server.
|
||||
// that you don't want all players in.
|
||||
base.NetworkManager.SceneManager.UnloadConnectionScenes(sud);
|
||||
```
|
||||
|
||||
## Advanced info
|
||||
|
||||
### Behind the "scenes"
|
||||
|
||||
The [**SceneManager**](../../../fishnet-building-blocks/components/managers/scenemanager.md) Class has very detailed XML comments on how the unload process works in detail, if you need to troubleshoot the scene unload process, these comments will help you understand the flow of how a scene loads.
|
||||
|
||||
### Events
|
||||
|
||||
Make sure to check out the [**Scene Events**](scene-events.md) that you can subscribe to to give better control over your game.
|
||||
Reference in New Issue
Block a user