From 091c79b429e2dd6a5c63b35def96f7e5d32c6c19 Mon Sep 17 00:00:00 2001 From: Chris Langsenkamp Date: Mon, 29 Jul 2019 03:21:30 -0400 Subject: [PATCH] NetworkBehaviour --- Assets/Mirror/Runtime/NetworkBehaviour.cs | 133 ++++++---------------- 1 file changed, 33 insertions(+), 100 deletions(-) diff --git a/Assets/Mirror/Runtime/NetworkBehaviour.cs b/Assets/Mirror/Runtime/NetworkBehaviour.cs index 4583d1a26..4a44d67d1 100644 --- a/Assets/Mirror/Runtime/NetworkBehaviour.cs +++ b/Assets/Mirror/Runtime/NetworkBehaviour.cs @@ -17,140 +17,73 @@ public class NetworkBehaviour : MonoBehaviour { float lastSyncTime; + // hidden because NetworkBehaviourInspector shows it only if has OnSerialize. /// /// sync interval for OnSerialize (in seconds) /// - // hidden because NetworkBehaviourInspector shows it only if has OnSerialize. [HideInInspector] public float syncInterval = 0.1f; /// /// This value is set on the NetworkIdentity and is accessible here for convenient access for scripts. /// public bool localPlayerAuthority => netIdentity.localPlayerAuthority; + /// /// Returns true if this object is active on an active server. /// This is only true if the object has been spawned. This is different from NetworkServer.active, which is true if the server itself is active rather than this object being active. /// public bool isServer => netIdentity.isServer; + /// /// Returns true if running as a client and this object was spawned by a server. /// public bool isClient => netIdentity.isClient; + /// /// This returns true if this object is the one that represents the player on the local machine. /// In multiplayer games, there are multiple instances of the Player object. The client needs to know which one is for "themselves" so that only that player processes input and potentially has a camera attached. The IsLocalPlayer function will return true only for the player instance that belongs to the player on the local machine, so it can be used to filter out input for non-local players. /// This example shows processing input for only the local player. - /// - /// using UnityEngine; - /// using UnityEngine.Networking; - /// - /// public class Player : NetworkBehaviour - /// { - /// int moveX = 0; - /// int moveY = 0; - /// - /// void Update() - /// { - /// if (!isLocalPlayer) - /// { - /// return; - /// } - /// // input handling for local player only - /// int oldMoveX = moveX; - /// int oldMoveY = moveY; - /// moveX = 0; - /// moveY = 0; - /// if (Input.GetKey(KeyCode.LeftArrow)) - /// { - /// moveX -= 1; - /// } - /// if (Input.GetKey(KeyCode.RightArrow)) - /// { - /// moveX += 1; - /// } - /// if (Input.GetKey(KeyCode.UpArrow)) - /// { - /// moveY += 1; - /// } - /// if (Input.GetKey(KeyCode.DownArrow)) - /// { - /// moveY -= 1; - /// } - /// if (moveX != oldMoveX || moveY != oldMoveY) - /// { - /// CmdMove(moveX, moveY); - /// } - /// } - /// - /// [Command] - /// void CmdMove(int dx, int dy) - /// { - /// // move here - /// } - /// } - /// /// public bool isLocalPlayer => netIdentity.isLocalPlayer; + + /// + /// + /// public bool isServerOnly => isServer && !isClient; + + /// + /// + /// public bool isClientOnly => isClient && !isServer; + /// /// This returns true if this object is the authoritative version of the object in the distributed network application. /// The localPlayerAuthority value on the NetworkIdentity determines how authority is determined. For most objects, authority is held by the server / host. For objects with localPlayerAuthority set, authority is held by the client of that player. /// public bool hasAuthority => netIdentity.hasAuthority; + /// /// The unique network Id of this object. /// This is assigned at runtime by the network server and will be unique for all objects for that network session. /// public uint netId => netIdentity.netId; + /// /// The NetworkConnection associated with this NetworkIdentity. This is only valid for player objects on the server. /// public NetworkConnection connectionToServer => netIdentity.connectionToServer; + /// /// The NetworkConnection associated with this NetworkIdentity. This is only valid for player objects on the server. - /// - /// //Attach this script to a GameObject - /// //Attach a TextMesh to the GameObject. To do this click the GameObject, click the Add Component button in the Inspector window, and go to Mesh>Text Mesh. - /// //Attach a NetworkIdentity to the GameObject by clicking Add Component, then go to Network>NetworkIdentity. In the component that was added, check the Local Player Authority checkbox. - /// //Next, create an empty GameObject. Attach a NetworkManager to it by clicking the GameObject, clicking Add Component going to Network>NetworkManager. Also add a NetworkManagerHUD the same way. - /// - /// //This script outputs the Connection ID and address to the console when the Client is started - /// - /// using UnityEngine; - /// using UnityEngine.Networking; - /// - /// public class ConnectionToClientExample : NetworkBehaviour - /// { - /// //This is a TextMesh component that you attach to the child of the NetworkIdentity GameObject - /// TextMesh m_TextMesh; - /// - /// void Start() - /// { - /// //Output the connection ID and IP address of the connection by using connectionToClient - /// Debug.Log("Connection ID : " + connectionToClient.connectionId); - /// Debug.Log("Connection Address : " + connectionToClient.address); - /// //Check that the connection is marked as ready - /// if (connectionToClient.isReady) - /// { - /// Debug.Log("Ready!"); - /// } - /// //Enter the child of your GameObject (the GameObject with the TextMesh you attach) - /// //Fetch the TextMesh component of it - /// m_TextMesh = GetComponentInChildren(typeof(TextMesh)) as TextMesh; - /// //Change the Text of the TextMesh to show the netId - /// m_TextMesh.text = "ID : " + netId; - /// //Output the connection to Client - /// } - /// } - /// /// public NetworkConnection connectionToClient => netIdentity.connectionToClient; + protected ulong syncVarDirtyBits { get; private set; } protected bool syncVarHookGuard { get; set; } [EditorBrowsable(EditorBrowsableState.Never), Obsolete("Use syncObjects instead.")] protected List m_SyncObjects => syncObjects; + /// /// objects that can synchronize themselves, such as synclists /// @@ -160,6 +93,10 @@ public class NetworkBehaviour : MonoBehaviour /// NetworkIdentity component caching for easier access /// NetworkIdentity netIdentityCache; + + /// + /// + /// public NetworkIdentity netIdentity { get @@ -176,6 +113,9 @@ public NetworkIdentity netIdentity } } + /// + /// + /// public int ComponentIndex { get @@ -336,7 +276,7 @@ protected void SendTargetRPCInternal(NetworkConnection conn, Type invokeClass, s /// /// Manually invoke an RPC function. /// - /// Hash of the RPC name. + /// Hash of the RPC name. /// Parameters to pass to the RPC function. /// Returns true if successful. [EditorBrowsable(EditorBrowsableState.Never)] @@ -371,7 +311,7 @@ protected void SendEventInternal(Type invokeClass, string eventName, NetworkWrit /// /// Manually invoke a SyncEvent. /// - /// Hash of the SyncEvent name. + /// Hash of the SyncEvent name. /// Parameters to pass to the SyncEvent. /// Returns true if successful. [EditorBrowsable(EditorBrowsableState.Never)] @@ -737,46 +677,39 @@ void DeSerializeObjectsDelta(NetworkReader reader) } } - [EditorBrowsable(EditorBrowsableState.Never)] /// /// This is invoked on clients when the server has caused this object to be destroyed. /// This can be used as a hook to invoke effects or do client specific cleanup. - /// - /// using UnityEngine; - /// using UnityEngine.Networking; - /// - /// class Bomb : NetworkBehaviour - /// { - /// public override void OnNetworkDestroy() - /// { - /// // play explosion sound - /// } - /// } - /// /// + [EditorBrowsable(EditorBrowsableState.Never)] public virtual void OnNetworkDestroy() {} + /// /// This is invoked for NetworkBehaviour objects when they become active on the server. /// This could be triggered by NetworkServer.Listen() for objects in the scene, or by NetworkServer.Spawn() for objects that are dynamically created. /// This will be called for objects on a "host" as well as for object on a dedicated server. /// public virtual void OnStartServer() {} + /// /// Called on every NetworkBehaviour when it is activated on a client. /// Objects on the host have this function called, as there is a local client on the host. The values of SyncVars on object are guaranteed to be initialized correctly with the latest state from the server when this function is called on the client. /// public virtual void OnStartClient() {} + /// /// Called when the local player object has been set up. /// This happens after OnStartClient(), as it is triggered by an ownership message from the server. This is an appropriate place to activate components or functionality that should only be active for the local player, such as cameras and input. /// public virtual void OnStartLocalPlayer() {} + /// /// This is invoked on behaviours that have authority, based on context and 'NetworkIdentity.localPlayerAuthority.' /// This is called after OnStartServer and OnStartClient. /// When NetworkIdentity.AssignClientAuthority is called on the server, this will be called on the client that owns the object. When an object is spawned with NetworkServer.SpawnWithClientAuthority, this will be called on the client that owns the object. /// public virtual void OnStartAuthority() {} + /// /// This is invoked on behaviours when authority is removed. /// When NetworkIdentity.RemoveClientAuthority is called on the server, this will be called on the client that owns the object.