如何实时展示团结玩家的网络摄像头? (我尝试使用光子)
我正在尝试制作一款多人游戏,每个玩家面对头像的网络摄像头显示。我获得了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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将为您提供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.