資源簡(jiǎn)介
這是一個(gè)可以發(fā)布到HoloLens的socket通信示例,基于Unity5.3.4f1編寫的,發(fā)布到HoloLens絕對(duì)沒問(wèn)題的,你可以根據(jù)自己需求自行修改。

代碼片段和文件信息
/**
?*?版本:0.01
?*?作者:吳雁濤
?*?日期:2016年4月29日
?*?內(nèi)容:服務(wù)器端客戶端通信學(xué)習(xí)
?*?
?**/
using?UnityEngine;
using?UnityEngine.Networking;
using?UnityEngine.UI;
using?System;
public?class?MyNetworkManager?:?MonoBehaviour
{
///?
///?服務(wù)器ip地址
///?
public?InputField?ip;
///?
///?服務(wù)器端口
///?
public?InputField?port;
///?
///?要發(fā)送的信息
///?
public?InputField?send;
///?
///?顯示信息
///?
public?Text?info;
///?
///?網(wǎng)絡(luò)客戶端
///?
private?NetworkClient?myClient;
///?
///?用戶信息分類
///?
private?const?short?userMsg?=?64;
void?Start?()
{
info.text?=?“Start...“;
myClient?=?new?NetworkClient?();
}
??
///?
///?建立服務(wù)器
///?
public?void?SetupServer?()
{
if?(!NetworkServer.active)?{
ShowMsg?(“setup?server“);
ServerRegisterHandler?();
NetworkServer.Listen?(int.Parse?(port.text));
if?(NetworkServer.active)?{
ShowMsg?(“Server?setup?ok.“);
}
}
}
///?
///?建立客戶端
///?
public?void?SetupClient?()
{
if?(!myClient.isConnected)?{
ShowMsg?(“setup?client“);
ClientRegisterHandler?();
myClient.Connect?(ip.text?int.Parse?(port.text));
}
}
///?
///?停止客戶端
///?
public?void?ShutdownClient?()
{
if?(myClient.isConnected)?{
ClientUnregisterHandler?();
myClient.Disconnect?();
//NetworkClient.Shutdown()使用后,無(wú)法再次連接。
//This?should?be?done?when?a?client?is?no?longer?going?to?be?used.
//myClient.Shutdown?();
}
}
///?
///?停止服務(wù)器端
///?
public?void?ShutdownServer?()
{
if?(NetworkServer.active)?{
ServerUnregisterHandler?();
NetworkServer.DisconnectAll?();
NetworkServer.Shutdown?();
if?(!NetworkServer.active)?{
ShowMsg?(“shut?down?server“);
}
}
}
///?
///?客戶端連接到服務(wù)器事件
///?
///?Net?message.
private?void?OnClientConnected?(NetworkMessage?netMsg)
{
ShowMsg?(“Client?connected?to?server“);
}
///?
///客戶端從服務(wù)器斷開事件
///?
///?Net?message.
private?void?OnClientDisconnected?(NetworkMessage?netMsg)
{
ShowMsg?(“Client?disconnected?from?server“);
}
///?
///?客戶端錯(cuò)誤事件
///?
///?Net?message.
private?void?OnClientError?(NetworkMessage?netMsg)
{
ClientUnregisterHandler?();
ShowMsg?(“Client?error“);
}
///?
///?服務(wù)器端有客戶端連入事件
///?
///?Net?message.
private?void?OnServerConnected?(NetworkMessage?netMsg)
{
ShowMsg?(“One?client?connected?to?server“);
}
///?
///?服務(wù)器端有客戶端斷開事件
///?
///?Net?message.
private?void?OnServerDisconnected?(NetworkMessage?netMsg)
{
ShowMsg?(“One?client?connected?from?server“);
}
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件??????14435??2016-04-27?10:25??HololensSocketDemo\Assembly-CSharp-Editor.csproj
?????文件???????4304??2016-04-28?09:22??HololensSocketDemo\Assembly-CSharp-firstpass.csproj
?????文件???????4188??2016-04-29?14:11??HololensSocketDemo\Assembly-CSharp.csproj
?????文件???????6246??2016-04-29?15:05??HololensSocketDemo\Assets\MyNetworkManager.cs
????..A..H.???????263??2016-04-28?17:51??HololensSocketDemo\Assets\MyNetworkManager.cs.me
?????文件????????721??2016-04-28?17:56??HololensSocketDemo\Assets\NWLearn.cs
????..A..H.???????263??2016-04-28?17:40??HololensSocketDemo\Assets\NWLearn.cs.me
????I.A....??????4908??2016-04-28?17:39??HololensSocketDemo\Assets\play.prefab
????..A..H.???????180??2016-04-28?17:39??HololensSocketDemo\Assets\play.prefab.me
????I.A....?????38872??2016-04-28?17:57??HololensSocketDemo\Assets\server.unity
????..A..H.???????175??2016-04-28?17:38??HololensSocketDemo\Assets\server.unity.me
????I.A....??????4120??2016-04-19?15:10??HololensSocketDemo\Library\AnnotationManager
????I.A....???1546436??2016-04-29?15:05??HololensSocketDemo\Library\assetDataba
????I.A....????????17??2016-04-29?14:11??HololensSocketDemo\Library\AssetImportState
????I.A....??????4756??2016-04-29?15:05??HololensSocketDemo\Library\AssetServerCacheV3
????I.A....?????14336??2016-04-20?10:02??HololensSocketDemo\Library\AssetVersioning.db
????I.A....????267700??2016-04-25?10:54??HololensSocketDemo\Library\AtlasCache\11\111396342696616dd8dd8b995bde38c9
????I.A....??????8596??2016-04-25?10:54??HololensSocketDemo\Library\AtlasCache\58\58dc768eb225cb597fc603bc9cc9ceca
????I.A....??????5860??2016-04-25?10:54??HololensSocketDemo\Library\AtlasCache\79\79d17962e46a00b8a8886503a748da94
????I.A....??11195052??2016-04-25?10:54??HololensSocketDemo\Library\AtlasCache\a2\a2f82126fda7305f2a23618ab2749eba
????I.A....?????????0??2016-04-19?15:10??HololensSocketDemo\Library\BuildPla
????I.A....??????4128??2016-04-19?15:10??HololensSocketDemo\Library\BuildSettings.asset
????I.A....?????40260??2016-04-29?15:05??HololensSocketDemo\Library\CurrentLayout.dwlt
????I.A....??????4341??2016-04-29?14:11??HololensSocketDemo\Library\EditorUserBuildSettings.asset
????I.A....??????4148??2016-04-19?15:10??HololensSocketDemo\Library\EditorUserSettings.asset
????I.A....??????4464??2016-04-29?15:05??HololensSocketDemo\Library\expandedItems
????I.A....??????4544??2016-04-28?15:41??HololensSocketDemo\Library\InspectorExpandedItems.asset
?????文件?????????78??2016-04-29?15:05??HololensSocketDemo\Library\LastSceneManagerSetup.txt
?????文件?????????65??2016-04-19?15:10??HololensSocketDemo\Library\LibraryFormatVersion.txt
????I.A....??????4285??2016-04-19?15:10??HololensSocketDemo\Library\me
............此處省略2125個(gè)文件信息
評(píng)論
共有 條評(píng)論