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 and made a instance of it.
On running i get following Error Message:
UNetWeaver error: SyncListStruct member variable [UnityEngine.Networking.SyncListStruct`1 SendWebcamTextureToAnotherTexture::webcamDataList] must use a dervied class, like “class MySyncList : SyncListStruct {}”.
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:
// send WebcamTexture to another texture2d
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
// Camera Support http://answers.unity3d.com/questions/1218754/optimize-rpc-streamed-video-as-byte-array-frame-by.html
public struct WebcamData
{
public byte red;
public byte green;
public byte blue;
public byte alpha;
public WebcamData(byte _red, byte _green, byte _blue, byte _alpha)
{
red = _red;
green = _green;
blue = _blue;
alpha = _alpha;
}
}
public class SyncListWebcamData : SyncListStruct<WebcamData>
{
}
public class SendWebcamTextureToAnotherTexture : NetworkBehaviour
{
//public GameObject webcamHolderPrefab;
public Texture2D myTexture;
public Texture2D texture2;
public WebCamTexture webCamTexture;
public RawImage display1;
public RawImage display2;
Color32[] data;
private byte[] texture1Bytes;
private byte[] texture2Bytes;
SyncListStruct<WebcamData> webcamDataList = new SyncListStruct<WebcamData>();
WebCamDevice device;
private NetworkView nView;
bool isCamSelected;
bool isCamConnected;
// number with connected Devices
int selectedCam = 0;
public int SelectedCam
{
set
{
if (value <= 0)
{
selectedCam = 0;
Debug.Log("selectedCam: " + selectedCam);
}
if (value > 0)
{
Debug.Log("Value: " + value);
selectedCam = WebCamTexture.devices.Length;
Debug.Log("selectedCam: " + selectedCam);
}
}
get
{
return selectedCam;
}
}
[SyncVar]
public SyncListWebcamData m_camDatas = new SyncListWebcamData();
void Start()
{
isCamSelected = false;
isCamConnected = false;
nView = display1.GetComponent<NetworkView>();
selectedCam = 0;
#region Check for devices
if (WebCamTexture.devices.Length > 0)
{
Debug.Log("Get count of Devices: " + WebCamTexture.devices.Length);
selectedCam += 1;
// Check if ther devices
//currentCamIndex %= WebCamTexture.devices.Length;
Debug.Log("selectedCam: " + selectedCam);
}
// if are more than one webcamdevices connected
// clear the webcamTexture and stop receiving information from the display
if (webCamTexture != null)
{
Debug.Log("Clear webcamTexture.");
display1.texture = null;
webCamTexture.Stop();
webCamTexture = null;
}
#endregion
webCamTexture = new WebCamTexture();
texture2 = new Texture2D(webCamTexture.width, webCamTexture.height);
//device = WebCamTexture.devices[selectedCam];
//Debug.Log("Devicename: " + device.name);
}
void Update()
{
#region Check if a Cam selected
if (!isCamConnected)
{
SetCam();
isCamConnected = true;
}
if (isCamSelected)
{
data = new Color32[webCamTexture.width + webCamTexture.height];
Debug.Log("data length: " + data.Length);
myTexture = new Texture2D(webCamTexture.width, webCamTexture.height, TextureFormat.RGB24, false);
}
#endregion
if (isServer)
{
#region Input
if (Input.GetKeyDown(KeyCode.UpArrow))
{
SelectedCam++;
SetCam();
Debug.Log("Key Up: " + SelectedCam);
}
if (Input.GetKeyDown(KeyCode.DownArrow))
{
SelectedCam--;
SetCam();
Debug.Log("Key down: " + SelectedCam);
}
#endregion
//StartCoroutine(GetPixels());
//setWebcamToNewTextur(webCamTexture);
ConvertWebcamIntoColors32(webCamTexture);
Color32ArrayToByteArray(data);
Debug.Log("texture1Bytes size after function Color32ArrayToByteArray: " + texture1Bytes.Length);
for (int i = 0; i < texture1Bytes.Length; i+= 4)
{
WebcamData webCamData = new WebcamData(texture1Bytes[i], texture1Bytes[i + 1], texture1Bytes[i + 2], texture1Bytes[i + 3]);
webcamDataList.Add(webCamData);
}
Debug.Log("Webcamstructs size: " + webcamDataList.Count);
}
else
{
//texture2.LoadImage(texture2Bytes);
}
}
/// <summary>
/// Only local
/// Transfer the Webcamtexture to another Texture
/// Texture2 receive the Webcamtexturedata with _hostWebcam.GetPixels()
/// and set it with texture2.SetPixels()
/// textur2 will be set to display2.texture
/// </summary>
/// <param name="_hostWebcam"></param>
//void setWebcamToNewTextur(WebCamTexture _hostWebcam)
//{
// texture2 = new Texture2D(_hostWebcam.width, _hostWebcam.height);
// texture2.SetPixels(_hostWebcam.GetPixels());
// texture2.Apply();
// display2.texture = texture2;
//}
/// <summary>
/// Convert webcamtexture into color[] to make them convertable
/// </summary>
/// <param name="_hostWebcam">Webcamtexture which should be send over Network</param>
/// <returns></returns>
Color32[] ConvertWebcamIntoColors32(WebCamTexture _hostWebcam)
{
data = _hostWebcam.GetPixels32();
Debug.Log("WebcamColors size in function: " + data.Length);
return data;
}
/// <summary>
/// Convert color[] into byte[] for transfer over network
/// bytes will set to texture1bytes to make them accessable for server
/// </summary>
/// <param name="colors">Used Webcamtextur which is converted into a color[] from ConvertWebcamIntoColors32</param>
/// <returns></returns>
private byte[] Color32ArrayToByteArray(Color32[] colors)
{
byte[] bytes = new byte[colors.Length * 4];
for (int i = 0; i < bytes.Length / 4; i += 4)
{
bytes[i] = colors[i].r;
bytes[i + 1] = colors[i].g;
bytes[i + 2] = colors[i].b;
bytes[i + 3] = colors[i].a;
}
texture1Bytes = bytes;
Debug.Log("texture1Bytes size in function Color32ArrayToByteArray: " + texture1Bytes.Length);
return texture1Bytes;
}
/// <summary>
/// Look for selectedCam value
/// activate Cam if value > 0
/// deactivate if value lower / equals 0
/// set device
/// </summary>
void SetCam()
{
Debug.Log("Device name: " + device.name);
if (selectedCam == 0)
{
device = WebCamTexture.devices[selectedCam];
if (webCamTexture == null && display1.texture != myTexture)
display1.texture = myTexture;
if (webCamTexture != null)
{
Debug.Log("Disable cam.");
Debug.Log("Clear webcamTexture.");
display1.texture = myTexture;
webCamTexture.Stop();
webCamTexture = null;
}
isCamSelected = false;
}
if (selectedCam > 0)
{
Debug.Log("Display texture: " + display1.texture);
if (display1.texture == myTexture || display1.texture == null)
{
device = WebCamTexture.devices[selectedCam - 1];
Debug.Log("Activate Cam");
webCamTexture = new WebCamTexture(device.name);
display1.texture = webCamTexture;
webCamTexture.Play();
}
isCamSelected = true;
}
}
}
[Code]