here is my code i am again and again getting cast exception …please help me in this code …i am trying to capture an image and then show it …i have used gameobject cube on which i am streaming the camera feed .here is the code
using UnityEngine;
using System.Collections;
public class Selfie : MonoBehaviour {
public string deviceName;
WebCamTexture wct;
// Use this for initialization
void Start ()
{
WebCamDevice[] devices = WebCamTexture.devices;
deviceName = devices [0].name;
wct = new WebCamTexture (deviceName, 400, 300, 12);
renderer.material.mainTexture = wct;
wct.Play ();
}
//for photo variables
public Texture2D heightmap;
public Vector3 size= new Vector3(100,10,100);
void OnGUI()
{
if (GUI.Button (new Rect (10, 70, 50, 30), "click"))
TakeSnapshot ();
}
//for saving to the _savepath
private string _SavePath= "D:/WebcamSnaps/" ;
int _CaptureCounter = 0;
void TakeSnapshot()
{
Texture2D snap = new Texture2D (wct.width, wct.height);
snap.SetPixels (wct.GetPixels ());
snap.Apply ();
//byte[] bytes = snap.EncodeToPNG ();
System.IO.File.WriteAllBytes(_SavePath + _CaptureCounter.ToString() + ".png", snap.EncodeToPNG());
++_CaptureCounter;
WWW www = new WWW ("file://D:/WebcamSnaps/0.png");
while (!www.isDone)
www.LoadImageIntoTexture((Texture2D)renderer.material.mainTexture) ;
}
}