Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Third Party Mirror Unity Observer Mode

Discussion in 'Multiplayer' started by kusi2017, Oct 7, 2023.

  1. kusi2017

    kusi2017

    Joined:
    Jan 25, 2017
    Posts:
    2
    I have a network connection using Mirror. There is a Client that is cycling. And there is a Server that has a canvas with a Raw Image.

    I want to stream live feed from the Client's camera to the Server's Canvas. How to do this? Please help me

    I tried this solution, but Camera texture and Raw Image texture is empty.

    public class CameraMultiplayer : NetworkBehaviour
    {
    public Camera clientCamera;
    public RawImage serverRawImage;

    private Texture2D clientCameraTexture;

    private void Start()
    {
    int screenWidth = Screen.width;
    int screenHeight = Screen.height;
    clientCameraTexture = new Texture2D(screenWidth, screenHeight, TextureFormat.RGB24, false);
    }

    private void OnPostRender()
    {
    CaptureCameraImage();

    if (isClient)
    {
    CmdSendTextureToServer(clientCameraTexture.EncodeToPNG());
    }
    }
    private void CaptureCameraImage()
    {
    clientCameraTexture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
    clientCameraTexture.Apply();
    }

    [Command].
    private void CmdSendTextureToServer(byte[] textureBytes)
    {
    Texture2D receivedTexture = new Texture2D(Screen.width, Screen.height);
    receivedTexture.LoadImage(textureBytes);

    serverRawImage.texture = receivedTexture;

    RpcReceiveTextureOnClients(textureBytes);
    }

    [ClientRpc].
    private void RpcReceiveTextureOnClients(byte[] textureBytes)
    {
    Texture2D receivedTexture = new Texture2D(Screen.width, Screen.height);
    receivedTexture.LoadImage(textureBytes);

    serverRawImage.texture = receivedTexture;
    }
    }