- Home /
Virtual Whiteboard stream writing problem
Hey I am building a collaborative 3d whiteboard. Users can draw on a 3d whiteboard which all the other users can see and edit.My problem is only the user who starts the server is able to write, while the rest can only see his drawing and cannot stream their drawing over the network. Any suggestions?My network streaming code is this
void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info) {
if (stream.isWriting) {
Debug.Log("Writing");
if(Input.GetMouseButton(0)){
myray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(myray, out myhit, 100.0f) && Input.GetMouseButtonDown(0))
Debug.Log(myhit.point);
mouseDown =Input.GetMouseButton(0);
curr = myhit.point;
Vector3 currentC=curr;
//Debug.Log("This is the Current Coordinate"+ currentC);
stream.Serialize(ref currentC);
stream.Serialize(ref mouseDown);
Debug.Log(curr);
}
else {
mouseDown =Input.GetMouseButton(0);
stream.Serialize(ref mouseDown);
}
}
else {
Debug.Log("Reading");
Vector3 currentC= new Vector3(0,0,-100.0f);
mouseDown=false;
stream.Serialize(ref currentC);
stream.Serialize(ref mouseDown);
Debug.Log(mouseDown);
curr=currentC;
Debug.Log(curr);
if(mouseDown){
if(last.z != -100.0f) {
myObject.createLine(last,curr,lineSize,col);
Debug.Log("last:"+last+" current:"+curr);
points.Add(curr);
}
last = curr;
}
else {
last.z = -100.0f;
}
}
I am new to unity3d ..can anyone help?
Answer by Bunny83 · Jun 27, 2011 at 12:01 PM
Only the owner of a NetworkView can "send" data because it's his object so he controls it. All data streaming with OnSerializeNetworkView is always unidirectional. In your case it's better to use RPCs.
Btw: you have to be careful you can stream different content in OnSerializeNetworkView but you have to make sure that the clients are able to read it the same way. It seems you have two different "network-packet-formats": (Vector3 + bool) or (bool). You can't distinguish between those two when receiving it. If you send only a bool but the clients read a Vector3 you will end up with garbage data.
One way to send variable data is that you send always a byte first that identifies what data follows. If you just reverse the bool and Vector3 you could use the bool to selectively write & read the Vector3.
Anyways :D RPCs!
float sendRate = 20.0f;
float nextUpdate = 0.0f;
void Update()
{
bool mouseDown = Input.GetMouseButton(0);
myray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(myray, out myhit, 100.0f) && Input.GetMouseButtonDown(0))
{
if (Time.time > nextUpdate)
{
// Send to all others
networkView.RPC("OnNetworkMouseMove",RPCMode.Others,myhit.point,mouseDown,Network.player);
// call it locally
OnNetworkMouseMove(myhit.point, mouseDown, Network.player);
nextUpdate = Time.time + 1.0f / sendRate;
}
}
}
[RPC]
void OnNetworkMouseMove(Vector3 mousePosition, bool down, NetworkPlayer owner)
{
// Now save the points you get seperated for each player!!
// If you use one list and two players are drawing it will be a mixture ;)
// Also if down is false you should save the last mouseposition of each player,
// otherwise it will produce a continuous line which can't be interrupted.
}
Just a small note: I used RPCMode.Others and not RPCMode.All because if you are the server RPCMode.All will not send it to you. The server can't send a message to itself. On clients it would be no problem but it's also unnecessary traffic.
I don't know how you actually "draw" so that's up to you ;) Just make sure you seperate the users by it's NetworkPlayer and you'll be fine.
It can be optimised by sending nothing when the mouse position hasn't changed. usually 90%+ of the users will idle so it's better to do nothing ;)
Your answer
Follow this Question
Related Questions
Mysterious OnSerializeNetworkView calls with an invalid NetworkMessageInfo.sender 1 Answer
How does BitStream work? 1 Answer
Data Synchronization using OnSerializeNetworkView 1 Answer
How to send a gameobject in the OnSerializeNetworkView() 0 Answers
OnSerializeNetworkView NOT sending a bool value when button is pressed??? 2 Answers