Hey all
I’m trying to make a small project where a phone build should send its device orientation to the editor, to simulate VR headmovement. Its just to have an ease-of-use way of testing builds without putting your phone in the google cardboard, gearvr or whatever.
I decided to try and send the device orientation via a Vector3 SyncVar, but nothing happens on the receiving end. I set up the phone to act as a server, and the editor to act as a client (SyncVars go from server->client).
I double checked the device gyro output, and it reads just fine on the phone build, but isnt coming across the network. I just tagged my orientation variable with [SyncVar] and the each frame I update that value with the gyro readings.
I see the “Connected to server” message, so I’m assuming theres an actual connection.
Another solution would be to use OnSerialize/OnDeserialize, but the OnSerialize function doesnt seem to exist? Using Unity 5.3.4.
What am I missing here?
code below
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
public class MyNetworkManager : NetworkBehaviour {
public bool isAtStartup = true;
private bool serverMode = false;
private bool serverRunning = false;
NetworkClient myClient;
[SyncVar(hook ="OnRotChange")]
public Vector3 phoneOrientation;
// Use this for initialization
void Start () {
Input.gyro.enabled = true;
}
void OnRotChange(Vector3 rot)
{
Debug.Log("new value: "+rot);
}
// Update is called once per frame
void Update () {
if (isServer)
{
SyncPhoneRotationData();
}
}
void SyncPhoneRotationData()
{
phoneOrientation = Input.gyro.attitude.eulerAngles;
}
void OnGUI()
{
if (isAtStartup)
{
if (GUI.Button(new Rect(10,10,100,100),"Phone server"))
{
SetupServer();
}
if (GUI.Button(new Rect(10, 120, 100, 100), "Desktop client"))
{
SetupClient();
}
}
}
// Create a server and listen on a port
public void SetupServer()
{
NetworkServer.Listen(4444);
isAtStartup = false;
serverMode = true;
}
// Create a client and connect to the server port
public void SetupClient()
{
myClient = new NetworkClient();
myClient.RegisterHandler(MsgType.Connect, OnConnected);
myClient.Connect("192.168.1.71", 4444);
isAtStartup = false;
}
// Create a local client and connect to the local server
public void SetupLocalClient()
{
myClient = ClientScene.ConnectLocalServer();
myClient.RegisterHandler(MsgType.Connect, OnConnected);
isAtStartup = false;
}
// client function
public void OnConnected(NetworkMessage netMsg)
{
Debug.Log("Connected to server");
}
}