疑问,有空可以测试一下。
这两个脚本都没有继承自monobehaviour是不是只需要写好后,不需要给物体上挂就可以运行发送数据了。
2,传数据和架构的建立。
a,导入holotoolkit后,界面自动会多一个 HoloToolkit按钮。
运行服务器时holotoolkit----> sharing service -----> launch
sharing service,这样服务器会运行起来。
b, 每个客户端的建立只需要拖预制体:HoloToolkit/Sharing/Perfabs/Sharing就好了。
需要修改IP地址,或写为localhost.
c,数据传输和1完全一样。修改这两个脚本。
d Loacl client, 这两个脚本都需要。
step1, RemoteHeadManager.cs
private void Update()
{
// Grab the current head transform and broadcast it to all the other users in the session
Transform headTransform = Camera.main.transform;
// Transform the head position and rotation from world space into local space
Vector3 headPosition = transform.InverseTransformPoint(headTransform.position);
Quaternion headRotation = Quaternion.Inverse(transform.rotation) * headTransform.rotation;
// CustomMessages.Instance.SendHeadTransform(headPosition, headRotation);
// CustomMessages.Instance.SendHeadTransform();
CustomMessages.Instance.Send_Messages();
}
step2, CustomMessages.cs
//发送数据的函数 public void Send_Messages() { // If we are connected to a session, broadcast our info if (serverConnection != null && serverConnection.IsConnected()) { // Create an outgoing network message to contain all the info we want to send NetworkOutMessage msg = CreateMessage((byte)TestMessageID.HeadTransform); //把要发送的数据写这儿或在这儿从其他地方来调用,然后发送到服务器 //1,把要发送的数据先转换为 byte[] byteArray = ..... int inTest = 10; msg.Write(inTest); string str = "Hello Poem and poet!"; msg.Write(str); //基础数据类型都可以通过这样的方式传送 double doubleTest = 3.14159; byte[] inTestArray = BitConverter.GetBytes(doubleTest); //byte[] data = inTestArray; ////2 把转换后byte[]数组的长度写入 msg.Write(Length); msg.Write(inTestArray.Length); //uint len = (uint)inTestArray.Length; ////3, 把数据转化为要求发送的格式 msg.WriteArray(byteArray,(uint)Length); msg.WriteArray(inTestArray, (uint)inTestArray.Length); // Send the message as a broadcast, which will cause the server to forward it to all other users in the session. serverConnection.Broadcast( msg, MessagePriority.Immediate, MessageReliability.UnreliableSequenced, MessageChannel.Avatar); } }
e, Remote Client。如果只负责接受数据则只需要
RemoteHeadManager个脚本,如果要发送数据则两个都需要。
step1,
RemoteHeadManager
private void Start()
{
//CustomMessages.Instance.MessageHandlers[CustomMessages.TestMessageID.HeadTransform] = UpdateHeadTransform;
CustomMessages.Instance.MessageHandlers[CustomMessages.TestMessageID.HeadTransform] = Recv_OtherClientMesssages;
// SharingStage should be valid at this point, but we may not be connected.
if (SharingStage.Instance.IsConnected)
{
Connected();
}
else
{
SharingStage.Instance.SharingManagerConnected += Connected;
}
}
..........................
//接受来自其他客户端的信息
private void Recv_OtherClientMesssages(NetworkInMessage msg)
{
// Parse the message
long userID = msg.ReadInt64();
int rec = msg.ReadInt32();
Debug.Log("rec is " + rec);
string str = msg.ReadString();
Debug.Log("str" + str);
int len = msg.ReadInt32();
byte[] data = new byte[sizeof(double)];
msg.ReadArray(data, (uint)len);
double pi = BitConverter.ToDouble(data, 0);
Debug.Log("pi" + pi);
}
step2, 如果要发数据写法和Local client的写法一样。