Add Server and client
This commit is contained in:
parent
3d7c9e3f7f
commit
6258438d44
262 changed files with 2661 additions and 0 deletions
BIN
Unity network UDP/Assets/DupeHeightMap.png
Normal file
BIN
Unity network UDP/Assets/DupeHeightMap.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 79 KiB |
57
Unity network UDP/Assets/DupeHeightMap.png.meta
Normal file
57
Unity network UDP/Assets/DupeHeightMap.png.meta
Normal file
|
@ -0,0 +1,57 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 0c3a130c5911fb94da97ee37073f4b81
|
||||
timeCreated: 1461176781
|
||||
licenseType: Free
|
||||
TextureImporter:
|
||||
fileIDToRecycleName: {}
|
||||
serializedVersion: 2
|
||||
mipmaps:
|
||||
mipMapMode: 0
|
||||
enableMipMap: 1
|
||||
linearTexture: 0
|
||||
correctGamma: 0
|
||||
fadeOut: 0
|
||||
borderMipMap: 0
|
||||
mipMapFadeDistanceStart: 1
|
||||
mipMapFadeDistanceEnd: 3
|
||||
bumpmap:
|
||||
convertToNormalMap: 0
|
||||
externalNormalMap: 0
|
||||
heightScale: 0.25
|
||||
normalMapFilter: 0
|
||||
isReadable: 0
|
||||
grayScaleToAlpha: 0
|
||||
generateCubemap: 0
|
||||
cubemapConvolution: 0
|
||||
cubemapConvolutionSteps: 7
|
||||
cubemapConvolutionExponent: 1.5
|
||||
seamlessCubemap: 0
|
||||
textureFormat: -1
|
||||
maxTextureSize: 2048
|
||||
textureSettings:
|
||||
filterMode: -1
|
||||
aniso: -1
|
||||
mipBias: -1
|
||||
wrapMode: -1
|
||||
nPOTScale: 1
|
||||
lightmap: 0
|
||||
rGBM: 0
|
||||
compressionQuality: 50
|
||||
allowsAlphaSplitting: 0
|
||||
spriteMode: 0
|
||||
spriteExtrude: 1
|
||||
spriteMeshType: 1
|
||||
alignment: 0
|
||||
spritePivot: {x: 0.5, y: 0.5}
|
||||
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
|
||||
spritePixelsToUnits: 100
|
||||
alphaIsTransparency: 0
|
||||
textureType: -1
|
||||
buildTargetSettings: []
|
||||
spriteSheet:
|
||||
sprites: []
|
||||
outline: []
|
||||
spritePackingTag:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
BIN
Unity network UDP/Assets/New Terrain.asset
Normal file
BIN
Unity network UDP/Assets/New Terrain.asset
Normal file
Binary file not shown.
8
Unity network UDP/Assets/New Terrain.asset.meta
Normal file
8
Unity network UDP/Assets/New Terrain.asset.meta
Normal file
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: a650dbd5d579bc8499d32782b7c9104c
|
||||
timeCreated: 1461172757
|
||||
licenseType: Free
|
||||
NativeFormatImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
BIN
Unity network UDP/Assets/heightmap.map
Normal file
BIN
Unity network UDP/Assets/heightmap.map
Normal file
Binary file not shown.
8
Unity network UDP/Assets/heightmap.map.meta
Normal file
8
Unity network UDP/Assets/heightmap.map.meta
Normal file
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 24249e1e07f50b94d823e5b641b07f67
|
||||
timeCreated: 1461186732
|
||||
licenseType: Free
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
9
Unity network UDP/Assets/scenes.meta
Normal file
9
Unity network UDP/Assets/scenes.meta
Normal file
|
@ -0,0 +1,9 @@
|
|||
fileFormatVersion: 2
|
||||
guid: cb3879966b4e9284e8e630de5266c03e
|
||||
folderAsset: yes
|
||||
timeCreated: 1461077713
|
||||
licenseType: Free
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
BIN
Unity network UDP/Assets/scenes/GameMenu.unity
Normal file
BIN
Unity network UDP/Assets/scenes/GameMenu.unity
Normal file
Binary file not shown.
8
Unity network UDP/Assets/scenes/GameMenu.unity.meta
Normal file
8
Unity network UDP/Assets/scenes/GameMenu.unity.meta
Normal file
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 4f84caedd08afdb4aa9abf11a8f90022
|
||||
timeCreated: 1461152442
|
||||
licenseType: Free
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
BIN
Unity network UDP/Assets/scenes/MainMenu.unity
Normal file
BIN
Unity network UDP/Assets/scenes/MainMenu.unity
Normal file
Binary file not shown.
8
Unity network UDP/Assets/scenes/MainMenu.unity.meta
Normal file
8
Unity network UDP/Assets/scenes/MainMenu.unity.meta
Normal file
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 67b343623e0fb7e45af3f4acd9d07b43
|
||||
timeCreated: 1461077713
|
||||
licenseType: Free
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
9
Unity network UDP/Assets/scripts.meta
Normal file
9
Unity network UDP/Assets/scripts.meta
Normal file
|
@ -0,0 +1,9 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 3b4bdb98a5880ed458e0b2db273cbd75
|
||||
folderAsset: yes
|
||||
timeCreated: 1461077658
|
||||
licenseType: Free
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
57
Unity network UDP/Assets/scripts/HeightmapExportPNG.js
Normal file
57
Unity network UDP/Assets/scripts/HeightmapExportPNG.js
Normal file
|
@ -0,0 +1,57 @@
|
|||
import UnityEngine;
|
||||
import UnityEditor;
|
||||
import System.Collections;
|
||||
import System.IO;
|
||||
|
||||
class HeightmapExportPNG extends EditorWindow
|
||||
{
|
||||
static var terraindata : TerrainData;
|
||||
|
||||
@MenuItem ("Terrain/Export Height Map as PNG")
|
||||
static function Init () {
|
||||
terraindata = null;
|
||||
var terrain : Terrain = null;
|
||||
|
||||
if ( Selection.activeGameObject )
|
||||
terrain = Selection.activeGameObject.GetComponent( Terrain );
|
||||
|
||||
if (!terrain) {
|
||||
terrain = Terrain.activeTerrain;
|
||||
}
|
||||
if (terrain) {
|
||||
terraindata = terrain.terrainData;
|
||||
}
|
||||
if (terraindata == null) {
|
||||
EditorUtility.DisplayDialog("No terrain selected", "Please select a terrain.", "Cancel");
|
||||
return;
|
||||
}
|
||||
|
||||
//// get the terrain heights into an array and apply them to a texture2D
|
||||
var myBytes : byte[];
|
||||
var myIndex : int = 0;
|
||||
var rawHeights = new Array(0.0,0.0);
|
||||
var duplicateHeightMap = new Texture2D(terraindata.heightmapWidth, terraindata.heightmapHeight, TextureFormat.ARGB32, false);
|
||||
rawHeights = terraindata.GetHeights(0, 0, terraindata.heightmapWidth, terraindata.heightmapHeight);
|
||||
|
||||
/// run through the array row by row
|
||||
for (y=0; y < duplicateHeightMap.height; ++y)
|
||||
{
|
||||
for (x=0; x < duplicateHeightMap.width; ++x)
|
||||
{
|
||||
/// for wach pixel set RGB to the same so it's gray
|
||||
var data = rawHeights[myIndex];
|
||||
var color = Vector4(data, data,data, 1.0);
|
||||
duplicateHeightMap.SetPixel (x, y, color);
|
||||
myIndex++;
|
||||
}
|
||||
}
|
||||
// Apply all SetPixel calls
|
||||
duplicateHeightMap.Apply();
|
||||
|
||||
/// make it a PNG and save it to the Assets folder
|
||||
myBytes = duplicateHeightMap.EncodeToPNG();
|
||||
var filename : String = "DupeHeightMap.png";
|
||||
File.WriteAllBytes(Application.dataPath + "/" + filename, myBytes);
|
||||
EditorUtility.DisplayDialog("Heightmap Duplicated", "Saved as PNG in Assets/ as: " + filename, "");
|
||||
}
|
||||
}
|
12
Unity network UDP/Assets/scripts/HeightmapExportPNG.js.meta
Normal file
12
Unity network UDP/Assets/scripts/HeightmapExportPNG.js.meta
Normal file
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 377b3e4ac5993cb4785ccd432840d615
|
||||
timeCreated: 1461176678
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
9
Unity network UDP/Assets/scripts/editor.meta
Normal file
9
Unity network UDP/Assets/scripts/editor.meta
Normal file
|
@ -0,0 +1,9 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 948a750a1a0784a4188bf7767a99be0a
|
||||
folderAsset: yes
|
||||
timeCreated: 1461178066
|
||||
licenseType: Free
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
85
Unity network UDP/Assets/scripts/editor/TerrainExport.cs
Normal file
85
Unity network UDP/Assets/scripts/editor/TerrainExport.cs
Normal file
|
@ -0,0 +1,85 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using UnityEngine;
|
||||
using System.IO;
|
||||
using UnityEditor;
|
||||
|
||||
class TerrainExport : EditorWindow
|
||||
{
|
||||
|
||||
static TerrainData terraindata;
|
||||
|
||||
[MenuItem("Terrain/Export Height Map as MAP")]
|
||||
static void Init()
|
||||
{
|
||||
terraindata = null;
|
||||
Terrain terrain = null;
|
||||
|
||||
if (Selection.activeGameObject)
|
||||
terrain = Selection.activeGameObject.GetComponent<Terrain>();
|
||||
|
||||
if (!terrain)
|
||||
{
|
||||
terrain = Terrain.activeTerrain;
|
||||
}
|
||||
if (terrain)
|
||||
{
|
||||
terraindata = terrain.terrainData;
|
||||
}
|
||||
if (terraindata == null)
|
||||
{
|
||||
EditorUtility.DisplayDialog("No terrain selected", "Please select a terrain.", "Cancel");
|
||||
return;
|
||||
}
|
||||
|
||||
float[,] rawHeights = terraindata.GetHeights(0, 0, terraindata.heightmapWidth, terraindata.heightmapHeight);
|
||||
byte[] data = new byte[terraindata.heightmapWidth * terraindata.heightmapHeight * 4 + 16];
|
||||
|
||||
int a = terraindata.heightmapWidth;
|
||||
data[0] = (byte)((a >> 24) & 0xff);
|
||||
data[1] = (byte)((a >> 16) & 0xff);
|
||||
data[2] = (byte)((a >> 8) & 0xff);
|
||||
data[3] = (byte)((a >> 0) & 0xff);
|
||||
|
||||
a = terraindata.heightmapHeight;
|
||||
data[4] = (byte)((a >> 24) & 0xff);
|
||||
data[5] = (byte)((a >> 16) & 0xff);
|
||||
data[6] = (byte)((a >> 8) & 0xff);
|
||||
data[7] = (byte)((a >> 0) & 0xff);
|
||||
|
||||
int pointer = 8;
|
||||
|
||||
byte[] array = BitConverter.GetBytes(terraindata.size.x);
|
||||
foreach (byte b in array)
|
||||
{
|
||||
data[pointer++] = b;
|
||||
}
|
||||
|
||||
array = BitConverter.GetBytes(terraindata.size.z);
|
||||
foreach (byte b in array)
|
||||
{
|
||||
data[pointer++] = b;
|
||||
}
|
||||
|
||||
float max = 0;
|
||||
for (int x = 0; x < terraindata.heightmapWidth; x++)
|
||||
{
|
||||
for (int y = 0; y < terraindata.heightmapHeight; y++)
|
||||
{
|
||||
float s = rawHeights[y, x] * terraindata.heightmapScale.y;
|
||||
max = (s > max) ? s : max;
|
||||
array = BitConverter.GetBytes(s);
|
||||
foreach (byte b in array)
|
||||
{
|
||||
data[pointer++] = b;
|
||||
}
|
||||
}
|
||||
}
|
||||
File.WriteAllBytes(Application.dataPath + "/" + "heightmap.map", data);
|
||||
EditorUtility.DisplayDialog("Heightmap Duplicated", "Saved as PNG in Assets/ as: " + "heightmap.map maxheight:" + max + " width:" + terraindata.size.x + " height:" + terraindata.size.z, "Ok");
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: e4e69e764e1d0e444886b477010ea874
|
||||
timeCreated: 1461178068
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
9
Unity network UDP/Assets/scripts/gamestate.meta
Normal file
9
Unity network UDP/Assets/scripts/gamestate.meta
Normal file
|
@ -0,0 +1,9 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 4433c6aa3dfe38c448fd03c00ae00b54
|
||||
folderAsset: yes
|
||||
timeCreated: 1461099886
|
||||
licenseType: Free
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
25
Unity network UDP/Assets/scripts/gamestate/GameState.cs
Normal file
25
Unity network UDP/Assets/scripts/gamestate/GameState.cs
Normal file
|
@ -0,0 +1,25 @@
|
|||
using System;
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
class GameState : MonoBehaviour
|
||||
{
|
||||
|
||||
void Start()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void OnGUI()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
12
Unity network UDP/Assets/scripts/gamestate/GameState.cs.meta
Normal file
12
Unity network UDP/Assets/scripts/gamestate/GameState.cs.meta
Normal file
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 7764a314530f3b44686c82db227ddf1e
|
||||
timeCreated: 1461152479
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
192
Unity network UDP/Assets/scripts/gamestate/MainState.cs
Normal file
192
Unity network UDP/Assets/scripts/gamestate/MainState.cs
Normal file
|
@ -0,0 +1,192 @@
|
|||
using System;
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
class MainState : MonoBehaviour
|
||||
{
|
||||
public string stringPseudo = "Pseudo";
|
||||
public string stringHost = "IP:PORT";
|
||||
private int preStatus = -1;
|
||||
|
||||
public float preTime = 0;
|
||||
public Boolean windowOpen = false;
|
||||
public int winID = -1;
|
||||
public Rect windowRect = new Rect(Screen.width / 2 - 100, Screen.height / 2 - 50, 200, 100);
|
||||
|
||||
void Start()
|
||||
{
|
||||
windowRect = new Rect(Screen.width / 2 - 100, Screen.height / 2 - 50, 200, 100);
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if(preStatus != MainClient.connected)
|
||||
{
|
||||
if (MainClient.connected == MainState_Connection_Request_Packet.REQUEST)
|
||||
{
|
||||
preTime = Time.time;
|
||||
}
|
||||
if (MainClient.connected == MainState_Connection_Request_Packet.ACCEPTED)
|
||||
{
|
||||
winID = 0;
|
||||
windowOpen = true;
|
||||
}
|
||||
if (MainClient.connected == MainState_Connection_Request_Packet.REFUSED)
|
||||
{
|
||||
winID = 5;
|
||||
windowOpen = true;
|
||||
}
|
||||
if (MainClient.connected == MainState_Connection_Request_Packet.TIME_OUT)
|
||||
{
|
||||
winID = 4;
|
||||
windowOpen = true;
|
||||
MainClient.client.disconnect();
|
||||
}
|
||||
if (MainClient.connected == MainState_Connection_Request_Packet.ALREADY_CONNECTED)
|
||||
{
|
||||
winID = 7;
|
||||
windowOpen = true;
|
||||
}
|
||||
if (MainClient.connected == MainState_Connection_Request_Packet.PSEUDO_INCORRECT)
|
||||
{
|
||||
winID = 2;
|
||||
windowOpen = true;
|
||||
}
|
||||
preStatus = MainClient.connected;
|
||||
}
|
||||
if(preStatus == 0 && preStatus == MainClient.connected)
|
||||
{
|
||||
if ((Time.time - preTime) >= 10)
|
||||
{
|
||||
MainClient.connected = MainState_Connection_Request_Packet.TIME_OUT;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OnGUI()
|
||||
{
|
||||
GUILayout.BeginArea(new Rect(Screen.width/2 - 50, Screen.height / 2 - 50, 100, 100));
|
||||
stringHost = GUILayout.TextField(stringHost);
|
||||
stringPseudo = GUILayout.TextField(stringPseudo);
|
||||
if (GUILayout.Button("Login"))
|
||||
{
|
||||
if(stringHost.Length > 0)
|
||||
{
|
||||
if(stringPseudo.Length > 0)
|
||||
{
|
||||
string[] c = stringHost.Split(':');
|
||||
string host = "";
|
||||
int port = 9999;
|
||||
if (c.Length == 1)
|
||||
{
|
||||
host = c[0];
|
||||
MainClient.connect(host, port, stringPseudo);
|
||||
winID = 6;
|
||||
windowOpen = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
host = c[0];
|
||||
if (Int32.TryParse(c[1], out port))
|
||||
{
|
||||
MainClient.connect(host, port, stringPseudo);
|
||||
winID = 6;
|
||||
windowOpen = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
winID = 1;
|
||||
windowOpen = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
winID = 2;
|
||||
windowOpen = true;
|
||||
}
|
||||
|
||||
}else
|
||||
{
|
||||
winID = 3;
|
||||
windowOpen = true;
|
||||
}
|
||||
}
|
||||
GUILayout.EndArea();
|
||||
if (windowOpen)
|
||||
{
|
||||
GUI.Box(windowRect, "");
|
||||
GUI.Box(windowRect, "");
|
||||
GUI.Box(windowRect, "");
|
||||
GUI.Box(windowRect, "");
|
||||
windowRect = GUILayout.Window(winID, windowRect, connectedWindow, "Information");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void connectedWindow(int windowID)
|
||||
{
|
||||
if(windowID == 0)
|
||||
{
|
||||
GUILayout.Label("Connexion réussi !");
|
||||
if (GUILayout.Button("Valider"))
|
||||
{
|
||||
windowOpen = false;
|
||||
}
|
||||
}
|
||||
else if (windowID == 1)
|
||||
{
|
||||
GUILayout.Label("Le Port entré est incorrect !");
|
||||
if (GUILayout.Button("Valider"))
|
||||
{
|
||||
windowOpen = false;
|
||||
}
|
||||
}
|
||||
else if (windowID == 2)
|
||||
{
|
||||
GUILayout.Label("Pseudo incorrect !");
|
||||
if (GUILayout.Button("Valider"))
|
||||
{
|
||||
windowOpen = false;
|
||||
}
|
||||
}
|
||||
else if (windowID == 3)
|
||||
{
|
||||
GUILayout.Label("Aucune ip entré !");
|
||||
if (GUILayout.Button("Valider"))
|
||||
{
|
||||
windowOpen = false;
|
||||
}
|
||||
}
|
||||
else if (windowID == 4)
|
||||
{
|
||||
GUILayout.Label("Echec de la connexion");
|
||||
if (GUILayout.Button("Valider"))
|
||||
{
|
||||
windowOpen = false;
|
||||
}
|
||||
}
|
||||
else if (windowID == 5)
|
||||
{
|
||||
GUILayout.Label("Connexion refusée !");
|
||||
if (GUILayout.Button("Valider"))
|
||||
{
|
||||
windowOpen = false;
|
||||
}
|
||||
}
|
||||
else if (windowID == 6)
|
||||
{
|
||||
GUILayout.Label("Connexion en cours...");
|
||||
}
|
||||
else if (windowID == 7)
|
||||
{
|
||||
GUILayout.Label("Vous etes déja connecté !");
|
||||
if (GUILayout.Button("Valider"))
|
||||
{
|
||||
windowOpen = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
12
Unity network UDP/Assets/scripts/gamestate/MainState.cs.meta
Normal file
12
Unity network UDP/Assets/scripts/gamestate/MainState.cs.meta
Normal file
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: efde826cbfe60a64ebd845c9080b1d6b
|
||||
timeCreated: 1461099889
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
9
Unity network UDP/Assets/scripts/network.meta
Normal file
9
Unity network UDP/Assets/scripts/network.meta
Normal file
|
@ -0,0 +1,9 @@
|
|||
fileFormatVersion: 2
|
||||
guid: e2fc0e66fe770324a8c90d0be6e109c0
|
||||
folderAsset: yes
|
||||
timeCreated: 1461077665
|
||||
licenseType: Free
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
60
Unity network UDP/Assets/scripts/network/Client.cs
Normal file
60
Unity network UDP/Assets/scripts/network/Client.cs
Normal file
|
@ -0,0 +1,60 @@
|
|||
using System;
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading;
|
||||
|
||||
|
||||
class Client
|
||||
{
|
||||
private UdpClient client;
|
||||
private IPEndPoint ep;
|
||||
private Thread receiveThread;
|
||||
|
||||
private volatile Boolean connected = false;
|
||||
|
||||
public Client(String ip,int port)
|
||||
{
|
||||
ep = new IPEndPoint(IPAddress.Parse(ip), port);
|
||||
client = new UdpClient();
|
||||
receiveThread = new Thread(new ThreadStart(loop));
|
||||
Register.registerClass();
|
||||
}
|
||||
|
||||
public void connect()
|
||||
{
|
||||
client.Connect(ep);
|
||||
connected = true;
|
||||
receiveThread.Start();
|
||||
}
|
||||
|
||||
public void loop()
|
||||
{
|
||||
while (connected)
|
||||
{
|
||||
byte[] data = client.Receive(ref ep);
|
||||
DataBuffer dataBuffer = new DataBuffer(data);
|
||||
int id = dataBuffer.getInt();
|
||||
IPacket packet = (IPacket)Register.instantiate(id);
|
||||
packet.read(dataBuffer);
|
||||
packet.manage(data, this);
|
||||
}
|
||||
}
|
||||
|
||||
public void send(IPacket packet)
|
||||
{
|
||||
DataBuffer data = new DataBuffer();
|
||||
packet.write(data);
|
||||
client.Send(data.getData(), data.getData().Length);
|
||||
}
|
||||
|
||||
public void disconnect()
|
||||
{
|
||||
client.Close();
|
||||
connected = false;
|
||||
}
|
||||
|
||||
}
|
12
Unity network UDP/Assets/scripts/network/Client.cs.meta
Normal file
12
Unity network UDP/Assets/scripts/network/Client.cs.meta
Normal file
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 6ef5ac19c402f2c42a01c3f10df1e3e0
|
||||
timeCreated: 1461077674
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
22
Unity network UDP/Assets/scripts/network/MainClient.cs
Normal file
22
Unity network UDP/Assets/scripts/network/MainClient.cs
Normal file
|
@ -0,0 +1,22 @@
|
|||
using UnityEngine;
|
||||
|
||||
class MainClient {
|
||||
|
||||
public static Client client;
|
||||
public static string pseudo;
|
||||
public static int connected = -1;
|
||||
|
||||
public static void connect (string host,int port,string pseudo) {
|
||||
client = new Client(host, port);
|
||||
client.connect();
|
||||
connected = 0;
|
||||
client.send(new MainState_Connection_Request_Packet(pseudo, 0));
|
||||
}
|
||||
|
||||
|
||||
public static void OnGUI()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
12
Unity network UDP/Assets/scripts/network/MainClient.cs.meta
Normal file
12
Unity network UDP/Assets/scripts/network/MainClient.cs.meta
Normal file
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 353dd1b92e911804cba3e451a2a5aa47
|
||||
timeCreated: 1461092835
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
9
Unity network UDP/Assets/scripts/network/common.meta
Normal file
9
Unity network UDP/Assets/scripts/network/common.meta
Normal file
|
@ -0,0 +1,9 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 49ebafc3208dd5e42b7dcba1440f6f45
|
||||
folderAsset: yes
|
||||
timeCreated: 1461077683
|
||||
licenseType: Free
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
214
Unity network UDP/Assets/scripts/network/common/DataBuffer.cs
Normal file
214
Unity network UDP/Assets/scripts/network/common/DataBuffer.cs
Normal file
|
@ -0,0 +1,214 @@
|
|||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using System;
|
||||
|
||||
public class DataBuffer{
|
||||
|
||||
//readonly constant static -> const non static
|
||||
public static readonly int SIZE = 1024;
|
||||
|
||||
private byte[] data;
|
||||
private int pointer;
|
||||
|
||||
public DataBuffer()
|
||||
{
|
||||
data = new byte[SIZE];
|
||||
pointer = 0;
|
||||
}
|
||||
|
||||
public DataBuffer(byte[] data)
|
||||
{
|
||||
this.data = data;
|
||||
pointer = 0;
|
||||
}
|
||||
|
||||
public void put(byte a)
|
||||
{
|
||||
if (pointer >= SIZE)
|
||||
{
|
||||
Debug.Log( "Databuffer write overflow");
|
||||
return;
|
||||
}
|
||||
data[pointer++] = a;
|
||||
}
|
||||
|
||||
public void put(sbyte a)
|
||||
{
|
||||
if (pointer >= SIZE)
|
||||
{
|
||||
Debug.Log("Databuffer write overflow");
|
||||
return;
|
||||
}
|
||||
data[pointer++] = (byte)a;
|
||||
}
|
||||
|
||||
public void put(short a)
|
||||
{
|
||||
put((byte)((a >> 8) & 0xff));
|
||||
put((byte)((a >> 0) & 0xff));
|
||||
}
|
||||
|
||||
public void put(int a)
|
||||
{
|
||||
put((byte)((a >> 24) & 0xff));
|
||||
put((byte)((a >> 16) & 0xff));
|
||||
put((byte)((a >> 8) & 0xff));
|
||||
put((byte)((a >> 0) & 0xff));
|
||||
}
|
||||
|
||||
public void put(long a)
|
||||
{
|
||||
put((byte)((a >> 56) & 0xff));
|
||||
put((byte)((a >> 48) & 0xff));
|
||||
put((byte)((a >> 40) & 0xff));
|
||||
put((byte)((a >> 32) & 0xff));
|
||||
put((byte)((a >> 24) & 0xff));
|
||||
put((byte)((a >> 16) & 0xff));
|
||||
put((byte)((a >> 8) & 0xff));
|
||||
put((byte)((a >> 0) & 0xff));
|
||||
}
|
||||
|
||||
public void put(float a)
|
||||
{
|
||||
byte[] array = BitConverter.GetBytes(a);
|
||||
foreach(byte b in array)
|
||||
{
|
||||
put(b);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void put(double a)
|
||||
{
|
||||
byte[] array = BitConverter.GetBytes(a);
|
||||
foreach (byte b in array)
|
||||
{
|
||||
put(b);
|
||||
}
|
||||
}
|
||||
|
||||
public void put(char a)
|
||||
{
|
||||
put((byte)a);
|
||||
}
|
||||
|
||||
public void put(String a)
|
||||
{
|
||||
char[] b = a.ToCharArray();
|
||||
put(a.Length);
|
||||
for (int i = 0; i < a.Length; i++)
|
||||
{
|
||||
put(b[i]);
|
||||
}
|
||||
}
|
||||
|
||||
public byte getByte()
|
||||
{
|
||||
if (pointer >= SIZE)
|
||||
{
|
||||
Debug.Log("Databuffer write overflow");
|
||||
return 0;
|
||||
}
|
||||
return data[pointer++];
|
||||
}
|
||||
|
||||
public sbyte getSByte()
|
||||
{
|
||||
if (pointer >= SIZE)
|
||||
{
|
||||
Debug.Log("Databuffer write overflow");
|
||||
return 0;
|
||||
}
|
||||
return (sbyte)data[pointer++];
|
||||
}
|
||||
|
||||
public short getShort()
|
||||
{
|
||||
return (short)((
|
||||
(getByte() << 8) & 0xff00) |
|
||||
(getByte() & 0xff));
|
||||
}
|
||||
|
||||
public int getInt()
|
||||
{
|
||||
return (int)((
|
||||
(getByte() << 24) & 0xff000000) |
|
||||
(getByte() << 16 & 0xff0000) |
|
||||
(getByte() << 8 & 0xff00) |
|
||||
(getByte() & 0xff));
|
||||
}
|
||||
|
||||
public long getLong()
|
||||
{
|
||||
return (long)(
|
||||
((ulong)((long)getByte() << 56) & 0xff00000000000000L) |
|
||||
((ulong)((long)getByte() << 48) & 0xff000000000000L) |
|
||||
((ulong)((long)getByte() << 40) & 0xff0000000000L) |
|
||||
(ulong)(((long)getByte() << 32) & 0xff00000000L) |
|
||||
((ulong)((long)getByte() << 24) & 0xff000000L) |
|
||||
((ulong)((long)getByte() << 16) & 0xff0000L) |
|
||||
((ulong)((long)getByte() << 8) & 0xff00L) |
|
||||
((ulong)(long)getByte() & 0xffL));
|
||||
}
|
||||
|
||||
public float getFloat()
|
||||
{
|
||||
byte[] array = new byte[4] { getByte(), getByte(), getByte(), getByte() };
|
||||
return BitConverter.ToSingle(array, 0);
|
||||
}
|
||||
|
||||
public double getDouble()
|
||||
{
|
||||
byte[] array = new byte[8] { getByte(), getByte(), getByte(), getByte(), getByte(), getByte(), getByte(), getByte() };
|
||||
return BitConverter.ToDouble(array, 0);
|
||||
}
|
||||
|
||||
public char getChar()
|
||||
{
|
||||
return (char)getByte();
|
||||
}
|
||||
|
||||
public String getString()
|
||||
{
|
||||
int size = getInt();
|
||||
char[] st = new char[size];
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
st[i] = getChar();
|
||||
}
|
||||
return new String(st);
|
||||
}
|
||||
|
||||
public byte[] getData()
|
||||
{
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(byte[] data)
|
||||
{
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public int getPointer()
|
||||
{
|
||||
return pointer;
|
||||
}
|
||||
|
||||
public void setPointer(int pointer)
|
||||
{
|
||||
this.pointer = pointer;
|
||||
}
|
||||
|
||||
public static int getSize()
|
||||
{
|
||||
return SIZE;
|
||||
}
|
||||
|
||||
public void clear()
|
||||
{
|
||||
pointer = 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 078e293494940144a9d5d954dc84daed
|
||||
timeCreated: 1461077692
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
11
Unity network UDP/Assets/scripts/network/common/IPacket.cs
Normal file
11
Unity network UDP/Assets/scripts/network/common/IPacket.cs
Normal file
|
@ -0,0 +1,11 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
interface IPacket
|
||||
{
|
||||
void read(DataBuffer data);
|
||||
void write(DataBuffer data);
|
||||
void manage(byte[] data, Client client);
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: c5c6e5983b09e5b45a1c0ff1cb10f177
|
||||
timeCreated: 1461092836
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
36
Unity network UDP/Assets/scripts/network/common/Register.cs
Normal file
36
Unity network UDP/Assets/scripts/network/common/Register.cs
Normal file
|
@ -0,0 +1,36 @@
|
|||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using System;
|
||||
|
||||
public class Register {
|
||||
|
||||
public static Type[] registeredClass;
|
||||
|
||||
public static void registerClass()
|
||||
{
|
||||
registeredClass = new Type[] {
|
||||
typeof(MessagePacket),
|
||||
typeof(MainState_Connection_Request_Packet),
|
||||
typeof(Disconnect_Client_Packet)
|
||||
};
|
||||
}
|
||||
|
||||
public static Type getClass(int id)
|
||||
{
|
||||
return registeredClass[id];
|
||||
}
|
||||
|
||||
public static int getId(Type cl)
|
||||
{
|
||||
for (int i = 0; i < registeredClass.Length; i++)
|
||||
{
|
||||
if (cl == registeredClass[i]) return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static object instantiate(int id)
|
||||
{
|
||||
return getClass(id).GetConstructor(Type.EmptyTypes).Invoke(Type.EmptyTypes);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 7b248295a2003cb4f8f2c02963c4dc69
|
||||
timeCreated: 1461091725
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
9
Unity network UDP/Assets/scripts/network/packet.meta
Normal file
9
Unity network UDP/Assets/scripts/network/packet.meta
Normal file
|
@ -0,0 +1,9 @@
|
|||
fileFormatVersion: 2
|
||||
guid: eafcd88293cff404b855b1726af268b7
|
||||
folderAsset: yes
|
||||
timeCreated: 1461092835
|
||||
licenseType: Free
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,39 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
class Disconnect_Client_Packet : IPacket
|
||||
{
|
||||
public string pseudo;
|
||||
|
||||
public Disconnect_Client_Packet() { }
|
||||
|
||||
public Disconnect_Client_Packet(string pseudo) { this.pseudo = pseudo; }
|
||||
|
||||
void IPacket.read(DataBuffer data)
|
||||
{
|
||||
this.pseudo = data.getString();
|
||||
}
|
||||
|
||||
void IPacket.write(DataBuffer data)
|
||||
{
|
||||
data.put(Register.getId(typeof(Disconnect_Client_Packet)));
|
||||
data.put(this.pseudo);
|
||||
}
|
||||
|
||||
void IPacket.manage(byte[] data, Client client)
|
||||
{
|
||||
if (pseudo.Equals(MainClient.pseudo))
|
||||
{
|
||||
MainClient.connected = MainState_Connection_Request_Packet.DISCONNECTED;
|
||||
MainClient.client.disconnect();
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 09c2a6099528c8445b15ffe7adbde542
|
||||
timeCreated: 1461172729
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,57 @@
|
|||
using System;
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
class MainState_Connection_Request_Packet : IPacket
|
||||
{
|
||||
|
||||
public static readonly int REQUEST = 0, ACCEPTED = 1, REFUSED = 2, TIME_OUT = 3, ALREADY_CONNECTED = 4, PSEUDO_INCORRECT = 5,DISCONNECTED = 6;
|
||||
|
||||
public String pseudo;
|
||||
public int id;
|
||||
|
||||
public MainState_Connection_Request_Packet() { }
|
||||
|
||||
public MainState_Connection_Request_Packet(String pseudo,int id)
|
||||
{
|
||||
this.pseudo = pseudo;
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
void IPacket.write(DataBuffer data)
|
||||
{
|
||||
data.put(Register.getId(typeof(MainState_Connection_Request_Packet)));
|
||||
data.put(pseudo);
|
||||
data.put(id);
|
||||
}
|
||||
|
||||
void IPacket.read(DataBuffer data)
|
||||
{
|
||||
this.pseudo = data.getString();
|
||||
this.id = data.getInt();
|
||||
}
|
||||
|
||||
void IPacket.manage(byte[] data, Client client)
|
||||
{
|
||||
if(id == ACCEPTED)
|
||||
{
|
||||
MainClient.connected = ACCEPTED;
|
||||
MainClient.pseudo = this.pseudo;
|
||||
}
|
||||
else if(id == REFUSED)
|
||||
{
|
||||
MainClient.connected = REFUSED;
|
||||
}
|
||||
else if (id == ALREADY_CONNECTED)
|
||||
{
|
||||
MainClient.connected = ALREADY_CONNECTED;
|
||||
}
|
||||
else if (id == PSEUDO_INCORRECT)
|
||||
{
|
||||
MainClient.connected = PSEUDO_INCORRECT;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: e632acbe3e907fb42992ec51c2549272
|
||||
timeCreated: 1461155199
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,38 @@
|
|||
using System;
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
class MessagePacket : IPacket
|
||||
{
|
||||
|
||||
private String pseudo, message;
|
||||
|
||||
public MessagePacket() { }
|
||||
|
||||
public MessagePacket(String pseudo, String message)
|
||||
{
|
||||
this.message = message;
|
||||
this.pseudo = pseudo;
|
||||
}
|
||||
|
||||
void IPacket.read(DataBuffer data)
|
||||
{
|
||||
this.pseudo = data.getString();
|
||||
this.message = data.getString();
|
||||
}
|
||||
|
||||
void IPacket.write(DataBuffer data)
|
||||
{
|
||||
data.put(Register.getId(typeof(MessagePacket)));
|
||||
data.put(this.pseudo);
|
||||
data.put(this.message);
|
||||
}
|
||||
|
||||
void IPacket.manage(byte[] data,Client client)
|
||||
{
|
||||
Debug.Log(this.pseudo + " : " + this.message);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 6df0f62eafd1c224ea52a0721bc3e4b1
|
||||
timeCreated: 1461092835
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,27 @@
|
|||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class PingPacket : IPacket {
|
||||
|
||||
public long current;
|
||||
|
||||
public PingPacket()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void IPacket.read(DataBuffer data)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void IPacket.write(DataBuffer data)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void IPacket.manage(byte[] data, Client client)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 6c3875a60f90ff146950d2ad190f0e09
|
||||
timeCreated: 1462518396
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in a new issue