- Home /
Render a live webcamtexture in photon 2
Hi, I want to render a webcamtexture on a player in cube. So Every player in game have its own video on their cub. Currently i am use RPC call but after few seconds my player disconnect and my game crashed
Any one have idea how to do it I am using PUN 2
Script that i am using given blewo
void Update() { if (photonView.IsMine) {
if (backCam == null)
{
backCam = new WebCamTexture(100, 100, 10);
}
if (!backCam.isPlaying)
{
backCam.Play();
}
Texture2D tex = new Texture2D(backCam.width, backCam.height, TextureFormat.RGB24, false);
tex.SetPixels(backCam.GetPixels());
tex.Apply();
byte[] bytes = tex.EncodeToJPG();
PhotonView photonView = PhotonView.Get(this);
photonView.RPC("MyTextuerRPC", RpcTarget.All, bytes);
}
} [PunRPC] void MyTextuerRPC(byte[] bytes) {
Texture2D tex = new Texture2D(2, 2);
// Load data into the texture.
tex.LoadImage(bytes);
// Assign texture to renderer's material.
GetComponent<Renderer>().material.mainTexture = tex;
}
Answer by mircea_02 · Dec 21, 2020 at 06:45 PM
I use a similar script, and I had the same problem: The memory was growing until the time of the crash. I solved it by entering the line: "Resources.UnloadUnusedAssets (); " somewhere before that line "Texture2D.tex = ........." Don't ask me for more. Some have the script in "Update", others in "IEnumerator", others (me) in an "Invoke", so don't ask me more than that. Just experiment.. I hope I was helpful