如何使用统一的光子融合将实时视频从网络摄像头流式传输到游戏中?也使用原始纹理

问题描述 投票:0回答:0

我正在尝试制作一款游戏,玩家可以在玩游戏时看到彼此的直播或实时摄像头。流将在原始图像纹理上播放网络摄像头视频。我在共享模式和统一模式下使用光子融合制作多人游戏。我使用 chat-gpt 作为解决方案,他给了我这样的代码,

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;
        }
    }
}

这段代码对于光子融合共享模式有些不合适。我找不到任何关于

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

任何人都可以帮助我如何使用 PhotonNetwork.Send 方法将原始图像数据发送到光子融合脚本的更新方法中的所有连接的对等点和 光子融合中的OnEventReceived方法如何使用PhotonNetwork.OnEvent方法接收其他同行的原始图像数据并相应地更新RawImage纹理?

c# unity3d photon fusion
© www.soinside.com 2019 - 2024. All rights reserved.