void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info) { Vector3 syncPosition = Vector3.zero; // if (stream.isWriting) { syncPosition = rigidbody.position; // stream.Serialize(ref syncPosition); // } else { stream.Serialize(ref syncPosition); // rigidbody.position = syncPosition; // . } }
using UnityEngine; using System.Collections; [RequireComponent( typeof( NetworkView ) )] // Unity , NetworkView. NetworkStateSynchronization Off. public class ServerSide : MonoBehaviour { private int playerCount = 0; // public int PlayersCount { get { return playerCount; } } // void OnServerInitialized() { SendMessage( "SpawnPlayer", "Player Server" ); // } void OnPlayerConnected( NetworkPlayer player ) { ++playerCount; // networkView.RPC( "SpawnPlayer", player, "Player " + playerCount.ToString() ); // } void OnPlayerDisconnected( NetworkPlayer player ) { --playerCount; // Network.RemoveRPCs( player ); // Network.DestroyPlayerObjects( player ); // } }
using UnityEngine; using System.Collections; [RequireComponent( typeof( NetworkView ) )] // Unity , NetworkView. NetworkStateSynchronization Off. public class ClientSide : MonoBehaviour { public GameObject playerPrefab; // , public Vector2 spawnArea = new Vector2( 8.0f, 8.0f ); // private Vector3 RandomPosition { // get { return transform.position + transform.right * ( Random.Range( 0.0f, spawnArea.x ) - spawnArea.x * 0.5f ) + transform.forward * ( Random.Range( 0.0f, spawnArea.y ) - spawnArea.y * 0.5f ); } } [RPC] // Unity , private void SpawnPlayer( string playerName ) { Vector3 position = RandomPosition; // GameObject newPlayer = Network.Instantiate( playerPrefab, position, Quaternion.LookRotation( transform.position - position, Vector3.up ), 0 ) as GameObject; // newPlayer.BroadcastMessage( "SetPlayerName", playerName ); // ( ) } void OnDisconnectedFromServer( NetworkDisconnection info ) { Network.DestroyPlayerObjects( Network.player ); // } }
using UnityEngine; using System.Collections; public class MultiplayerMenu : MonoBehaviour { const int NETWORK_PORT = 4585; // const int MAX_CONNECTIONS = 20; // const bool USE_NAT = false; // NAT? private string remoteServer = "127.0.0.1"; // ( localhost) void OnGUI() { if ( Network.peerType == NetworkPeerType.Disconnected ) { // if ( GUILayout.Button( "Start Server" ) ) { // « » Network.InitializeSecurity(); // Network.InitializeServer( MAX_CONNECTIONS, NETWORK_PORT, USE_NAT ); // } GUILayout.Space(30f); // remoteServer = GUILayout.TextField( remoteServer ); // if ( GUILayout.Button( "Connect to server" ) ) { // «» Network.Connect( remoteServer, NETWORK_PORT ); // } } else if ( Network.peerType == NetworkPeerType.Connecting ) { // GUILayout.Label( "Trying to connect to server" ); // } else { // ( NetworkPeerType.Server, NetworkPeerType.Client) if ( GUILayout.Button( "Disconnect" ) ) { // «» Network.Disconnect(); // } } } void OnFailedToConnect( NetworkConnectionError error ) { Debug.Log( "Failed to connect: " + error.ToString() ); // } void OnDisconnectedFromServer( NetworkDisconnection info ) { if ( Network.isClient ) { Debug.Log( "Disconnected from server: " + info.ToString() ); // } else { Debug.Log( "Connections closed" ); // Network.Disconnect() } } void OnConnectedToServer() { Debug.Log( "Connected to server" ); // } }
using UnityEngine; using System.Collections; [RequireComponent( typeof( Rigidbody ) )] // Rigidbody public class PlayerControls : MonoBehaviour { /* */ private float lastSynchronizationTime; // private float syncDelay = 0f; // private float syncTime = 0f; // private Vector3 syncStartPosition = Vector3.zero; // private Vector3 syncEndPosition = Vector3.zero; // private Quaternion syncStartRotation = Quaternion.identity; // private Quaternion syncEndRotation = Quaternion.identity; // private NetworkView netView; // NetworkView private string myName = ""; // ( , ) public string MyName { get { return myName; } } // public float power = 20f; void Awake () { netView = gameObject.AddComponent( typeof( NetworkView ) ) as NetworkView; // NetworkView netView.viewID = Network.AllocateViewID(); // netView.observed = this; // () netView.stateSynchronization = NetworkStateSynchronization.Unreliable; // , lastSynchronizationTime = Time.time; // } void FixedUpdate () { if ( netView.isMine ) { // , , float inputX = Input.GetAxis( "Horizontal" ); float inputY = Input.GetAxis( "Vertical" ); if ( inputX != 0.0f ) { rigidbody.AddTorque( Vector3.forward * -inputX * power, ForceMode.Impulse ); } if ( inputY != 0.0f ) { rigidbody.AddTorque( Vector3.right * inputY * power, ForceMode.Impulse ); } } else { syncTime += Time.fixedDeltaTime; rigidbody.position = Vector3.Lerp( syncStartPosition, syncEndPosition, syncTime / syncDelay ); // rigidbody.rotation = Quaternion.Lerp( syncStartRotation, syncEndRotation, syncTime / syncDelay ); // } } void OnSerializeNetworkView( BitStream stream, NetworkMessageInfo info ) { Vector3 syncPosition = Vector3.zero; // Vector3 syncVelocity = Vector3.zero; // Quaternion syncRotation = Quaternion.identity; // if ( stream.isWriting ) { // , syncPosition = rigidbody.position; stream.Serialize( ref syncPosition ); syncPosition = rigidbody.velocity; stream.Serialize( ref syncVelocity ); syncRotation = rigidbody.rotation; stream.Serialize( ref syncRotation ); } else { // stream.Serialize( ref syncPosition ); stream.Serialize( ref syncVelocity ); stream.Serialize( ref syncRotation ); syncTime = 0f; // syncDelay = Time.time - lastSynchronizationTime; // lastSynchronizationTime = Time.time; // syncEndPosition = syncPosition + syncVelocity * syncDelay; // , syncStartPosition = rigidbody.position; // syncEndRotation = syncRotation; // syncStartRotation = rigidbody.rotation; // } } void SetPlayerName( string name ) { myName = name; // } }
Source: https://habr.com/ru/post/211202/
All Articles