How to stream live video from webcam to in-game using photon fusion in unity? Also using raw image texture to display the webcamtexture

244 Views Asked by At

I am trying to make a game where players can be able to see each other live stream or live cam while playing. The stream will play the webcam video on a raw image texture. I'm making the multiplayer using photon fusion in shared mode and unity. I used chat-gpt for the solution and he gave me the code something like this,

using Photon.Pun.Fusion;
using UnityEngine;
using UnityEngine.UI;

public class WebCamStreamer : MonoBehaviour
{
    public RawImage rawImage;

    private WebCamTexture webcamTexture;

    private void Start()
    {
        webcamTexture = new WebCamTexture();
        webcamTexture.Play();
    }

    private void Update()
    {
        // Update the RawImage texture with the webcam texture
        rawImage.texture = webcamTexture;

        // Convert the webcam texture to a byte array
        byte[] imageData = webcamTexture.GetRawTextureData();

        // Send the byte array to all connected peers
        PhotonNetwork.Send(new WebCamEventData(imageData), PhotonNetwork.RaiseEventOptions.Default, null);
    }

    private void OnEnable()
    {
        PhotonNetwork.NetworkingClient.EventReceived += OnEventReceived;
    }

    private void OnDisable()
    {
        PhotonNetwork.NetworkingClient.EventReceived -= OnEventReceived;
    }

    private void OnEventReceived(EventData eventData)
    {
        if (eventData.Code == PhotonFusionDemoEvents.WebCamEventCode)
        {
            object[] data = (object[])eventData.CustomData;
            byte[] imageData = (byte[])data[0];

            // Create a new texture from the received image data
            Texture2D texture = new Texture2D(webcamTexture.width, webcamTexture.height, TextureFormat.RGBA32, false);
            texture.LoadRawTextureData(imageData);
            texture.Apply();

            // Update the RawImage texture with the received image data
            rawImage.texture = texture;
        }
    }
}

This code is somewhat inappropriate for the photon fusion shared mode. I couldnt find anything about the

PhotonNetwork.Send(new WebCamEventData(imageData), PhotonNetwork.RaiseEventOptions.Default, null);

Can anyone help me with how to Use the PhotonNetwork.Send method to send the raw image data to all connected peers in the Update method of the script for photon fusion and How to Use the PhotonNetwork.OnEvent method to receive the raw image data from other peers and update the RawImage texture accordingly in the OnEventReceived method in photon fusion?

0

There are 0 best solutions below