NetworkedObject
In a scene, each GameObject is assiotiated to a connected client. When many players join a scene, all GameObjects are distributed to them and redistributed if a player joins or leaves.
To share a GameObject, append a NetworkedObject
behaviour to it. You can share variables, events or spawners in your own behaviours after that.
Associated
If you check Associated
, objects will be duplicated on each client and each client will always manages its objects. (typically used on player character)
A copy of this GameObject is created on Awake
Shared
If you check Shared
, all clients variables will be shared. (typically used on a button). This mode looks like NetGlobal (with the same API).
using UnityEngine;
using EODE.Wonderland.SimpleNet;
public class SharedButton : MonoBehaviour {
public Renderer Renderer;
public Material MatOff;
public Material MatOn;
NetVar<bool> _on;
void Start() {
_on = this.NetVar<bool>();
_on.OnUpdate += on => {
Debug.Log("Shared = "+string.Join(", ", _on.Values));
var active = _on.Values.FirstOrNull(kv => kv.Value);
if (active.HasValue) Renderer.material = MatOn;
else Renderer.material = MatOff;
};
}
void OnTriggerEnter(Collider collider) {
if (collider.GetComponent<NetworkedObject>().IsActive) {
_on.Value = true;
}
}
void OnTriggerExit(Collider collider) {
if (collider.GetComponent<NetworkedObject>().IsActive) {
_on.Value = false;
}
}
}
Shared is compatible with Event and Action variable Unlike Globals, Clients does not emit values in Host mode. Server side, Values =
[-1: value]
, all clients receive[serverId: value]
.
Simulate
You can simulate an online clone (in editor). You can configure Latency, change object layer and do something on the clone like this code:
#if UNITY_EDITOR
// networked
var netobj = GetComponent<NetworkedObject>();
netobj._Editor_OnTestCloneEnabled += go => {
go.GetComponent<SpriteRenderer>().color = Color.blue;
};
#endif
Status
- IsNetworked: The scene is online (linked to a host server or 2 players in a P2P server)
- IsEmitter: True if this object is managed by the client
- IsActive (client): !IsNetworked || IsEmitter
- IsClone: Clone of an Associated object
- IsActive (Host server): Adds Clone objects. Required to trigger events.