Skip to content

Commit 4d8497e

Browse files
committed
docs: adding section for filtering scene objects
1 parent 9339d92 commit 4d8497e

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

doc/docs/guides/game-objects/scene-objects.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,48 @@ Networked scene objects are spawned by the ClientObjectManager and ServerObjectM
2222
If a Scene game object is destroyed on the server before a client joins the game, then it is never spawned on new clients. It will be left in the state it is when you were editing the scene.
2323

2424
After a client has connected and you have called [AddCharacter](/docs/reference/Mirage/ServerObjectManager#addcharacterinetworkplayer-networkidentity) or [SpawnVisibleObjects](/docs/reference/Mirage/ServerObjectManager#spawnvisibleobjectsinetworkplayer-boolean), the client is sent a spawn message for each of the Scene objects that exist on the server, that are visible to that client. This message causes the game object on the client to be enabled and has the latest state of that game object from the server in it. This means that only game objects that are visible to the client and not destroyed on the server, are spawned on the client. Like regular non-Scene objects, these Scene objects are started with the latest state when the client joins the game.
25+
26+
27+
### Filtering Scene Objects
28+
29+
By default, calling `SpawnSceneObjects` or `PrepareToSpawnSceneObjects` will cause Mirage to find all `NetworkIdentity` components in all loaded scenes using `Resources.FindObjectsOfTypeAll<NetworkIdentity>()`.
30+
31+
In some cases, like when running multiple server or client instances in the same unity process, this can be problematic as it might find objects from scenes that don't belong to the current instance.
32+
33+
To solve this, you can provide a custom filter by setting the `SceneObjectFilter` property on the `ServerObjectManager` and `ClientObjectManager`. This allows you to control exactly which `NetworkIdentity` components are included. If the filter is left `null`, the default behavior is used.
34+
35+
**Example: Only include objects from a specific scene**
36+
```csharp
37+
using Mirage;
38+
using UnityEngine;
39+
using UnityEngine.SceneManagement;
40+
41+
public class MySceneManager : MonoBehaviour
42+
{
43+
public ServerObjectManager serverObjectManager;
44+
public ClientObjectManager clientObjectManager;
45+
public Scene myScene;
46+
47+
// Set the scene to use for filtering
48+
public void SetScene(Scene scene)
49+
{
50+
myScene = scene;
51+
}
52+
53+
void Awake()
54+
{
55+
// Set the filter before spawning scene objects
56+
var filter = (NetworkIdentity identity) =>
57+
{
58+
return identity.gameObject.scene == myScene;
59+
};
60+
61+
serverObjectManager.SceneObjectFilter = filter;
62+
clientObjectManager.SceneObjectFilter = filter;
63+
64+
// Now when SpawnSceneObjects is called, it will only
65+
// consider objects from `myScene`.
66+
serverObjectManager.SpawnSceneObjects();
67+
}
68+
}
69+
```

0 commit comments

Comments
 (0)