Live webcam stream in unity

So, I’ve been trying to make a webcam stream where the server sends a live stream from his webcam to the clients on the network. I’ve tried to write a code myself (I’m not very good at it), but for some reason it doesn’t work. Here it is:

#pragma strict

var webcamTexture : WebCamTexture;
var data : Color32[];


function OnServerInitialized() {
    	webcamTexture = WebCamTexture();
   		renderer.material.mainTexture = webcamTexture;  
   		webcamTexture.Play();
    	data = new Color32[webcamTexture.width * webcamTexture.height];
}

function Update () {
    webcamTexture.GetPixels32 (data);
}

function OnConnectedToServer() {
   	networkView.RPC ("WebcamStream", RPCMode.Others, data, webcamTexture);
    webcamTexture.Play();
}

@RPC
function WebcamStream (data : Color32 [], webcamTexture: WebCamTexture) {
	var	newwebcamTexture: WebCamTexture = webcamTexture;
	renderer.material.mainTexture = newwebcamTexture;
 	var newdata: Color32[] = data;
    webcamTexture.GetPixels32 (newdata);
}

It just results in a blank screen on the client’s side. Any help will be much appreciated :slight_smile:

Do you get any errors? You do know you can't just send textures using the inbuilt Networking

There was a tutorial I saw, where the guy was able to send textures along the network (when the server clicked a button, the texture of a certain object changed in the whole network). The thing is I can't get that texture to be updated from the webcam display. What do you suggest I do?

Can you post the link to that tutorial?

2 Answers

2

If someone still needs examples, you may try FMETP STREAM | Forum

It provides Streaming Demo with UDP/TCP/WebSocket solution, and Web browser demo with Socket.IO.

All source code are written in C# and easy to modify.

Supported: Android/iOS/Mac/PC/AR/VR/Linux/HoloLens/OculusGO/MagicLeap/WebGL/HTML…etc

Hello,

he problem in your program is that you have used invalid arguments for RPC

Valid RPC parameters are int, float, string, NetworkPlayer, NetworkViewID, Vector3 and Quaternion.

so you cannot pass anything else other than the above in RPC…

In this case you have to use OnSerializeNetworkView for transferring data. And you cannot transfer webcam texture straight away, you have to use GetPixel32 in webcamtexture class. and get the array, use serialize on the array and it would be sent to the other system (This part will be more clear if u check the example in OnSerializeNetworkview page)

yes correct only in update as it need to know every frrame if being used