如何实时展示团结玩家的网络摄像头? (我尝试使用光子)

发布于 2025-02-09 15:49:25 字数 1504 浏览 1 评论 0原文

我正在尝试制作一款多人游戏,每个玩家面对头像的网络摄像头显示。我获得了null引用异常:对象引用未设置为对象的实例。以下代码附加到Quad,网络摄像头输出为 应显示转换为JPEG。任何帮助将不胜感激。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;

public class photonwebcam : MonoBehaviour
{  
    WebCamTexture webcamTexture;
    PhotonView photonView;

    void Start()
    {    
        PhotonView photonView = PhotonView.Get(this);
        if (!webcamTexture.isPlaying)
        {
            webcamTexture = new WebCamTexture();
            Renderer renderer = GetComponent<Renderer>();
            renderer.material.mainTexture = webcamTexture;
            webcamTexture.Play();

        }
    
    }

    void FixedUpdate()
    {   
        
        Texture2D tex = new Texture2D(webcamTexture.width, webcamTexture.height, TextureFormat.RGB24, false);
        tex.SetPixels(webcamTexture.GetPixels());
        tex.Apply();
        byte[] bytes = tex.EncodeToJPG();
        photonView.RPC("VideoToBytes", RpcTarget.AllBuffered, bytes);
        Resources.UnloadUnusedAssets();
        
    }

    [PunRPC]
    public void VideoToBytes(byte[] bytes)
    {   
        if (bytes!= null)
        {
            Texture2D texa = new Texture2D(2, 2);
            // Load data into the texture.
            texa.LoadImage(bytes);
            // Assign texture to renderer's material.
            GetComponent<Renderer>().material.mainTexture = texa;
        }
        
    }
}

I am trying to make a multiplayer game where each player has their webcam displayed in the face of their avatar. I am getting the null reference exception: object reference not set to an instance of an object. The following code is attached to a quad in which the webcam output that was
converted to jpeg should be displayed. Any help would be appreciated.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;

public class photonwebcam : MonoBehaviour
{  
    WebCamTexture webcamTexture;
    PhotonView photonView;

    void Start()
    {    
        PhotonView photonView = PhotonView.Get(this);
        if (!webcamTexture.isPlaying)
        {
            webcamTexture = new WebCamTexture();
            Renderer renderer = GetComponent<Renderer>();
            renderer.material.mainTexture = webcamTexture;
            webcamTexture.Play();

        }
    
    }

    void FixedUpdate()
    {   
        
        Texture2D tex = new Texture2D(webcamTexture.width, webcamTexture.height, TextureFormat.RGB24, false);
        tex.SetPixels(webcamTexture.GetPixels());
        tex.Apply();
        byte[] bytes = tex.EncodeToJPG();
        photonView.RPC("VideoToBytes", RpcTarget.AllBuffered, bytes);
        Resources.UnloadUnusedAssets();
        
    }

    [PunRPC]
    public void VideoToBytes(byte[] bytes)
    {   
        if (bytes!= null)
        {
            Texture2D texa = new Texture2D(2, 2);
            // Load data into the texture.
            texa.LoadImage(bytes);
            // Assign texture to renderer's material.
            GetComponent<Renderer>().material.mainTexture = texa;
        }
        
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

被你宠の有点坏 2025-02-16 15:49:25

将为您提供NullPoInterException的一个问题是,您可以创建一个局部变量photonview,而不是分配字段。
这种方法的问题在于,RPC未针对大数据进行优化,因此图像将很糟糕。
我认为您可以使用 agora 而不是RPC来处理网络摄像头共享。

One problem that will give you NullPointerException is that in Start you create a local variable photonView, instead of assigning the field.
The problem with this approach is that RPC is not optimized for large data, so the image will be laggy.
I think you can use Agora instead of RPC to handle webcam sharing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文