Search Unity

Sending Texture over PhotonNetwork

Discussion in 'Connected Games' started by jobjongebloet, May 9, 2019.

  1. jobjongebloet

    jobjongebloet

    Joined:
    Apr 8, 2019
    Posts:
    7
    Hi everyone,

    I have 2 unity builds.
    1. is my mobile device
    the other one is my desktop.

    I use the PUN (Photon Unity Network) to connect them both in a single room.

    I have texture on one side, similar to a webcam texture. But in my case it's my monitor.
    My mobile device build doesn't have access to this texture directly.
    I want to send it over the network and update a mesh with this texture. (Copy the texture and update it, same thing)

    Hope someone can help me out.
    Cheers Zelos

    TLDR; Send a texture from the host to a client.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Photon.Pun;
    5.  
    6. public class SendTex : MonoBehaviour
    7. {
    8.  
    9.     private PhotonView PV;
    10.  
    11.  
    12.     void Start()
    13.     {
    14.         PV = GetComponent<PhotonView>();
    15.     }
    16.  
    17.  
    18.     public Texture2D textureToSend;
    19.     private Texture2D receivedTexture;
    20.  
    21.     void Update()
    22.     {
    23.         if (PV.IsMine)
    24.         {
    25.             if (Input.GetKeyDown(KeyCode.X))
    26.             {
    27.                 PV.RPC("Send", RpcTarget.AllBuffered, textureToSend.EncodeToPNG());
    28.             }
    29.         }
    30.     }
    31.  
    32.     [PunRPC]
    33.     private void Send(byte[] receivedByte)
    34.     {
    35.         receivedTexture = new Texture2D(1, 1);
    36.         receivedTexture.LoadImage(receivedByte);
    37.     }
    38. }
    39.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Photon.Pun;
    5.  
    6. public class UpdateTex : MonoBehaviour
    7. {
    8.  
    9.     private PhotonView PV;
    10.     public Texture2D receivedTexture;
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         PV = GetComponent<PhotonView>();
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.         if (PV.IsMine)
    22.         {
    23.             PV.RPC("ChangeTexture", RpcTarget.AllBuffered, 1);
    24.         }
    25.        
    26.     }
    27.  
    28.     [PunRPC]
    29.  
    30.     void ChangeTexture (int choice)
    31.     {
    32.         if (choice == 1)
    33.         {
    34.             GetComponent<Renderer>().material.mainTexture = receivedTexture;
    35.         }
    36.     }
    37. }
    38.  
     
  2. lgmovil183

    lgmovil183

    Joined:
    Mar 30, 2019
    Posts:
    1
    Hi there! Works? I need to implement the function of sending a texture captured with the webcam to the other clients in the room. Like a video conference