Audio Room
AudioRoom
In some cases, it may be useful to divide the players into different audio "rooms", regardless of the distance between them.
This means that only players in the same audio room can talk to each other if a room is isolated thanks to a door. To do so, we provide 4 class :
AudioRoomManager
AudioRoom
AudioRoomMember
AudioDoor
AudioRoomManager
AudioRoomManager
manages the list of rooms & players in the scene.
The MatchingRooms()
method is called by players after each move to get a list of the rooms in which he is located (please note that rooms can overlap on each other).
AudioRoom
An audio room is defined with the AudioRoom
component.
The spatial position of the room is set by a central point and a box (a gizmo cube display it in the Scene view).
Each AudioRoom
has a specific Id
and registers itself on the AudioRoomManager
during the Awake()
.
The IsIsolated
bool is used to define whether players located into the room are isolated from others thanks to a door.
When the door associated with a room moves, the Isolate()
methods is called (by AudioDoor
) and checks if a player is located into the room to ask him to update the audio group filtering thanks to OnCurrentRoomIsolationChange()
method.
AudioRoomMember
Each network player prefab has a AudioRoomMember
class so it can registed itself on the AudioRoomManager
at start.
At each move, thanks to the DidMove()
callback, AudioRoomMember
checks whether the user has moved to another room.
Then, it set the audio group (DynamicAudioGroupMember
) according the room status (isolated or not).
When leaving a room, the group filtering is either reset to :
- the default value (we can listen to anybody) if set to
OutOfRoomFilterMode.NoFilter
- or to
OutOfRoomFilterMode.NeverMatchingGroupFilter
, making the user unable to listen to anybody.
If the member or the room contains child implementing IAudioRoomListener, they will receive the OnIsInRoom
callback when a player enter or leave the room.
AudioDoor
The AudioDoor
class must be added to a door game object separating audio rooms.
The door can be open or close thanks to the ToogleDoor()
and Open()
public methods.
If the player requesting the opening and closing of the door does not have the authority to do so, then he sends a RPC to the StateAuthority.
C#
public void ToogleDoor()
{
Open(!IsOpened);
}
public void Open(bool isOpen)
{
if (Object.HasStateAuthority)
{
IsOpened = isOpen;
}
else
{
RPC_Open(isOpen);
}
}
[Rpc(sources:RpcSources.All, targets: RpcTargets.StateAuthority)]
public void RPC_Open(bool isOpen)
{
IsOpened = isOpen;
}
The door status (open or close) is synchronized over the network thanks to the networked bool IsOpened
.
A change ChangeDetector
is used to detect modification in the Render()
C#
[Networked]
public NetworkBool IsOpened { get; set; } = true;
ChangeDetector renderChangeDetector;
public override void Spawned()
{
base.Spawned();
OnIsOpenedChange();
renderChangeDetector = GetChangeDetector(ChangeDetector.Source.SnapshotFrom);
}
public override void Render()
{
// Check if the IsOpened changed
if (TryDetectIsOpenedChange())
{
OnIsOpenedChange();
}
}
OnIsOpenedChange()
method change the isolation status of rooms separated by the door.
Then the rooms inform players located into the room about the isolation status : if the room is isolated, the additionalGroupFilter
of the DynamicAudioGroupMember
is set with the roomId
.
In this way, players in the room listen only to members of the room and not to other players, even if they are within the defined distance limit.
The OnStatusChange()
event is generated when the door status changed.
Dependencies
Dynamic Audio Group version 2.0.1
Demo
To illustrate the usage, a demo scene can be found in Assets\Photon\FusionAddons\AudioRoom\Demo\Scenes\AudioRoom.unity
.
The scene has two rooms separated by a door.
The door can be opened or closed using the Toggle is opened
button of the AudioDoor
component located on the Door
game object.
Connect multiple clients and observe the information panel associated with each player.
Each player is surrounded by two spheres.
If the door is opened :
- The green sphere is the area that will start listening to another player as soon as his head enters it, even if they are not in the same room.
- Listening will stop as soon as the head leaves the red sphere.
If the door is closed :
- only players located in the same room and at proximity will be able to chat with each other.
Download
This addon latest version is included into the Industries addon project
Supported topologies
- shared mode
Changelog
- Version 2.0.1: Add new option when leaving a room (listen to anybody or not) + add (un)register methods for AudioRoom & AudioRoomMember + add IAudioRoomListener callback
- Version 2.0.0: Fusion 2.0 support
- Version 1.0.3: Change MoveDoorWithAudioRoomStatus from NetworkBehaviour to MonoBehavior
- Version 1.0.2: Namespace modification
- Version 1.0.1: Add demo scene + add namespace
- Version 1.0.0: First release