Search Unity

Can't LoadRawTextureData from Client to Server using UNET

Discussion in 'UNet' started by kenmarold, Oct 30, 2017.

  1. kenmarold

    kenmarold

    Joined:
    Jun 11, 2015
    Posts:
    27
    I'm at a loss as to why this is not working but I cannot apply a texture to a gameobject that I'm instantiating on a server, from a client. I'm preparing all of my data and creating an object to send as a byte[], sending my data, receiving the data, instantiating my player on the server, but failing on applying the texture to my player. I'm not getting any errors. What is going wrong?

    screenshot.png

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.Networking;
    4. using UnityEngine.UI;
    5.  
    6. public class Player_Data
    7. {
    8.     public byte[] texBytes;
    9.     public int texWidth;
    10.     public int texHeight;
    11.  
    12.     public Vector3 location;
    13.     public string type;
    14.     public string id;
    15.     public int strength;
    16.     public int hitpoints;
    17. }
    18.  
    19. public class Player_Controller : NetworkBehaviour
    20. {
    21.     Player_Data playerData = new Player_Data();
    22.  
    23.     public GameObject clientAvatar;
    24.     public GameObject serverAvatar;
    25.  
    26.     public Texture2D texToSend;
    27.     string typeToSend = "Deer";
    28.     public string idToSend { get; set; }
    29.     int strengthToSend = 80;
    30.     int hitPointsToSend = 2;
    31.  
    32.     // The OnStartLocalPlayer function is a good place to do initialization
    33.     // that is only for the local player, such as configuring cameras and input.
    34.  
    35.     public override void OnStartLocalPlayer()
    36.     {
    37.         PrepareData();
    38.     }
    39.  
    40.     [Client]
    41.     void InstantiateClientAvator()
    42.     {
    43.         Instantiate(clientAvatar);
    44.  
    45.         Renderer avatarRend;
    46.         avatarRend = clientAvatar.GetComponent<Renderer>();
    47.         avatarRend.sharedMaterial.mainTexture = texToSend;
    48.     }
    49.  
    50.     [Client]
    51.     void PrepareData()
    52.     {
    53.         Player_ID id = GetComponent<Player_ID>();
    54.         Player_Spawn spawn = GetComponent<Player_Spawn>();
    55.  
    56.         StartCoroutine(DoGetRawTextureData(texToSend));
    57.  
    58.         playerData.texWidth = texToSend.width;
    59.         playerData.texHeight = texToSend.height;
    60.         playerData.location = spawn.GetPlayerPos();
    61.         playerData.type = typeToSend;
    62.         playerData.id = id.MakeUniqueIdentity();
    63.         playerData.strength = strengthToSend;
    64.         playerData.hitpoints = hitPointsToSend;
    65.  
    66.         SendData(playerData);
    67.     }
    68.     IEnumerator DoGetRawTextureData(Texture2D tex)
    69.     {
    70.         playerData.texBytes = texToSend.GetRawTextureData();
    71.  
    72.         yield return new WaitForEndOfFrame();
    73.  
    74.         GameObject infoDisplayText = GameObject.Find("InfoDisplay");
    75.         infoDisplayText.GetComponent<Text>().text += "Bytes to send : " + playerData.texBytes.Length + "\n";
    76.     }
    77.  
    78.     // Send Client player data to server
    79.  
    80.     [Client]
    81.     void SendData(Player_Data data)
    82.     {
    83.         CmdInstantiatePlayerOnServer(data);
    84.         CmdInstantiatePlayerOnClient(data);
    85.         CmdShowClientDataOnServer(data);
    86.     }
    87.  
    88.     [Client]
    89.     void CmdInstantiatePlayerOnClient(Player_Data data)
    90.     {
    91.         GameObject go = (GameObject)Instantiate(Resources.Load("Avatar"), data.location, Quaternion.identity) as GameObject;
    92.  
    93.         //StartCoroutine(DoApplyTex(go, data.texBytes, data.texWidth, data.texHeight));
    94.  
    95.         go.GetComponent<Renderer>().material.mainTexture = texToSend;
    96.     }
    97.  
    98.     [Command(channel = 2)]
    99.     void CmdInstantiatePlayerOnServer(Player_Data data)
    100.     {
    101.         GameObject go = (GameObject)Instantiate(Resources.Load("Avatar"), data.location, Quaternion.identity) as GameObject;
    102.  
    103.         StartCoroutine(DoApplyTex(go, data.texBytes, data.texWidth, data.texHeight));
    104.     }
    105.  
    106.     IEnumerator DoApplyTex(GameObject go, byte[] texBytes, int texWidth, int texHeight)
    107.     {
    108.         Texture2D tex = new Texture2D(texWidth, texHeight);
    109.         tex.LoadRawTextureData(texBytes);
    110.         tex.Apply();
    111.         yield return new WaitForEndOfFrame();
    112.         go.GetComponent<Renderer>().material.mainTexture = tex;
    113.     }
    114.  
    115.     [Command(channel = 2)]
    116.     void CmdShowClientDataOnServer(Player_Data data)
    117.     {
    118.         GameObject infoDisplayText = GameObject.Find("InfoDisplay");
    119.         infoDisplayText.GetComponent<Text>().text += "Image Size : " + data.texBytes.Length.ToString() + "\n";
    120.         infoDisplayText.GetComponent<Text>().text += "Image Width : " + data.texWidth.ToString() + "\n";
    121.         infoDisplayText.GetComponent<Text>().text += "Image Height : " + data.texHeight.ToString() + "\n";
    122.         infoDisplayText.GetComponent<Text>().text += "Player Location : " + data.location.ToString() + "\n";
    123.         infoDisplayText.GetComponent<Text>().text += "Player Type : " + data.type + "\n";
    124.         infoDisplayText.GetComponent<Text>().text += "Player ID : " + data.id + "\n";
    125.         infoDisplayText.GetComponent<Text>().text += "Player Strength : " + data.strength + "\n";
    126.         infoDisplayText.GetComponent<Text>().text += "Player Hit Points : " + data.hitpoints + "\n";
    127.  
    128.         RpcDataReceivedConfirmation("Data Received by Server");
    129.     }
    130.  
    131.     // Show Server Confimration on Client InfoDisplay
    132.  
    133.     [ClientRpc]
    134.     void RpcDataReceivedConfirmation(string data)
    135.     {
    136.         if (isLocalPlayer)
    137.         {
    138.             GameObject infoDisplayText = GameObject.Find("InfoDisplay");
    139.             infoDisplayText.GetComponent<Text>().text += data + "\n";
    140.         }
    141.     }
    142. }