Search Unity

Error with SyncListStruct

Discussion in 'Multiplayer' started by SchChris, May 19, 2017.

  1. SchChris

    SchChris

    Joined:
    May 11, 2017
    Posts:
    5
    Hei everyone.
    I am new to Unity Network and tried to send a Texture over Network but it didn't worked for me, even with tutorials.
    Now I try to send the Webcamtexture to a local second Texture to figure out how it works. I get it simple with texture2.SetPixels(webcamTexture.GetPixels32()) but i try to convert the Webcamtexture into bytes in a struct to send this over Network as soon as it works locally.

    I created a struct and in there a function to set the rgba to single bytes and want to save these structs in a SycnListStruct.
    I createt a public class SyncListWebcamdata which inherit from my SyncListStruct <WebcamData> and made a instance of it.
    On running i get following Error Message:
    UNetWeaver error: SyncListStruct member variable [UnityEngine.Networking.SyncListStruct`1<WebcamData> SendWebcamTextureToAnotherTexture::webcamDataList] must use a dervied class, like "class MySyncList : SyncListStruct<MyStruct> {}".

    Hope you can help me figure this out.

    And is somebody known with the Plugin VideoChat on the Asset Store https://www.assetstore.unity3d.com/en/#!/content/7447 ?

    Is it worth to buy it or should try to make it on own?

    In following is my Code:

    Code (csharp):
    1.  
    2. // send WebcamTexture to another texture2d
    3. using System;
    4. using System.Collections;
    5. using System.Collections.Generic;
    6. using UnityEngine;
    7. using UnityEngine.Networking;
    8. using UnityEngine.UI;
    9.  
    10. // Camera Support http://answers.unity3d.com/questions/1218754/optimize-rpc-streamed-video-as-byte-array-frame-by.html
    11.  
    12.  
    13. public struct WebcamData
    14. {
    15.     public byte red;
    16.     public byte green;
    17.     public byte blue;
    18.     public byte alpha;
    19.  
    20.     public WebcamData(byte _red, byte _green, byte _blue, byte _alpha)
    21.     {
    22.         red = _red;
    23.         green = _green;
    24.         blue = _blue;
    25.         alpha = _alpha;
    26.     }
    27. }
    28.  
    29. public class SyncListWebcamData : SyncListStruct<WebcamData>
    30. {
    31. }
    32.  
    33. public class SendWebcamTextureToAnotherTexture : NetworkBehaviour
    34. {
    35.     //public GameObject webcamHolderPrefab;
    36.     public Texture2D myTexture;
    37.     public Texture2D texture2;
    38.     public WebCamTexture webCamTexture;
    39.     public RawImage display1;
    40.     public RawImage display2;
    41.  
    42.     Color32[] data;
    43.     private byte[] texture1Bytes;
    44.     private byte[] texture2Bytes;
    45.  
    46.     SyncListStruct<WebcamData> webcamDataList = new SyncListStruct<WebcamData>();
    47.  
    48.     WebCamDevice device;
    49.     private NetworkView nView;
    50.     bool isCamSelected;
    51.  
    52.     bool isCamConnected;
    53.  
    54.     // number with connected Devices
    55.     int selectedCam = 0;
    56.     public int SelectedCam
    57.     {
    58.         set
    59.         {
    60.             if (value <= 0)
    61.             {
    62.                 selectedCam = 0;
    63.                 Debug.Log("selectedCam: " + selectedCam);
    64.             }
    65.             if (value > 0)
    66.             {
    67.                 Debug.Log("Value: " + value);
    68.                 selectedCam = WebCamTexture.devices.Length;
    69.                 Debug.Log("selectedCam: " + selectedCam);
    70.             }
    71.         }
    72.  
    73.         get
    74.         {
    75.             return selectedCam;
    76.         }
    77.     }
    78.  
    79.    
    80.     [SyncVar]
    81.     public SyncListWebcamData m_camDatas = new SyncListWebcamData();
    82.  
    83.  
    84.     void Start()
    85.     {
    86.         isCamSelected = false;
    87.         isCamConnected = false;
    88.  
    89.         nView = display1.GetComponent<NetworkView>();
    90.  
    91.         selectedCam = 0;
    92.         #region Check for devices
    93.         if (WebCamTexture.devices.Length > 0)
    94.         {
    95.             Debug.Log("Get count of Devices: " + WebCamTexture.devices.Length);
    96.             selectedCam += 1;
    97.             // Check if ther devices
    98.             //currentCamIndex %= WebCamTexture.devices.Length;
    99.             Debug.Log("selectedCam: " + selectedCam);
    100.         }
    101.  
    102.         // if are more than one webcamdevices connected
    103.         // clear the webcamTexture and stop receiving information from the display
    104.         if (webCamTexture != null)
    105.         {
    106.             Debug.Log("Clear webcamTexture.");
    107.             display1.texture = null;
    108.             webCamTexture.Stop();
    109.             webCamTexture = null;
    110.         }
    111.  
    112.         #endregion
    113.  
    114.         webCamTexture = new WebCamTexture();
    115.         texture2 = new Texture2D(webCamTexture.width, webCamTexture.height);
    116.  
    117.         //device = WebCamTexture.devices[selectedCam];
    118.         //Debug.Log("Devicename: " + device.name);
    119.  
    120.  
    121.  
    122.     }
    123.  
    124.     void Update()
    125.     {
    126.         #region Check if a Cam selected
    127.         if (!isCamConnected)
    128.         {
    129.             SetCam();
    130.             isCamConnected = true;
    131.         }
    132.  
    133.  
    134.         if (isCamSelected)
    135.         {
    136.  
    137.             data = new Color32[webCamTexture.width + webCamTexture.height];
    138.             Debug.Log("data length: " + data.Length);
    139.  
    140.             myTexture = new Texture2D(webCamTexture.width, webCamTexture.height, TextureFormat.RGB24, false);
    141.         }
    142.         #endregion
    143.  
    144.         if (isServer)
    145.         {
    146.             #region Input
    147.             if (Input.GetKeyDown(KeyCode.UpArrow))
    148.             {
    149.                 SelectedCam++;
    150.                 SetCam();
    151.                 Debug.Log("Key Up: " + SelectedCam);
    152.             }
    153.             if (Input.GetKeyDown(KeyCode.DownArrow))
    154.             {
    155.                 SelectedCam--;
    156.                 SetCam();
    157.                 Debug.Log("Key down: " + SelectedCam);
    158.             }
    159.             #endregion
    160.  
    161.             //StartCoroutine(GetPixels());
    162.             //setWebcamToNewTextur(webCamTexture);
    163.  
    164.             ConvertWebcamIntoColors32(webCamTexture);
    165.             Color32ArrayToByteArray(data);
    166.             Debug.Log("texture1Bytes size after function Color32ArrayToByteArray: " + texture1Bytes.Length);
    167.  
    168.             for (int i = 0; i < texture1Bytes.Length; i+= 4)
    169.             {
    170.                 WebcamData webCamData = new WebcamData(texture1Bytes[i], texture1Bytes[i + 1], texture1Bytes[i + 2], texture1Bytes[i + 3]);
    171.                 webcamDataList.Add(webCamData);
    172.             }
    173.             Debug.Log("Webcamstructs size: " + webcamDataList.Count);
    174.         }
    175.         else
    176.         {
    177.             //texture2.LoadImage(texture2Bytes);
    178.         }
    179.  
    180.     }
    181.  
    182.  
    183.     /// <summary>
    184.     /// Only local
    185.     /// Transfer the Webcamtexture to another Texture
    186.     /// Texture2 receive the Webcamtexturedata with _hostWebcam.GetPixels()
    187.     /// and set it with texture2.SetPixels()
    188.     /// textur2 will be set to display2.texture
    189.     /// </summary>
    190.     /// <param name="_hostWebcam"></param>
    191.     //void setWebcamToNewTextur(WebCamTexture _hostWebcam)
    192.     //{
    193.     //    texture2 = new Texture2D(_hostWebcam.width, _hostWebcam.height);
    194.     //    texture2.SetPixels(_hostWebcam.GetPixels());
    195.     //    texture2.Apply();
    196.     //    display2.texture = texture2;
    197.     //}
    198.  
    199.     /// <summary>
    200.     /// Convert webcamtexture into color[] to make them convertable
    201.     /// </summary>
    202.     /// <param name="_hostWebcam">Webcamtexture which should be send over Network</param>
    203.     /// <returns></returns>
    204.     Color32[] ConvertWebcamIntoColors32(WebCamTexture _hostWebcam)
    205.     {
    206.         data = _hostWebcam.GetPixels32();
    207.         Debug.Log("WebcamColors size in function: " + data.Length);
    208.         return data;
    209.     }
    210.  
    211.     /// <summary>
    212.     /// Convert color[] into byte[] for transfer over network
    213.     /// bytes will set to texture1bytes to make them accessable for server
    214.     /// </summary>
    215.     /// <param name="colors">Used Webcamtextur which is converted into a color[] from ConvertWebcamIntoColors32</param>
    216.     /// <returns></returns>
    217.     private byte[] Color32ArrayToByteArray(Color32[] colors)
    218.     {
    219.         byte[] bytes = new byte[colors.Length * 4];
    220.         for (int i = 0; i < bytes.Length / 4; i += 4)
    221.         {
    222.             bytes[i] = colors[i].r;
    223.             bytes[i + 1] = colors[i].g;
    224.             bytes[i + 2] = colors[i].b;
    225.             bytes[i + 3] = colors[i].a;
    226.         }
    227.  
    228.         texture1Bytes = bytes;
    229.         Debug.Log("texture1Bytes size in function Color32ArrayToByteArray: " + texture1Bytes.Length);
    230.         return texture1Bytes;
    231.     }
    232.  
    233.     /// <summary>
    234.     /// Look for selectedCam value
    235.     /// activate Cam if value > 0
    236.     /// deactivate if value lower / equals 0
    237.     /// set device
    238.     /// </summary>
    239.     void SetCam()
    240.     {
    241.  
    242.         Debug.Log("Device name: " + device.name);
    243.         if (selectedCam == 0)
    244.         {
    245.             device = WebCamTexture.devices[selectedCam];
    246.             if (webCamTexture == null && display1.texture != myTexture)
    247.                 display1.texture = myTexture;
    248.  
    249.             if (webCamTexture != null)
    250.             {
    251.                 Debug.Log("Disable cam.");
    252.                 Debug.Log("Clear webcamTexture.");
    253.                 display1.texture = myTexture;
    254.                 webCamTexture.Stop();
    255.                 webCamTexture = null;
    256.             }
    257.  
    258.             isCamSelected = false;
    259.         }
    260.  
    261.         if (selectedCam > 0)
    262.         {
    263.             Debug.Log("Display texture: " + display1.texture);
    264.             if (display1.texture == myTexture || display1.texture == null)
    265.             {
    266.                 device = WebCamTexture.devices[selectedCam - 1];
    267.                 Debug.Log("Activate Cam");
    268.                 webCamTexture = new WebCamTexture(device.name);
    269.                 display1.texture = webCamTexture;
    270.  
    271.                 webCamTexture.Play();
    272.             }
    273.             isCamSelected = true;
    274.         }
    275.     }
    276. }
    277.  
    278. [Code]
     
  2. SchChris

    SchChris

    Joined:
    May 11, 2017
    Posts:
    5
    @ TwoTen,
    i can't see your answer here, but in my mail account :)

    I tried this out but it doesn't work.. :(
     
  3. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    @SchChris
    You can't instantiate SyncListStruct basicly what's the error is telling you. It's not obvious from documentation, but you should treat SyncListStruct more like an interface.

    Instead of:
    Code (CSharp):
    1.   SyncListStruct<WebcamData> webcamDataList = new SyncListStruct<WebcamData>();
    Use:
    Code (CSharp):
    1. SyncListWebcamData webcamDataList = new SyncListWebcamData();
    Also, don't use SyncVar attributes on sync lists. It's pointless, since they automatically do what SyncVar does.
     
    Last edited: May 23, 2017