接受数据
RemoteCamera.cs
using UnityEngine;
public class RemoteCamera : MonoBehaviour {
public static byte[] currentFrame;
private Texture2D viewTexture;
public static int CameraID;
// Use this for initialization
void Start () {
currentFrame = new byte[640 * 480 * 4];
viewTexture = new Texture2D(640, 480);
}
// Update is called once per frame
void Update ()
{
viewTexture.LoadImage(currentFrame);
viewTexture.Apply();
GetComponent<Renderer>().material.mainTexture = viewTexture;
}
}
LocalCustomMessagesManager.cs
/// <summary>
/// Called when a remote user sends a message.
/// </summary>
/// <param name="msg"></param>
private void UpdateMessage(NetworkInMessage msg)
{
// Parse the message
long userID = msg.ReadInt64();
//接受video
int videoLen = msg.ReadInt32();
if (videoLen > 1) {
msg.ReadArray (RemoteCamera.currentFrame, (uint)videoLen);
}
//接受场景位置
Scene.transform.localPosition = LocalCustomMessages.ReadVector3(msg);
Scene.transform.localRotation = LocalCustomMessages.ReadQuaternion(msg);
//接收头部位置
remotehead.transform.localPosition = LocalCustomMessages.ReadVector3(msg);
remotehead.transform.localRotation = LocalCustomMessages.ReadQuaternion(msg);
//接收眼动位置
remotegaze.transform.localPosition = LocalCustomMessages.ReadVector3(msg);
remotegaze.transform.localRotation = LocalCustomMessages.ReadQuaternion(msg);
}
发送数据
LocalCamera.cs
using UnityEngine;
using System;
public class LocalCamera : MonoBehaviour
{
public static Texture2D viewTexture;
private WebCamTexture viewTextureCamera;
static public int CameraID;
// Use this for initialization
void Start ()
{
WebCamDevice[] devices = WebCamTexture.devices;
viewTextureCamera = new WebCamTexture(devices[0].name, 1280, 720, 30);
viewTextureCamera.Play();
viewTexture = new Texture2D(viewTextureCamera.width, viewTextureCamera.height);
}
// Update is called once per frame
void Update ()
{
viewTexture.SetPixels(viewTextureCamera.GetPixels());
viewTexture.Apply();
GetComponent<Renderer>().material.mainTexture = viewTexture;
}
}
LocalCustomMessages.cs
public void SendMessage()
{
// If we are connected to a session, broadcast our head 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.StringMessageID);
//send video
byte[] currentLocalVideo = LocalCamera.viewTexture.EncodeToJPG();
msg.Write(currentLocalVideo.Length);
msg.WriteArray(currentLocalVideo, (uint)currentLocalVideo.Length);
//发送场景位置
AppendVector3(msg, Scene.transform.localPosition);
AppendQuaternion(msg, Scene.transform.localRotation);
//发送头部位置
AppendVector3(msg, headPos.transform.localPosition);
AppendQuaternion(msg, headPos.transform.localRotation);
//发送眼动位置
AppendVector3(msg, GazePos.transform.localPosition);
AppendQuaternion(msg, GazePos.transform.localRotation);
}
}