Search Unity

Requested Code!Send Texture Over Network

Discussion in 'Multiplayer' started by Omidja, Aug 19, 2014.

  1. Omidja

    Omidja

    Joined:
    Jul 16, 2014
    Posts:
    19
    hi.many members asked this question.and i followed all answers!but nobody couldnt send texture!
    i tried all ways!but not works!
    please help me!Some body write a script for send texture please!many people need it!i need too!

    (i ready to pay your work cost)
     
  2. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    Create an enum ( : byte) which correlates to the textures, then send the byte (good for bandwidth) and load the texture locally. That is one way to do it ;)
     
    Omidja likes this.
  3. Omidja

    Omidja

    Joined:
    Jul 16, 2014
    Posts:
    19
    i dont know how can i do it?(please send for me a script!and i can compensate your help!)
    this is my last code:
    Code (CSharp):
    1.     public Texture2D MyTexture;
    2.     public byte[] N;
    3.     public Texture2D texi;
    4.     void Start () {
    5.          texi = new Texture2D(MyTexture.width,MyTexture.height);
    6.     }
    7.     void Update () {
    8.         if(Network.isServer){
    9.         if(Input.GetKeyDown(KeyCode.X)){
    10.             StartCoroutine(GetRenderTexturePixel(MyTexture));
    11.             networkView.RPC("Send", RPCMode.Other,N);
    12.         }
    13.         }
    14.         if(Network.isClient){
    15.         if(Input.GetKeyDown(KeyCode.X)){
    16.             texi.LoadImage(N);
    17.             renderer.material.mainTexture = texi;
    18.         }
    19.         }
    20.     }
    21.     IEnumerator GetRenderTexturePixel(Texture2D tex)
    22.     {
    23.         Texture2D tempTex = new Texture2D(tex.width, tex.height);
    24.         yield return new WaitForEndOfFrame();
    25.         tempTex.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0);
    26.         tempTex.Apply();
    27.         N= tempTex.EncodeToPNG();
    28.     }
    29.     [RPC]
    30.     public  void Send(byte[] receivedByte){
    31.         N=receivedByte;
    32.     }
    this code works for rendertexture.i tested it!(with Some Edit in scripts)!but it didnt send mytexture !
     
    Last edited: Aug 19, 2014
  4. Cha0os

    Cha0os

    Joined:
    Feb 22, 2014
    Posts:
    17
    Code (CSharp):
    1.     public Texture2D textureToSend;
    2.  
    3.     private Texture2D receivedTexture;
    4.  
    5.     void Update () {
    6.         if(Network.isServer){
    7.             if(Input.GetKeyDown(KeyCode.X)){
    8.                 networkView.RPC("Send", RPCMode.Others, textureToSend.EncodeToPNG());
    9.             }
    10.         }
    11.     }
    12.  
    13.     [RPC]
    14.     private void Send(byte[] receivedByte){
    15.         receivedTexture = new Texture2D(1, 1);
    16.         receivedTexture.LoadImage(receivedByte);
    17.     }

    This will take "textureToSend" and send it to all clients when you press x. The clients will store the received texture in "receivedTexture".
     
    Last edited: Aug 19, 2014
    Psyco92 and Omidja like this.
  5. Omidja

    Omidja

    Joined:
    Jul 16, 2014
    Posts:
    19
    wow.that was so easy,but i didnt know.i tested it with a simple texture.but my game have many big texture.i will test this code and come back!thankyou Cha0os
     
  6. Omidja

    Omidja

    Joined:
    Jul 16, 2014
    Posts:
    19
    that works nicely.

    now i want know sending texture with networkview is rational for mmo games or www load from urls?

    i have a host!my textures uploaded!now do i download textures with www foreach client or do i download textures by server and server sends textures to other clients with networkview?
     
    Last edited: Aug 20, 2014