视频地址: https://www.bilibili.com/video/BV1hJ411U7Mb/
1.1 四个最新的Microsoft.MixedReality.Toolkit.Unity.2.3.0安装包
链接:https://pan.baidu.com/s/1XuqS_kTKK1lFz8uX1cwqqw
提取码:nzi2
1.2 将Unity打包Visual Studio部署HoloLens找不到WindowMobile SDK的解决方案
具体报错信息“WindowsMobile version 10.0.xxx.0”
解决方法:
把下载的WindowsMobile SDK从下载的目录
[Windows Kit Root Dir]\10\Extension SDKs\WindowsMobile
拷贝到
C:\Program Files (x86)\Windows Kits\10\Extension SDKs\
问题深入描述链接:
地址:https://www.cnblogs.com/fws94/p/12720857.html
链接:http://www.manew.com/thread-107013-1-1.html
2.1 我所使用的工具
1.unity2019.2.7f2 (官网下载)
2.Microsoft.MixedReality.Toolkit.Unity.2.3.0安装包 (参见1.1中链接) 还有开发hololens的环境,真机,Visual Studio 2019等不在赘述.
3 SocketTool4
链接:https://pan.baidu.com/s/1VOaM9PU-HFEYp3OELOm3nQ
提取码:glj6
2.2 代码开发
导入 Microsoft.MixedReality.Toolkit.Unity.2.3.0安装包。新建文件夹 Test,内建文件夹Scripts和Scenes来存放脚本和场景,脚本内IP端口根据自己情况调整
在camera下面建个空物体gameobject,添加TextMesh组件,和三个脚本
按照图上设置好,调整相机能看到textmesh内容即可.
1 UDPCommunication
using UnityEngine;
using System;
using System.IO;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using UnityEngine.Events;
#if !UNITY_EDITOR
using Windows.Networking.Sockets;
using Windows.Networking.Connectivity;
using Windows.Networking;
#endif
[System.Serializable]
public class UDPMessageEvent : UnityEvent<string, string, byte[]>
{
}
public class UDPCommunication : Singleton<UDPCommunication>
{
[Tooltip("port to listen for incoming data")]//端口监听输入数据
public string internalPort = "12345";
[Tooltip("IP-Address for sending")] //ip地址发送
public string externalIP = "192.168.1.110";
[Tooltip("Port for sending")] //端口发送
public string externalPort = "12346";
[Tooltip("Send a message at Startup")] //在启动时发送消息
public bool sendPingAtStart = true;
[Tooltip("Conten of Ping")]
public string PingMessage = "hello";
[Tooltip("Function to invoke at incoming packet")]//函数对传入包进行调用
public UDPMessageEvent udpEvent = null;
private readonly Queue<Action> ExecuteOnMainThread = new Queue<Action>();
#if !UNITY_EDITOR
//we've got a message (data[]) from (host) in case of not assigned an event
void UDPMessageReceived(string host, string port, byte[] data)
{
Debug.Log("GOT MESSAGE FROM: " + host + " on port " + port + " " + data.Length.ToString() + " bytes ");
}
//Send an UDP-Packet
public async void SendUDPMessage(string HostIP, string HostPort, byte[] data)
{
await _SendUDPMessage(HostIP, HostPort, data);
}
DatagramSocket socket;
async void Start()
{
if (udpEvent == null)
{
udpEvent = new UDPMessageEvent();
udpEvent.AddListener(UDPMessageReceived);
}
Debug.Log("Waiting for a connection...");
socket = new DatagramSocket();
socket.MessageReceived += Socket_MessageReceived;
HostName IP = null;
try
{
var icp = NetworkInformation.GetInternetConnectionProfile();
IP = Windows.Networking.Connectivity.NetworkInformation.GetHostNames()
.SingleOrDefault(
hn =>
hn.IPInformation?.NetworkAdapter != null && hn.IPInformation.NetworkAdapter.NetworkAdapterId
== icp.NetworkAdapter.NetworkAdapterId);
await socket.BindEndpointAsync(IP, internalPort);
}
catch (Exception e)
{
Debug.Log(e.ToString());
Debug.Log(SocketError.GetStatus(e.HResult).ToString());
return;
}
if(sendPingAtStart)
SendUDPMessage(externalIP, externalPort, Encoding.UTF8.GetBytes(PingMessage));
}
private async System.Threading.Tasks.Task _SendUDPMessage(string externalIP, string externalPort, byte[] data)
{
using (var stream = await socket.GetOutputStreamAsync(new Windows.Networking.HostName(externalIP), externalPort))
{
using (var writer = new Windows.Storage.Streams.DataWriter(stream))
{
writer.WriteBytes(data);
await writer.StoreAsync();
}
}
}
#else
// to make Unity-Editor happy :-)
void Start()
{
}
public void SendUDPMessage(string HostIP, string HostPort, byte[] data)
{
}
#endif
static MemoryStream ToMemoryStream(Stream input)
{
try
{ // Read and write in
byte[] block = new byte[0x1000]; // blocks of 4K.
MemoryStream ms = new MemoryStream();
while (true)
{
int bytesRead = input.Read(block, 0, block.Length);
if (bytesRead == 0) return ms;
ms.Write(block, 0, bytesRead);
}
}
finally { }
}
// Update is called once per frame
void Update()
{
while (ExecuteOnMainThread.Count > 0)
{
ExecuteOnMainThread.Dequeue().Invoke();
}
}
#if !UNITY_EDITOR
private void Socket_MessageReceived(Windows.Networking.Sockets.DatagramSocket sender,
Windows.Networking.Sockets.DatagramSocketMessageReceivedEventArgs args)
{
Debug.Log("GOT MESSAGE FROM: " + args.RemoteAddress.DisplayName);
//Read the message that was received from the UDP client.
Stream streamIn = args.GetDataStream().AsStreamForRead();
MemoryStream ms = ToMemoryStream(streamIn);
byte[] msgData = ms.ToArray();
if (ExecuteOnMainThread.Count == 0)
{
ExecuteOnMainThread.Enqueue(() =>
{
Debug.Log("ENQEUED ");
if (udpEvent != null)
udpEvent.Invoke(args.RemoteAddress.DisplayName, internalPort, msgData);
});
}
}
#endif
}
2 UDPResponse
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UDPResponse : MonoBehaviour
{
public TextMesh tm = null;
public void ResponseToUDPPacket(string incomingIP, string incomingPort, byte[] data)
{
if (tm != null)
tm.text = System.Text.Encoding.UTF8.GetString(data);
#if !UNITY_EDITOR
//ECHO
UDPCommunication comm = UDPCommunication.Instance;
comm.SendUDPMessage(incomingIP, comm.externalPort, data);
#endif
}
}
3 Singleton
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Singleton<T> : MonoBehaviour where T : Singleton<T>
{
private static T _instance;
public static T Instance
{
get
{
if (_instance == null)
{
_instance = new GameObject(typeof(T).Name).AddComponent<T>();
}
return _instance;
}
}
}
2.3 发布部署
然后保存场景为UWP_UDP,添加发布UWP程序.通过VS部署到hololens里面,
然后运行程序。在运行的过程中,通过电脑TCP&UDP工具测试。
1 HoloLens 官方教程
链接:https://docs.microsoft.com/zh-cn/windows/mixed-reality/tutorials
2 快速入门:将Unity示例部署到HoloLens中
链接:https://docs.microsoft.com/zh-cn/azure/remote-rendering/quickstarts/deploy-to-hololens
3 VS2019下载
链接:https://developer.microsoft.com/zh-cn/
4 Getting started with MRTK
链接:
https://microsoft.github.io/MixedRealityToolkit-Unity/Documentation/GettingStartedWithTheMRTK.html
温馨注释: