# Conflicts: # Assets/Scipts/Timer.csmaster
@ -0,0 +1,159 @@ | |||||
%YAML 1.1 | |||||
%TAG !u! tag:unity3d.com,2011: | |||||
--- !u!29 &1 | |||||
OcclusionCullingSettings: | |||||
m_ObjectHideFlags: 0 | |||||
serializedVersion: 2 | |||||
m_OcclusionBakeSettings: | |||||
smallestOccluder: 5 | |||||
smallestHole: 0.25 | |||||
backfaceThreshold: 100 | |||||
m_SceneGUID: 00000000000000000000000000000000 | |||||
m_OcclusionCullingData: {fileID: 0} | |||||
--- !u!104 &2 | |||||
RenderSettings: | |||||
m_ObjectHideFlags: 0 | |||||
serializedVersion: 9 | |||||
m_Fog: 0 | |||||
m_FogColor: {r: 0.5, g: 0.5, b: 0.5, a: 1} | |||||
m_FogMode: 3 | |||||
m_FogDensity: 0.01 | |||||
m_LinearFogStart: 0 | |||||
m_LinearFogEnd: 300 | |||||
m_AmbientSkyColor: {r: 0.212, g: 0.227, b: 0.259, a: 1} | |||||
m_AmbientEquatorColor: {r: 0.114, g: 0.125, b: 0.133, a: 1} | |||||
m_AmbientGroundColor: {r: 0.047, g: 0.043, b: 0.035, a: 1} | |||||
m_AmbientIntensity: 1 | |||||
m_AmbientMode: 3 | |||||
m_SubtractiveShadowColor: {r: 0.42, g: 0.478, b: 0.627, a: 1} | |||||
m_SkyboxMaterial: {fileID: 0} | |||||
m_HaloStrength: 0.5 | |||||
m_FlareStrength: 1 | |||||
m_FlareFadeSpeed: 3 | |||||
m_HaloTexture: {fileID: 0} | |||||
m_SpotCookie: {fileID: 10001, guid: 0000000000000000e000000000000000, type: 0} | |||||
m_DefaultReflectionMode: 0 | |||||
m_DefaultReflectionResolution: 128 | |||||
m_ReflectionBounces: 1 | |||||
m_ReflectionIntensity: 1 | |||||
m_CustomReflection: {fileID: 0} | |||||
m_Sun: {fileID: 0} | |||||
m_IndirectSpecularColor: {r: 0, g: 0, b: 0, a: 1} | |||||
m_UseRadianceAmbientProbe: 0 | |||||
--- !u!157 &3 | |||||
LightmapSettings: | |||||
m_ObjectHideFlags: 0 | |||||
serializedVersion: 11 | |||||
m_GIWorkflowMode: 1 | |||||
m_GISettings: | |||||
serializedVersion: 2 | |||||
m_BounceScale: 1 | |||||
m_IndirectOutputScale: 1 | |||||
m_AlbedoBoost: 1 | |||||
m_TemporalCoherenceThreshold: 1 | |||||
m_EnvironmentLightingMode: 0 | |||||
m_EnableBakedLightmaps: 0 | |||||
m_EnableRealtimeLightmaps: 0 | |||||
m_LightmapEditorSettings: | |||||
serializedVersion: 10 | |||||
m_Resolution: 2 | |||||
m_BakeResolution: 40 | |||||
m_AtlasSize: 1024 | |||||
m_AO: 0 | |||||
m_AOMaxDistance: 1 | |||||
m_CompAOExponent: 1 | |||||
m_CompAOExponentDirect: 0 | |||||
m_Padding: 2 | |||||
m_LightmapParameters: {fileID: 0} | |||||
m_LightmapsBakeMode: 1 | |||||
m_TextureCompression: 1 | |||||
m_FinalGather: 0 | |||||
m_FinalGatherFiltering: 1 | |||||
m_FinalGatherRayCount: 256 | |||||
m_ReflectionCompression: 2 | |||||
m_MixedBakeMode: 2 | |||||
m_BakeBackend: 1 | |||||
m_PVRSampling: 1 | |||||
m_PVRDirectSampleCount: 32 | |||||
m_PVRSampleCount: 500 | |||||
m_PVRBounces: 2 | |||||
m_PVRFilterTypeDirect: 0 | |||||
m_PVRFilterTypeIndirect: 0 | |||||
m_PVRFilterTypeAO: 0 | |||||
m_PVRFilteringMode: 1 | |||||
m_PVRCulling: 1 | |||||
m_PVRFilteringGaussRadiusDirect: 1 | |||||
m_PVRFilteringGaussRadiusIndirect: 5 | |||||
m_PVRFilteringGaussRadiusAO: 2 | |||||
m_PVRFilteringAtrousPositionSigmaDirect: 0.5 | |||||
m_PVRFilteringAtrousPositionSigmaIndirect: 2 | |||||
m_PVRFilteringAtrousPositionSigmaAO: 1 | |||||
m_ShowResolutionOverlay: 1 | |||||
m_LightingDataAsset: {fileID: 0} | |||||
m_UseShadowmask: 1 | |||||
--- !u!196 &4 | |||||
NavMeshSettings: | |||||
serializedVersion: 2 | |||||
m_ObjectHideFlags: 0 | |||||
m_BuildSettings: | |||||
serializedVersion: 2 | |||||
agentTypeID: 0 | |||||
agentRadius: 0.5 | |||||
agentHeight: 2 | |||||
agentSlope: 45 | |||||
agentClimb: 0.4 | |||||
ledgeDropHeight: 0 | |||||
maxJumpAcrossDistance: 0 | |||||
minRegionArea: 2 | |||||
manualCellSize: 0 | |||||
cellSize: 0.16666667 | |||||
manualTileSize: 0 | |||||
tileSize: 256 | |||||
accuratePlacement: 0 | |||||
debug: | |||||
m_Flags: 0 | |||||
m_NavMeshData: {fileID: 0} | |||||
--- !u!1 &914867460 | |||||
GameObject: | |||||
m_ObjectHideFlags: 0 | |||||
m_CorrespondingSourceObject: {fileID: 0} | |||||
m_PrefabInternal: {fileID: 0} | |||||
serializedVersion: 6 | |||||
m_Component: | |||||
- component: {fileID: 914867462} | |||||
- component: {fileID: 914867461} | |||||
m_Layer: 0 | |||||
m_Name: ClientManager | |||||
m_TagString: Untagged | |||||
m_Icon: {fileID: 0} | |||||
m_NavMeshLayer: 0 | |||||
m_StaticEditorFlags: 0 | |||||
m_IsActive: 1 | |||||
--- !u!114 &914867461 | |||||
MonoBehaviour: | |||||
m_ObjectHideFlags: 0 | |||||
m_CorrespondingSourceObject: {fileID: 0} | |||||
m_PrefabInternal: {fileID: 0} | |||||
m_GameObject: {fileID: 914867460} | |||||
m_Enabled: 1 | |||||
m_EditorHideFlags: 0 | |||||
m_Script: {fileID: 11500000, guid: 4286216c0dea5f64e8d5a8b674f60424, type: 3} | |||||
m_Name: | |||||
m_EditorClassIdentifier: | |||||
ServerAddress: 127.0.0.1 | |||||
Port: 4444 | |||||
ConnectOnStart: 0 | |||||
Name: The Black Burrito | |||||
--- !u!4 &914867462 | |||||
Transform: | |||||
m_ObjectHideFlags: 0 | |||||
m_CorrespondingSourceObject: {fileID: 0} | |||||
m_PrefabInternal: {fileID: 0} | |||||
m_GameObject: {fileID: 914867460} | |||||
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} | |||||
m_LocalPosition: {x: 0, y: 0, z: 0} | |||||
m_LocalScale: {x: 1, y: 1, z: 1} | |||||
m_Children: [] | |||||
m_Father: {fileID: 0} | |||||
m_RootOrder: 0 | |||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} |
@ -0,0 +1,7 @@ | |||||
fileFormatVersion: 2 | |||||
guid: 072093f84b590984bab8d08bf3d32443 | |||||
DefaultImporter: | |||||
externalObjects: {} | |||||
userData: | |||||
assetBundleName: | |||||
assetBundleVariant: |
@ -0,0 +1,157 @@ | |||||
%YAML 1.1 | |||||
%TAG !u! tag:unity3d.com,2011: | |||||
--- !u!29 &1 | |||||
OcclusionCullingSettings: | |||||
m_ObjectHideFlags: 0 | |||||
serializedVersion: 2 | |||||
m_OcclusionBakeSettings: | |||||
smallestOccluder: 5 | |||||
smallestHole: 0.25 | |||||
backfaceThreshold: 100 | |||||
m_SceneGUID: 00000000000000000000000000000000 | |||||
m_OcclusionCullingData: {fileID: 0} | |||||
--- !u!104 &2 | |||||
RenderSettings: | |||||
m_ObjectHideFlags: 0 | |||||
serializedVersion: 9 | |||||
m_Fog: 0 | |||||
m_FogColor: {r: 0.5, g: 0.5, b: 0.5, a: 1} | |||||
m_FogMode: 3 | |||||
m_FogDensity: 0.01 | |||||
m_LinearFogStart: 0 | |||||
m_LinearFogEnd: 300 | |||||
m_AmbientSkyColor: {r: 0.212, g: 0.227, b: 0.259, a: 1} | |||||
m_AmbientEquatorColor: {r: 0.114, g: 0.125, b: 0.133, a: 1} | |||||
m_AmbientGroundColor: {r: 0.047, g: 0.043, b: 0.035, a: 1} | |||||
m_AmbientIntensity: 1 | |||||
m_AmbientMode: 3 | |||||
m_SubtractiveShadowColor: {r: 0.42, g: 0.478, b: 0.627, a: 1} | |||||
m_SkyboxMaterial: {fileID: 0} | |||||
m_HaloStrength: 0.5 | |||||
m_FlareStrength: 1 | |||||
m_FlareFadeSpeed: 3 | |||||
m_HaloTexture: {fileID: 0} | |||||
m_SpotCookie: {fileID: 10001, guid: 0000000000000000e000000000000000, type: 0} | |||||
m_DefaultReflectionMode: 0 | |||||
m_DefaultReflectionResolution: 128 | |||||
m_ReflectionBounces: 1 | |||||
m_ReflectionIntensity: 1 | |||||
m_CustomReflection: {fileID: 0} | |||||
m_Sun: {fileID: 0} | |||||
m_IndirectSpecularColor: {r: 0, g: 0, b: 0, a: 1} | |||||
m_UseRadianceAmbientProbe: 0 | |||||
--- !u!157 &3 | |||||
LightmapSettings: | |||||
m_ObjectHideFlags: 0 | |||||
serializedVersion: 11 | |||||
m_GIWorkflowMode: 1 | |||||
m_GISettings: | |||||
serializedVersion: 2 | |||||
m_BounceScale: 1 | |||||
m_IndirectOutputScale: 1 | |||||
m_AlbedoBoost: 1 | |||||
m_TemporalCoherenceThreshold: 1 | |||||
m_EnvironmentLightingMode: 0 | |||||
m_EnableBakedLightmaps: 0 | |||||
m_EnableRealtimeLightmaps: 0 | |||||
m_LightmapEditorSettings: | |||||
serializedVersion: 10 | |||||
m_Resolution: 2 | |||||
m_BakeResolution: 40 | |||||
m_AtlasSize: 1024 | |||||
m_AO: 0 | |||||
m_AOMaxDistance: 1 | |||||
m_CompAOExponent: 1 | |||||
m_CompAOExponentDirect: 0 | |||||
m_Padding: 2 | |||||
m_LightmapParameters: {fileID: 0} | |||||
m_LightmapsBakeMode: 1 | |||||
m_TextureCompression: 1 | |||||
m_FinalGather: 0 | |||||
m_FinalGatherFiltering: 1 | |||||
m_FinalGatherRayCount: 256 | |||||
m_ReflectionCompression: 2 | |||||
m_MixedBakeMode: 2 | |||||
m_BakeBackend: 1 | |||||
m_PVRSampling: 1 | |||||
m_PVRDirectSampleCount: 32 | |||||
m_PVRSampleCount: 500 | |||||
m_PVRBounces: 2 | |||||
m_PVRFilterTypeDirect: 0 | |||||
m_PVRFilterTypeIndirect: 0 | |||||
m_PVRFilterTypeAO: 0 | |||||
m_PVRFilteringMode: 1 | |||||
m_PVRCulling: 1 | |||||
m_PVRFilteringGaussRadiusDirect: 1 | |||||
m_PVRFilteringGaussRadiusIndirect: 5 | |||||
m_PVRFilteringGaussRadiusAO: 2 | |||||
m_PVRFilteringAtrousPositionSigmaDirect: 0.5 | |||||
m_PVRFilteringAtrousPositionSigmaIndirect: 2 | |||||
m_PVRFilteringAtrousPositionSigmaAO: 1 | |||||
m_ShowResolutionOverlay: 1 | |||||
m_LightingDataAsset: {fileID: 0} | |||||
m_UseShadowmask: 1 | |||||
--- !u!196 &4 | |||||
NavMeshSettings: | |||||
serializedVersion: 2 | |||||
m_ObjectHideFlags: 0 | |||||
m_BuildSettings: | |||||
serializedVersion: 2 | |||||
agentTypeID: 0 | |||||
agentRadius: 0.5 | |||||
agentHeight: 2 | |||||
agentSlope: 45 | |||||
agentClimb: 0.4 | |||||
ledgeDropHeight: 0 | |||||
maxJumpAcrossDistance: 0 | |||||
minRegionArea: 2 | |||||
manualCellSize: 0 | |||||
cellSize: 0.16666667 | |||||
manualTileSize: 0 | |||||
tileSize: 256 | |||||
accuratePlacement: 0 | |||||
debug: | |||||
m_Flags: 0 | |||||
m_NavMeshData: {fileID: 0} | |||||
--- !u!1 &555203924 | |||||
GameObject: | |||||
m_ObjectHideFlags: 0 | |||||
m_CorrespondingSourceObject: {fileID: 0} | |||||
m_PrefabInternal: {fileID: 0} | |||||
serializedVersion: 6 | |||||
m_Component: | |||||
- component: {fileID: 555203926} | |||||
- component: {fileID: 555203925} | |||||
m_Layer: 0 | |||||
m_Name: ServerManager | |||||
m_TagString: Untagged | |||||
m_Icon: {fileID: 0} | |||||
m_NavMeshLayer: 0 | |||||
m_StaticEditorFlags: 0 | |||||
m_IsActive: 1 | |||||
--- !u!114 &555203925 | |||||
MonoBehaviour: | |||||
m_ObjectHideFlags: 0 | |||||
m_CorrespondingSourceObject: {fileID: 0} | |||||
m_PrefabInternal: {fileID: 0} | |||||
m_GameObject: {fileID: 555203924} | |||||
m_Enabled: 1 | |||||
m_EditorHideFlags: 0 | |||||
m_Script: {fileID: 11500000, guid: 6f767c4220f486f49aba41a71b372a1f, type: 3} | |||||
m_Name: | |||||
m_EditorClassIdentifier: | |||||
StartOnAwake: 1 | |||||
Port: 4444 | |||||
--- !u!4 &555203926 | |||||
Transform: | |||||
m_ObjectHideFlags: 0 | |||||
m_CorrespondingSourceObject: {fileID: 0} | |||||
m_PrefabInternal: {fileID: 0} | |||||
m_GameObject: {fileID: 555203924} | |||||
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} | |||||
m_LocalPosition: {x: 0, y: 0, z: 0} | |||||
m_LocalScale: {x: 1, y: 1, z: 1} | |||||
m_Children: [] | |||||
m_Father: {fileID: 0} | |||||
m_RootOrder: 0 | |||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} |
@ -0,0 +1,7 @@ | |||||
fileFormatVersion: 2 | |||||
guid: 075d02d7077bafa4b9167ef449159fca | |||||
DefaultImporter: | |||||
externalObjects: {} | |||||
userData: | |||||
assetBundleName: | |||||
assetBundleVariant: |
@ -0,0 +1,8 @@ | |||||
fileFormatVersion: 2 | |||||
guid: 327c41c2e9379314baa6354c2ad8a92d | |||||
folderAsset: yes | |||||
DefaultImporter: | |||||
externalObjects: {} | |||||
userData: | |||||
assetBundleName: | |||||
assetBundleVariant: |
@ -0,0 +1,8 @@ | |||||
fileFormatVersion: 2 | |||||
guid: 27ae4060de79ea84d9854b62d3cd73ee | |||||
folderAsset: yes | |||||
DefaultImporter: | |||||
externalObjects: {} | |||||
userData: | |||||
assetBundleName: | |||||
assetBundleVariant: |
@ -0,0 +1,114 @@ | |||||
using System.Collections; | |||||
using System.Collections.Generic; | |||||
using UnityEngine; | |||||
using UnityEngine.Networking; | |||||
namespace Multiplayer | |||||
{ | |||||
public class ClientManager : MonoBehaviour | |||||
{ | |||||
#region Inspector Variables | |||||
[Header("Client Settings")] | |||||
[Tooltip("Address server is hosted on")] | |||||
[SerializeField] | |||||
private string ServerAddress = "127.0.0.1"; | |||||
[Tooltip("Port to connect to server on")] | |||||
[SerializeField] | |||||
private int Port = 4444; | |||||
[Tooltip("Connects client to server on awake")] | |||||
[SerializeField] | |||||
private bool ConnectOnStart = false; | |||||
public string Name; | |||||
#endregion | |||||
//Returns if client is current connected to a server | |||||
public bool IsConnected { get; private set; } | |||||
public byte ID { get; private set; } | |||||
//UNET client class which connects to the server; | |||||
private NetworkClient uNetClient; | |||||
/// <summary> | |||||
/// Call to connect client to host | |||||
/// </summary> | |||||
[ContextMenu("Start Client")] | |||||
public void StartClient() | |||||
{ | |||||
StartClient(ServerAddress, Port); | |||||
} | |||||
/// <summary> | |||||
/// Call to connect the client to the host | |||||
/// </summary> | |||||
/// <param name="ServerAddress">Server Address to connect to</param> | |||||
/// <param name="Port">Port to connect on</param> | |||||
public void StartClient(string ServerAddress, int Port) | |||||
{ | |||||
//If client is already connected to don't continue | |||||
if (IsConnected) | |||||
{ | |||||
Debug.Log("Client already connected to a server. Client needs to disconnect before it can connect to a server"); | |||||
return; | |||||
} | |||||
Debug.Log("Attempting to connect to: '" + ServerAddress + "' on port : '" + Port + "'"); | |||||
uNetClient = new NetworkClient(); | |||||
RegisterHandlers(uNetClient); | |||||
uNetClient.Configure(ChannelConfig.DefaultTopology()); | |||||
uNetClient.Connect(ServerAddress, Port); | |||||
} | |||||
/// <summary> | |||||
/// Helper function which sets up all necessary handlers for message types | |||||
/// </summary> | |||||
/// <param name="client"></param> | |||||
private void RegisterHandlers(NetworkClient client) | |||||
{ | |||||
client.RegisterHandler(MsgType.Connect, OnConnected); | |||||
client.RegisterHandler(MsgType.Disconnect, OnDisconnected); | |||||
} | |||||
private void OnConnected(NetworkMessage msg) | |||||
{ | |||||
IsConnected = true; | |||||
ID = (byte)msg.conn.connectionId; | |||||
Debug.Log("Successfully connected to server"); | |||||
Debug.Log("Connection ID: " + msg.conn.connectionId); | |||||
SendMessage(LoginMsgID.Name, new StringMsg(ID, Name)); | |||||
} | |||||
private void OnDisconnected(NetworkMessage msg) | |||||
{ | |||||
IsConnected = false; | |||||
Debug.Log("Disconnected from Server"); | |||||
} | |||||
/// <summary> | |||||
/// Sends message to server | |||||
/// </summary> | |||||
/// <param name="msgType">Message type, used to determin message handler</param> | |||||
/// <param name="msg">The message to send</param> | |||||
/// <param name="channel">Which channel to send on, by deafult sends on Reliable</param> | |||||
/// <param name="BackLog">If client isn't connected to server add to BackLog, by default true</param> | |||||
public void SendMessage(short msgType, MessageBase msg, ServerChannel channel = ServerChannel.Reliable) | |||||
{ | |||||
//if client isn't connected add to backlog | |||||
if (IsConnected) | |||||
{ | |||||
Debug.Log("Player not connected to server"); | |||||
} | |||||
uNetClient.SendByChannel(msgType, msg, (int)channel); | |||||
} | |||||
} | |||||
} |
@ -0,0 +1,11 @@ | |||||
fileFormatVersion: 2 | |||||
guid: 4286216c0dea5f64e8d5a8b674f60424 | |||||
MonoImporter: | |||||
externalObjects: {} | |||||
serializedVersion: 2 | |||||
defaultReferences: [] | |||||
executionOrder: 0 | |||||
icon: {instanceID: 0} | |||||
userData: | |||||
assetBundleName: | |||||
assetBundleVariant: |
@ -0,0 +1,8 @@ | |||||
fileFormatVersion: 2 | |||||
guid: 1ffdf64b9bb5ef145aa798d9faa3762e | |||||
folderAsset: yes | |||||
DefaultImporter: | |||||
externalObjects: {} | |||||
userData: | |||||
assetBundleName: | |||||
assetBundleVariant: |
@ -0,0 +1,50 @@ | |||||
using System.Collections; | |||||
using System.Collections.Generic; | |||||
using UnityEngine; | |||||
using UnityEngine.Networking; | |||||
namespace Multiplayer | |||||
{ | |||||
public static class ChannelConfig | |||||
{ | |||||
public static QosType[] GetDefaultChannels() | |||||
{ | |||||
QosType[] retVal = new QosType[3]; | |||||
retVal[0] = QosType.Reliable; | |||||
retVal[1] = QosType.StateUpdate; | |||||
retVal[2] = QosType.UnreliableSequenced; | |||||
return retVal; | |||||
} | |||||
public static HostTopology DefaultTopology() | |||||
{ | |||||
ConnectionConfig config = DefaultConfig(); | |||||
return new HostTopology(config, 8); | |||||
} | |||||
public static ConnectionConfig DefaultConfig() | |||||
{ | |||||
ConnectionConfig DefaultConfig = new ConnectionConfig(); | |||||
QosType[] Channels = GetDefaultChannels(); | |||||
foreach (QosType channel in Channels) | |||||
DefaultConfig.AddChannel(channel); | |||||
return DefaultConfig; | |||||
} | |||||
} | |||||
public enum ServerChannel | |||||
{ | |||||
Reliable = 0, | |||||
StateUpdate = 1, | |||||
UnreliableSequenced = 2, | |||||
} | |||||
} |
@ -0,0 +1,11 @@ | |||||
fileFormatVersion: 2 | |||||
guid: 016335d6e296c0b42a4f0eaa04459635 | |||||
MonoImporter: | |||||
externalObjects: {} | |||||
serializedVersion: 2 | |||||
defaultReferences: [] | |||||
executionOrder: 0 | |||||
icon: {instanceID: 0} | |||||
userData: | |||||
assetBundleName: | |||||
assetBundleVariant: |
@ -0,0 +1,12 @@ | |||||
using System.Collections; | |||||
using System.Collections.Generic; | |||||
using UnityEngine; | |||||
namespace Multiplayer | |||||
{ | |||||
public class LoginMsgID | |||||
{ | |||||
public const short QueryName = 200; | |||||
public const short Name = 201; | |||||
} | |||||
} |
@ -0,0 +1,11 @@ | |||||
fileFormatVersion: 2 | |||||
guid: f6d48a9f61750cd4ca5b651ad1c34bb7 | |||||
MonoImporter: | |||||
externalObjects: {} | |||||
serializedVersion: 2 | |||||
defaultReferences: [] | |||||
executionOrder: 0 | |||||
icon: {instanceID: 0} | |||||
userData: | |||||
assetBundleName: | |||||
assetBundleVariant: |
@ -0,0 +1,117 @@ | |||||
using System.Collections; | |||||
using System.Collections.Generic; | |||||
using UnityEngine; | |||||
using UnityEngine.Networking; | |||||
namespace Multiplayer | |||||
{ | |||||
public class PlayerMsgID | |||||
{ | |||||
public const short Position = 200; | |||||
public const short Rotation = 201; | |||||
} | |||||
public class PlayerMsg : MessageBase | |||||
{ | |||||
public ushort playerID; | |||||
public PlayerMsg() { } | |||||
public PlayerMsg(ushort playerID) | |||||
{ | |||||
this.playerID = playerID; | |||||
} | |||||
public override void Serialize(NetworkWriter writer) | |||||
{ | |||||
writer.Write(playerID); | |||||
} | |||||
public override void Deserialize(NetworkReader reader) | |||||
{ | |||||
playerID = reader.ReadUInt16(); | |||||
} | |||||
} | |||||
public class VectorMsg : PlayerMsg | |||||
{ | |||||
public Vector3 vector; | |||||
public VectorMsg() { } | |||||
public VectorMsg(ushort PlayerID, Vector3 Vector) : base(PlayerID) | |||||
{ | |||||
this.vector = Vector; | |||||
} | |||||
public override void Serialize(NetworkWriter writer) | |||||
{ | |||||
base.Serialize(writer); | |||||
writer.Write(vector); | |||||
} | |||||
public override void Deserialize(NetworkReader reader) | |||||
{ | |||||
base.Deserialize(reader); | |||||
vector = reader.ReadVector3(); | |||||
} | |||||
} | |||||
public class QuaternionMsg : PlayerMsg | |||||
{ | |||||
public Quaternion quaternion; | |||||
public QuaternionMsg() { } | |||||
public QuaternionMsg(ushort PlayerID, Quaternion quaternion) : base(PlayerID) | |||||
{ | |||||
this.quaternion = quaternion; | |||||
} | |||||
public override void Serialize(NetworkWriter writer) | |||||
{ | |||||
base.Serialize(writer); | |||||
writer.Write(quaternion); | |||||
} | |||||
public override void Deserialize(NetworkReader reader) | |||||
{ | |||||
base.Deserialize(reader); | |||||
quaternion = reader.ReadQuaternion(); | |||||
} | |||||
} | |||||
public class StringMsg : PlayerMsg | |||||
{ | |||||
public string String; | |||||
public StringMsg() { } | |||||
public StringMsg(ushort PlayerID, string String) : base(PlayerID) | |||||
{ | |||||
this.String = String; | |||||
} | |||||
public override void Serialize(NetworkWriter writer) | |||||
{ | |||||
base.Serialize(writer); | |||||
writer.Write(String); | |||||
} | |||||
public override void Deserialize(NetworkReader reader) | |||||
{ | |||||
base.Deserialize(reader); | |||||
String = reader.ReadString(); | |||||
} | |||||
} | |||||
} |
@ -0,0 +1,11 @@ | |||||
fileFormatVersion: 2 | |||||
guid: 9ec233016f3f5db4a84bdf1ee5ca34d5 | |||||
MonoImporter: | |||||
externalObjects: {} | |||||
serializedVersion: 2 | |||||
defaultReferences: [] | |||||
executionOrder: 0 | |||||
icon: {instanceID: 0} | |||||
userData: | |||||
assetBundleName: | |||||
assetBundleVariant: |
@ -0,0 +1,36 @@ | |||||
using System.Collections; | |||||
using System.Collections.Generic; | |||||
using UnityEngine; | |||||
using UnityEngine.Networking; | |||||
namespace Multiplayer | |||||
{ | |||||
public static class Utility | |||||
{ | |||||
public static bool ReadMessage<T>(NetworkMessage msg, out T outMsg, bool logError = true) where T : MessageBase, new() | |||||
{ | |||||
outMsg = new T(); | |||||
bool isValid = true; | |||||
try | |||||
{ | |||||
outMsg = msg.ReadMessage<T>(); | |||||
isValid = true; | |||||
} | |||||
catch (UnityException e) | |||||
{ | |||||
isValid = false; | |||||
msg.reader.SeekZero(); | |||||
#if UNITY_EDITOR | |||||
Debug.LogError(e); | |||||
#endif | |||||
} | |||||
return isValid; | |||||
} | |||||
} | |||||
} |
@ -0,0 +1,11 @@ | |||||
fileFormatVersion: 2 | |||||
guid: efc41e9e8a034494cb4a4e8d3d09cd9b | |||||
MonoImporter: | |||||
externalObjects: {} | |||||
serializedVersion: 2 | |||||
defaultReferences: [] | |||||
executionOrder: 0 | |||||
icon: {instanceID: 0} | |||||
userData: | |||||
assetBundleName: | |||||
assetBundleVariant: |
@ -0,0 +1,8 @@ | |||||
fileFormatVersion: 2 | |||||
guid: b0e9417df7ca8104196e1e80eb099f13 | |||||
folderAsset: yes | |||||
DefaultImporter: | |||||
externalObjects: {} | |||||
userData: | |||||
assetBundleName: | |||||
assetBundleVariant: |
@ -0,0 +1,48 @@ | |||||
using System.Collections; | |||||
using System.Collections.Generic; | |||||
using UnityEngine; | |||||
using UnityEngine.Networking; | |||||
namespace Multiplayer | |||||
{ | |||||
public class LoginManager | |||||
{ | |||||
private ServerManager ServerManager; | |||||
public LoginManager(ServerManager ServerManager) | |||||
{ | |||||
this.ServerManager = ServerManager; | |||||
RegisterHandles(); | |||||
} | |||||
public void RegisterHandles() | |||||
{ | |||||
ServerManager.Server.RegisterHandler(LoginMsgID.Name, OnPlayerLogin); | |||||
} | |||||
//Called when a player logs in | |||||
public void OnPlayerLogin(NetworkMessage msg) | |||||
{ | |||||
StringMsg stringMsg; | |||||
if (!Utility.ReadMessage<StringMsg>(msg, out stringMsg)) | |||||
return; | |||||
Debug.Log("Player login: " + stringMsg.String); | |||||
if (ServerManager.AllPlayers.ContainsKey(stringMsg.playerID)) | |||||
{ | |||||
Debug.Log(stringMsg.String + " already logged in"); | |||||
return; | |||||
} | |||||
Player newPlayer = new Player(msg.conn, stringMsg.String); | |||||
ServerManager.AllPlayers.Add(newPlayer.ID, newPlayer); | |||||
} | |||||
} | |||||
} |
@ -0,0 +1,11 @@ | |||||
fileFormatVersion: 2 | |||||
guid: abaefae4f67ed3649a0c9403438fde91 | |||||
MonoImporter: | |||||
externalObjects: {} | |||||
serializedVersion: 2 | |||||
defaultReferences: [] | |||||
executionOrder: 0 | |||||
icon: {instanceID: 0} | |||||
userData: | |||||
assetBundleName: | |||||
assetBundleVariant: |
@ -0,0 +1,33 @@ | |||||
using System.Collections; | |||||
using System.Collections.Generic; | |||||
using UnityEngine; | |||||
using UnityEngine.Networking; | |||||
namespace Multiplayer | |||||
{ | |||||
public class Player | |||||
{ | |||||
public byte ID; | |||||
public NetworkConnection Connection; | |||||
public Vector3 Position { get; private set; } | |||||
public Quaternion Rotation { get; private set; } | |||||
public string Name { get; private set; } | |||||
public Player(NetworkConnection Connection, string Name) | |||||
{ | |||||
this.ID = (byte)Connection.connectionId; | |||||
this.Connection = Connection; | |||||
this.Name = Name; | |||||
} | |||||
} | |||||
} |
@ -0,0 +1,11 @@ | |||||
fileFormatVersion: 2 | |||||
guid: ee044eb57634ca44288ae705c819ca76 | |||||
MonoImporter: | |||||
externalObjects: {} | |||||
serializedVersion: 2 | |||||
defaultReferences: [] | |||||
executionOrder: 0 | |||||
icon: {instanceID: 0} | |||||
userData: | |||||
assetBundleName: | |||||
assetBundleVariant: |
@ -0,0 +1,86 @@ | |||||
using System.Collections; | |||||
using System.Collections.Generic; | |||||
using UnityEngine; | |||||
using UnityEngine.Networking; | |||||
namespace Multiplayer | |||||
{ | |||||
public class ServerManager : MonoBehaviour | |||||
{ | |||||
[SerializeField] | |||||
private bool StartOnAwake = true; | |||||
[SerializeField] | |||||
private int Port = 4444; | |||||
public Dictionary<int, Player> AllPlayers; | |||||
public NetworkServerSimple Server { get; private set; } | |||||
private LoginManager loginManager; | |||||
private void Start() | |||||
{ | |||||
if (StartOnAwake) | |||||
StartServer(); | |||||
} | |||||
[ContextMenu("Start Server")] | |||||
public void StartServer() | |||||
{ | |||||
StartServer(Port); | |||||
} | |||||
public void StartServer(int Port) | |||||
{ | |||||
this.Port = Port; | |||||
AllPlayers = new Dictionary<int, Player>(); | |||||
Server = new NetworkServerSimple(); | |||||
Server.RegisterHandler(MsgType.Connect, OnConnect); | |||||
Server.RegisterHandler(MsgType.Disconnect, OnDisconnect); | |||||
Server.Configure(ChannelConfig.DefaultTopology()); | |||||
Debug.Log("Starting Server on " + Port); | |||||
loginManager = new LoginManager(this); | |||||
Server.Listen(Port); | |||||
} | |||||
private void Update() | |||||
{ | |||||
if (Server != null) | |||||
Server.Update(); | |||||
} | |||||
private void OnConnect(NetworkMessage msg) | |||||
{ | |||||
Debug.Log("New Connection: \n Address: " + msg.conn.address + "\n ID: " + msg.conn.connectionId); | |||||
//msg.conn.Send(LoginMsgID.QueryName, new PlayerMsg((byte)msg.conn.connectionId)); | |||||
} | |||||
private void OnDisconnect(NetworkMessage msg) | |||||
{ | |||||
if (AllPlayers.ContainsKey(msg.conn.connectionId)) | |||||
{ | |||||
Debug.Log(AllPlayers[msg.conn.connectionId].Name + "has disconnected."); | |||||
AllPlayers.Remove(msg.conn.connectionId); | |||||
} | |||||
else | |||||
Debug.Log("Unkown player has disconnected."); | |||||
} | |||||
} | |||||
} |
@ -0,0 +1,11 @@ | |||||
fileFormatVersion: 2 | |||||
guid: 6f767c4220f486f49aba41a71b372a1f | |||||
MonoImporter: | |||||
externalObjects: {} | |||||
serializedVersion: 2 | |||||
defaultReferences: [] | |||||
executionOrder: 0 | |||||
icon: {instanceID: 0} | |||||
userData: | |||||
assetBundleName: | |||||
assetBundleVariant: |
@ -0,0 +1,8 @@ | |||||
fileFormatVersion: 2 | |||||
guid: c026360f17cae77439eaac0f099f965e | |||||
folderAsset: yes | |||||
DefaultImporter: | |||||
externalObjects: {} | |||||
userData: | |||||
assetBundleName: | |||||
assetBundleVariant: |