这是一个可以发布到HoloLens的socket通信示例,基于Unity5.3.4f1编写的,发布到HoloLens绝对没问题的,你可以根据自己需求自行修改。
代码片段和文件信息
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 14435 2016-04-27 10:25 HololensSocketDemoAssembly-CSharp-Editor.csproj
文件 4304 2016-04-28 09:22 HololensSocketDemoAssembly-CSharp-firstpass.csproj
文件 4188 2016-04-29 14:11 HololensSocketDemoAssembly-CSharp.csproj
文件 6246 2016-04-29 15:05 HololensSocketDemoAssetsMyNetworkManager.cs
..A..H. 263 2016-04-28 17:51 HololensSocketDemoAssetsMyNetworkManager.cs.meta
文件 721 2016-04-28 17:56 HololensSocketDemoAssetsNWLearn.cs
..A..H. 263 2016-04-28 17:40 HololensSocketDemoAssetsNWLearn.cs.meta
I.A.... 4908 2016-04-28 17:39 HololensSocketDemoAssetsplay.prefab
..A..H. 180 2016-04-28 17:39 HololensSocketDemoAssetsplay.prefab.meta
I.A.... 38872 2016-04-28 17:57 HololensSocketDemoAssetsserver.unity
..A..H. 175 2016-04-28 17:38 HololensSocketDemoAssetsserver.unity.meta
I.A.... 4120 2016-04-19 15:10 HololensSocketDemoLibraryAnnotationManager
I.A.... 1546436 2016-04-29 15:05 HololensSocketDemoLibraryassetDatabase3
I.A.... 17 2016-04-29 14:11 HololensSocketDemoLibraryAssetImportState
I.A.... 4756 2016-04-29 15:05 HololensSocketDemoLibraryAssetServerCacheV3
I.A.... 14336 2016-04-20 10:02 HololensSocketDemoLibraryAssetVersioning.db
I.A.... 267700 2016-04-25 10:54 HololensSocketDemoLibraryAtlasCache11111396342696616dd8dd8b995bde38c9
I.A.... 8596 2016-04-25 10:54 HololensSocketDemoLibraryAtlasCache5858dc768eb225cb597fc603bc9cc9ceca
I.A.... 5860 2016-04-25 10:54 HololensSocketDemoLibraryAtlasCache7979d17962e46a00b8a8886503a748da94
I.A.... 11195052 2016-04-25 10:54 HololensSocketDemoLibraryAtlasCachea2a2f82126fda7305f2a23618ab2749eba
I.A.... 0 2016-04-19 15:10 HololensSocketDemoLibraryBuildPlayer.prefs
I.A.... 4128 2016-04-19 15:10 HololensSocketDemoLibraryBuildSettings.asset
I.A.... 40260 2016-04-29 15:05 HololensSocketDemoLibraryCurrentLayout.dwlt
I.A.... 4341 2016-04-29 14:11 HololensSocketDemoLibraryEditorUserBuildSettings.asset
I.A.... 4148 2016-04-19 15:10 HololensSocketDemoLibraryEditorUserSettings.asset
I.A.... 4464 2016-04-29 15:05 HololensSocketDemoLibraryexpandedItems
I.A.... 4544 2016-04-28 15:41 HololensSocketDemoLibraryInspectorExpandedItems.asset
文件 78 2016-04-29 15:05 HololensSocketDemoLibraryLastSceneManagerSetup.txt
文件 65 2016-04-19 15:10 HololensSocketDemoLibraryLibraryFormatVersion.txt
I.A.... 4285 2016-04-19 15:10 HololensSocketDemoLibrarymetadata 0 0000000000000001000000000000000
............此处省略2125个文件信息
/**
* 版本:0.01
* 作者:吴雁涛
* 日期:2016年4月29日
* 内容:服务器端客户端通信学习
*
**/
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using System;
public class MyNetworkManager : MonoBehaviour
{
///
/// 服务器ip地址
///
public InputField ip;
///
/// 服务器端口
///
public InputField port;
///
/// 要发送的信息
///
public InputField send;
///
/// 显示信息
///
public Text info;
///
/// 网络客户端
///
private NetworkClient myClient;
///
/// 用户信息分类
///
private const short userMsg = 64;
void Start ()
{
info.text = “Start...“;
myClient = new NetworkClient ();
}
///
/// 建立服务器
///
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()使用后,无法再次连接。
//This should be done when a client is no longer going to be used.
//myClient.Shutdown ();
}
}
///
/// 停止服务器端
///
public void ShutdownServer ()
{
if (NetworkServer.active) {
ServerUnregisterHandler ();
NetworkServer.DisconnectAll ();
NetworkServer.Shutdown ();
if (!NetworkServer.active) {
ShowMsg (“shut down server“);
}
}
}
///
/// 客户端连接到服务器事件
///
/// Net message.
private void OnClientConnected (NetworkMessage netMsg)
{
ShowMsg (“Client connected to server“);
}
///
///客户端从服务器断开事件
///
/// Net message.
private void OnClientDisconnected (NetworkMessage netMsg)
{
ShowMsg (“Client disconnected from server“);
}
///
/// 客户端错误事件
///
/// Net message.
private void OnClientError (NetworkMessage netMsg)
{
ClientUnregisterHandler ();
ShowMsg (“Client error“);
}
///
/// 服务器端有客户端连入事件
///
/// Net message.
private void OnServerConnected (NetworkMessage netMsg)
{
ShowMsg (“One client connected to server“);
}
///
/// 服务器端有客户端断开事件
///
/// Net message.
private void OnServerDisconnected (NetworkMessage netMsg)
{
ShowMsg (“One client connected from server“);
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 14435 2016-04-27 10:25 HololensSocketDemoAssembly-CSharp-Editor.csproj
文件 4304 2016-04-28 09:22 HololensSocketDemoAssembly-CSharp-firstpass.csproj
文件 4188 2016-04-29 14:11 HololensSocketDemoAssembly-CSharp.csproj
文件 6246 2016-04-29 15:05 HololensSocketDemoAssetsMyNetworkManager.cs
..A..H. 263 2016-04-28 17:51 HololensSocketDemoAssetsMyNetworkManager.cs.me
文件 721 2016-04-28 17:56 HololensSocketDemoAssetsNWLearn.cs
..A..H. 263 2016-04-28 17:40 HololensSocketDemoAssetsNWLearn.cs.me
I.A.... 4908 2016-04-28 17:39 HololensSocketDemoAssetsplay.prefab
..A..H. 180 2016-04-28 17:39 HololensSocketDemoAssetsplay.prefab.me
I.A.... 38872 2016-04-28 17:57 HololensSocketDemoAssetsserver.unity
..A..H. 175 2016-04-28 17:38 HololensSocketDemoAssetsserver.unity.me
I.A.... 4120 2016-04-19 15:10 HololensSocketDemoLibraryAnnotationManager
I.A.... 1546436 2016-04-29 15:05 HololensSocketDemoLibraryassetDataba
I.A.... 17 2016-04-29 14:11 HololensSocketDemoLibraryAssetImportState
I.A.... 4756 2016-04-29 15:05 HololensSocketDemoLibraryAssetServerCacheV3
I.A.... 14336 2016-04-20 10:02 HololensSocketDemoLibraryAssetVersioning.db
I.A.... 267700 2016-04-25 10:54 HololensSocketDemoLibraryAtlasCache11111396342696616dd8dd8b995bde38c9
I.A.... 8596 2016-04-25 10:54 HololensSocketDemoLibraryAtlasCache5858dc768eb225cb597fc603bc9cc9ceca
I.A.... 5860 2016-04-25 10:54 HololensSocketDemoLibraryAtlasCache7979d17962e46a00b8a8886503a748da94
I.A.... 11195052 2016-04-25 10:54 HololensSocketDemoLibraryAtlasCachea2a2f82126fda7305f2a23618ab2749eba
I.A.... 0 2016-04-19 15:10 HololensSocketDemoLibraryBuildPla
I.A.... 4128 2016-04-19 15:10 HololensSocketDemoLibraryBuildSettings.asset
I.A.... 40260 2016-04-29 15:05 HololensSocketDemoLibraryCurrentLayout.dwlt
I.A.... 4341 2016-04-29 14:11 HololensSocketDemoLibraryEditorUserBuildSettings.asset
I.A.... 4148 2016-04-19 15:10 HololensSocketDemoLibraryEditorUserSettings.asset
I.A.... 4464 2016-04-29 15:05 HololensSocketDemoLibraryexpandedItems
I.A.... 4544 2016-04-28 15:41 HololensSocketDemoLibraryInspectorExpandedItems.asset
文件 78 2016-04-29 15:05 HololensSocketDemoLibraryLastSceneManagerSetup.txt
文件 65 2016-04-19 15:10 HololensSocketDemoLibraryLibraryFormatVersion.txt
I.A.... 4285 2016-04-19 15:10 HololensSocketDemoLibraryme
............此处省略2125个文件信息
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。
评论列表(条)