Search Unity

Third Party Photon network problem

Discussion in 'Multiplayer' started by jobjongebloet, May 10, 2019.

  1. jobjongebloet

    jobjongebloet

    Joined:
    Apr 8, 2019
    Posts:
    7
    Hi I have this code that sends a byte array to my second build.

    It is super slow.
    It takes the view of my camera even due the texture is from my object.
    The Pun RPC doesn't even correspond SendTextures-UpdateMonitor
    I have both objects with PhotonView ID set to 1 or else it doesn't work.

    And it seems like there is a memory leak. So it crashes sometimes.

    I can't really use my own texture for some reason because it will give the out of bound error.
    attempting to ReadPixels outside of RenderTexture bounds! Reading (0, 0, 1920, 1080) from (476, 554)

    Does anyone have an idea how to properly send texture from one client to another?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Photon.Pun;
    5.  
    6. public class Send : MonoBehaviour
    7. {
    8.     public Texture2D MyTexture;
    9.     private PhotonView PV;
    10.     public byte[] receivedBytes;
    11.     public byte[] N;
    12.     public Texture2D texi;
    13.  
    14.     void Start()
    15.     {
    16.         // MyTexture = TextureToTexture2D(GetComponent<Renderer>().material.mainTexture);
    17.         texi = new Texture2D(MyTexture.width, MyTexture.height);
    18.         PV = GetComponent<PhotonView>();
    19.     }
    20.  
    21.     private Texture2D receivedTexture;
    22.  
    23.     void Update()
    24.     {
    25.         if (PV.IsMine)
    26.         {
    27.             StartCoroutine(GetRenderTexturePixel(MyTexture));
    28.             // MyTexture = TextureToTexture2D(GetComponent<Renderer>().material.mainTexture);
    29.             PV.RPC("SendTextures", RpcTarget.AllBuffered, N);
    30.         }
    31.     }
    32.  
    33.     IEnumerator GetRenderTexturePixel(Texture2D tex)
    34.     {
    35.         Texture2D tempTex = new Texture2D(tex.width, tex.height);
    36.         yield return new WaitForEndOfFrame();
    37.         tempTex.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0);
    38.         tempTex.Apply();
    39.         N = tempTex.EncodeToPNG();
    40.     }
    41.  
    42.     //private Texture2D TextureToTexture2D(Texture texture)
    43.     //{
    44.     //    Texture2D texture2D = new Texture2D(texture.width, texture.height, TextureFormat.RGBA32, false);
    45.     //    RenderTexture currentRT = RenderTexture.active;
    46.     //    RenderTexture renderTexture = RenderTexture.GetTemporary(texture.width, texture.height, 32);
    47.     //    Graphics.Blit(texture, renderTexture);
    48.  
    49.     //    RenderTexture.active = renderTexture;
    50.     //    texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
    51.     //    texture2D.Apply();
    52.  
    53.     //    RenderTexture.active = currentRT;
    54.     //    RenderTexture.ReleaseTemporary(renderTexture);
    55.     //    return texture2D;
    56.     //}
    57.  
    58.     [PunRPC]
    59.     void RPC_UpdateMonitor(byte[] receivedByte)
    60.     {
    61.         receivedTexture = null;
    62.         receivedTexture = new Texture2D(1, 1);
    63.         receivedTexture.LoadImage(receivedByte);
    64.         GetComponent<Renderer>().material.mainTexture = receivedTexture;
    65.     }
    66. }
    67.  
     
  2. jobjongebloet

    jobjongebloet

    Joined:
    Apr 8, 2019
    Posts:
    7
    First thing I will do is change PV.IsMine to Is.client or something