ObjectNet provides a special class that can be extended to provide a bunch of events and methods, this class is NetworkBehaviour.
The NetworkBehaviour is a class that replaces Unity MonoBehaviour class and provides a lot of embedded methods and events.
NetworkBehavour can be used even if your game doesn't implement MultiPlayer features, this means if your game must work on the single-player mode you can keep this inheritance without affecting your single-player mode behavior.
This is pretty useful since the developer can create your game to run in Single-player mode and multi-player while keeping the same code base.
To extend your MonoBehaviour script from NetworkBehaviour it's just change inheritance.
publicclassMyGameScript:NetworkBehaviour { // Your class code }
Once inherited, you can implement some special method to handle your multiplayer game, the following script will illustrate the main methods:
To use NetworkBehaviour functionality in multiplayer, the object must be registered as a NetworkObject in the NetworkManager's Prefab Database.
OnNetworkStarted
This event is called once when Network is available to this object.
/// <summary> /// This event is called when Network features starts /// /// Note : This event is controlled internally by framework and ensure that /// network is up and running /// </summary>publicoverridevoidOnNetworkStarted() { // Here you can do any that you need after ensure that Network is initialized }
Awake
Active Awake:
This event is the Awake execute only when object is running on Active Mode.
/// <summary> /// Active Awake is executed when objects on Active mode Awake /// </summary>publicvoidActiveAwake() { }
Passive Awake:
This event is the Awake execute only when object is running on Passive Mode.
/// <summary> /// Passive Awake is executed when objects on Passive mode Awake /// </summary>publicvoidPassiveAwake() { }
Start
Active Start:
This event is the Start execute only when object is running on Active Mode.
/// <summary>
/// Active Start is executed when objects on Active mode Start
/// </summary>
public void ActiveStart() {
}
Passive Start:
This event is the Start execute only when object is running on Passive Mode.
/// <summary>
/// Passive Start is executed when objects on Passive mode Start
/// </summary>
public void PassiveStart() {
}
OnEnable
Active OnEnable:
This event is the OnEnable execute only when object is running on Active Mode.
/// <summary>
/// Active OnEnable is executed when objects on Active mode
/// </summary>
public void ActiveOnEnable() {
}
Passive OnEnable:
This event is the OnEnable execute only when object is running on PassiveMode.
/// <summary>
/// Active OnEnable is executed when objects on Passive mode
/// </summary>
public void PassiveOnEnable() {
}
OnDisable
Active OnDisable:
This event is the OnDisable execute only when object is running on ActiveMode.
/// <summary>
/// Active OnDisable is executed when objects on Active mode
/// </summary>
public void ActiveOnDisable() {
}
Passive OnDisable:
This event is the OnDisable execute only when object is running on PassiveMode.
/// <summary>
/// Passive OnDisable is executed when objects on Passive mode
/// </summary>
public void PassiveOnDisable() {
}
Update
Active Update:
This event is the Update execute only when object is running on Active Mode.
/// <summary>
/// Active Update is executed every frame for Active object
/// </summary>
public void ActiveUpdate() {
}
Passive Update:
This event is the Update execute only when object is running on Passive Mode.
/// <summary>
/// Passive Update is executed every frame for Passive object
/// </summary>
public void PassiveUpdate() {
}
FixedUpdate
Active FixedUpdate:
This event is the FixedUpdate execute only when object is running on Active Mode.
/// <summary>
/// Active FixedUpdate is executed every physics frame for Active object
/// </summary>
public void ActiveFixedUpdate() {
}
Passive FixedUpdate:
This event is the FixedUpdate execute only when object is running on Passive Mode.
/// <summary>
/// Passive FixedUpdate is executed every physics frame for Passive object
/// </summary>
public void PassiveFixedUpdate() {
}
LateUpdate
Active LateUpdate:
This event is the LateUpdate execute only when object is running on Active Mode.
/// <summary>
/// Active LateUpdate is executed every frame after Update for Active object
/// </summary>
public void ActiveLateUpdate() {
}
Passive LateUpdate:
This event is the LateUpdate execute only when object is running on Passive Mode.
/// <summary>
/// Passive LateUpdate is executed every frame after Update for Passive object
/// </summary>
public void PassiveLateUpdate() {
}
Example:
Here is an example on how to use them
public class MyGameScript : NetworkBehaviour {
public override void OnNetworkStarted() {
Debug.Log("Network is Initialized");
}
public void ActiveStart() {
Debug.Log("This is an Active Object");
}
public void PassiveStart() {
Debug.Log("This is a Passive Object");
}
}