Hololens 开发环境的配置
在此之前,需要到Bios 或者 uefi中开始vt 以及 虚拟化支持
开启Hyper-V
打开 控制面板 -> 程序 -> 程序和功能 -> 打开和关闭系统功能 ,勾选 Hyper-V
Hololens模拟器
https://docs.microsoft.com/zh-cn/windows/mixed-reality/hololens-emulator-archive
U3d 下载 windowsMR 插件
在USB或者WIFI链接Hololens的情况下,在浏览器网址栏输入http://127.0.0.1:10080访问设备控制台,输入账号密码登陆之后点击左边栏的Apps按钮进入App界面,选择下方的Install app-App package下的Choose File按钮,选择你打包出来的Appx程序
-——————–
版权声明:本文为CSDN博主「梧桐Mormon」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_39308897/article/details/78332027
这边更新版本之后好像不用依赖文件,直接安装misx就好了
win10 里使用 socket需要用 Windows.Networking下的类库实现,我们如果直接在Unity C# 脚本里 using Windows.Networking是不可以的,Unity编辑器在编译的时候是不能通过的。Windows.Networking 这是属于 WinRT API,只有在VS里才能被编译。
所以,如果要在Unity开发HoloLens程序使用 WinRT API 需要用 #if WINDOWS_UWP …… #endif 对相应的代码进行包裹,这样在Unity里就不会报错,build后再VS里再编译打包
尝试
原来选的是.net 来进行backend,然后socket库没办法编译成功,api无法编译到UWP平台
换成IL2CPP后就可以了。
一开始没有勾选这些库,导致无法局域网通信成功
这样好像可以使用unity自带的socket 库
IL2CPP 变成CPP文件编译
可以尝试类似如下代码实现socket
来自 http://mshololens.cn/d/124/2
using UnityEngine;
using System.Collections.Generic;
using System.Collections;
using System;
\#if WINDOWS_UWP
using System.Net;
using System.Net.Sockets;
\#endif
using System.Text;
using System.Threading;
using System.IO;
using System.Runtime.InteropServices;
using UnityEngine.UI;
public class UDP_1: MonoBehaviour
{
\#if WINDOWS_UWP
public string ipAdress = "127.0.0.1";
public int port = 6000;
public string serveripAdress = "192.168.1.57";
public int server_port = 6000;
IPEndPoint iep_Recieve;
EndPoint ep;
private byte[] data = new byte[1024];
private Socket clientSocket;
private Thread receiveT;
private bool IsThreading = false;
public static bool server_is_connected = false;
public static GameObject UDP_Data_Game_OBJ;
private string UDP_show_content = "did not connect";
// 协议内容
//const int float_num = 5;
//const int DataSize = 4* float_num; //两个float 4*2
//[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
//public struct info_msg
//{
// [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
// public byte[] packages_head;
// [MarshalAs(UnmanagedType.ByValArray, SizeConst = DataSize)]
// public float[] packages_data;
// [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
// public byte[] packages_end;
//}
//info_msg info_msg_tmp;
//public byte[] packages_head_check = { 0XFF, 0XFE };
//public byte[] packages_end_check = { 0XBE, 0XEF, 0X00 };
//[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
//public struct BallisticStruct
//{
// public int BallX; /* -50000.0-50000.0 0.1*/
// public int BallY; /* -50000.0-50000.0 0.1*/
// public int BallZ; /* -50000.0-50000.0 0.1*/
// public short BallVX; /* -400.0 - 400.0 0.1*/
// public short BallVY; /* -400.0 - 400.0 0.1*/
// public short BallVZ; /* -400.0 - 400.0 0.1*/
//}
//const int BallisticStructSize = (4 * 3 + 2 * 3) * 10;
//[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
//public struct Send23D
//{
// public short identifier; /* 0A14H */
// public short length; /* 50 */
// public int sendID; /* 0AH */
// public int readID; /* 14H */
// public uint sendTime; /* 0~4294967295 */
// public short TargetType; /* 0 战斗机 1 直升机2 无人机3 制导炸弹 */
// public short TargetAzimuth; /* -180.0 - 180.0 180/2^15 */
// public short TargetAltitude; /* -10.0 - 90.0 180/2^15 */
// public int TargetDistance; /* 0 - 50000.0 0.1*/
// public int TargetRealX; /* -50000.0-50000.0 0.1 */
// public int TargetRealY;
// public int TargetRealZ;
// public short CarVelocity; /* 0-120.0 0.1 */
// public short CarCw; /* 0-360 180/2^15 */
// public short CarPhi; /* -45 - 45 180/2^15*/
// public short CarTheta; /* -45 - 45 180/2^15*/
// public short JAzimuth; /* -180.0 - 180.0 180/2^15*/
// public short JAltitude; /* -10.0 - 90.0 180/2^15*/
// public short JAzimuthVelocity; /* -90.0 - 90.0 180/2^15*/
// public short JAltitudeVelocity; /* -90.0 - 90.0 180/2^15*/
// public short ShootStatus;
// [MarshalAs(UnmanagedType.ByValArray, SizeConst = BallisticStructSize)]
// public BallisticStruct[] ballisticstruct; /*保存10个弹丸的位置等信息*/
// //冗余20字节信息
// [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
// public byte[] RemainInfo;
//}
//Send23D BattleInfo_Recv;
//// 用于其他脚本之间共享数据
//public static Send23D BattleInfo_Share;
//// 用于其他脚本之间共享数据
//public static info_msg fire_msg;
//// Bytes 与 Struct 相互转换
//protected T Bytes2Struct<T>(byte[] bytes)
//{
// IntPtr arrPtr = Marshal.UnsafeAddrOfPinnedArrayElement(bytes, 0);
// return (T)Marshal.PtrToStructure(arrPtr, typeof(T));
//}
//protected byte[] Struct2Bytes<T>(T obj)
//{
// int size = Marshal.SizeOf(obj);
// byte[] bytes = new byte[size];
// IntPtr arrPtr = Marshal.UnsafeAddrOfPinnedArrayElement(bytes, 0);
// Marshal.StructureToPtr(obj, arrPtr, true);
// return bytes;
//}
//bool check_packages(info_msg msg)
//{
// if (BitConverter.ToString(packages_head_check) == BitConverter.ToString(msg.packages_head)
// && BitConverter.ToString(packages_end_check) == BitConverter.ToString(msg.packages_end))
// return true;
// else return false;
//}
\#endif
private void Awake()
{
//byte[] buffer = new byte[Marshal.SizeOf(new Send23D())];
//BattleInfo_Recv = Bytes2Struct<Send23D>(buffer);
//BattleInfo_Share = Bytes2Struct<Send23D>(buffer);
//info_msg_tmp = Bytes2Struct<info_msg>(buffer);
//fire_msg = Bytes2Struct<info_msg>(buffer);
}
void Start()
{
\#if WINDOWS_UWP
Init_Udp_Clint();
IsThreading = true;
\#endif
}
private void Update()
{
\#if WINDOWS_UWP
if (Input.GetKeyDown(KeyCode.L))
{
SendMes(" client send msg to server ");
}
Text text_content;
text_content = GameObject.Find("UDP_Data").GetComponent<Text>();
text_content.text = UDP_show_content;
\#endif
}
\#if WINDOWS_UWP
void Init_Udp_Clint()
{
try
{
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint local_Recieve = new IPEndPoint(IPAddress.Parse(ipAdress), port);
iep_Recieve = new IPEndPoint(IPAddress.Parse(serveripAdress), server_port);
ep = (EndPoint)iep_Recieve;
clientSocket.Bind(local_Recieve);
receiveT = new Thread(ReceiveMsg);
receiveT.Start();
Debug.Log("Success 初始化Udp接受线程 成功");
}
catch (System.Exception ex)
{
Debug.Log("ERROR 初始化Udp接受线程 失败 ");
Debug.Log(ex.Message);
}
}
private void ReceiveMsg()
{
while (true)
{
// 接收数据
int intReceiveLength = clientSocket.ReceiveFrom(data, ref ep);
//info_msg_tmp = Bytes2Struct<info_msg>(data);
//if (check_packages(info_msg_tmp))
//{
// server_is_connected = true;
// fire_msg = info_msg_tmp;
// //Debug.Log("package check successfully");
// //Debug.Log("package head: " + BitConverter.ToString(fire_msg.packages_head));
// //Debug.Log("package data: " + fire_msg.packages_data[0]);
// //Debug.Log("package data: " + fire_msg.packages_data[1]);
// //Debug.Log("packages end: " + BitConverter.ToString(fire_msg.packages_end));
//}
//BattleInfo_Recv = Bytes2Struct<Send23D>(data);
//if (BattleInfo_Recv.identifier == 0xa14)
//{
// // 如果检验成功,则将收到的信息发布到各个驱动模块
// BattleInfo_Share = BattleInfo_Recv;
// server_is_connected = true;
// //print("battle info get");
// //print("intReceiveLength:" + BattleInfo_Recv.length);
// // print("from: " + BattleInfo_Recv.sendID);
// // print("recv: " + BattleInfo_Recv.readID);
// //for (int i = 0; i < intReceiveLength; ++i)
// //{
// // print(i + " " + data[i]);
// //}
//}
//else
{
//for (int i = 0; i < intReceiveLength; ++i)
//{
// print(i + " " + data[i]);
//}
//Debug.Log("raw:" + BattleInfo_Recv.identifier);
string str = Encoding.UTF8.GetString(data, 0, data.Length);
Debug.Log("client received : " + str);
UDP_show_content = str;
}
}
}
void SendMes(string ms)
{
byte[] data = new byte[1024];
data = Encoding.UTF8.GetBytes(ms);
//IPEndPoint iep_Recieve = new IPEndPoint(IPAddress.Parse(serveripAdress), server_port);
//EndPoint temsendIP = (EndPoint)iep_Recieve;
clientSocket.SendTo(data, ep);
}
//void AllSendStruct(object obj)
//{
// //IPEndPoint iep_send = new IPEndPoint(IPAddress.Parse(serveripAdress), server_port);
// //EndPoint temsendIP = (EndPoint)iep_send;
// data = Struct2Bytes<object>(obj);
// clientSocket.SendTo(data, ep);
//}
void OnDestroy()
{
try
{
if (receiveT != null)
{
receiveT.Interrupt();
receiveT.Abort();
if (clientSocket != null)
{
//clientSocket.Shutdown(SocketShutdown.Both);
clientSocket.Close();//关闭连接
}
server_is_connected = false;
}
}
catch (Exception ex)
{
Debug.Log(ex.Message);
}
}
\#endif
}