- Home /
Can not retrive image form device to the plane at runtime.
Hi.... Below is my code. i was successful in saving an image but can not retrieve back and apply to the plane. Please help??
using UnityEngine; using System.Collections;
public class LoadTexture : MonoBehaviour {
public string deviceName;
WebCamTexture wct;
public static string path = "";
private Texture2D m_Texture;
public static bool TakePic;
public GameObject PlaneObjet;
bool isplaneactive = false;
// Use this for initialization
static string imagename;
void Start ()
{
WebCamDevice[] devices = WebCamTexture.devices;
deviceName = devices[0].name;
wct = new WebCamTexture(deviceName,1280,800,30);
renderer.material.mainTexture = wct;
wct.Play();
TakePic = false;
imagename ="img.png";
}
void OnGUI()
{
if (GUI.Button(new Rect(10,10,100,30), "Click"))
{
Application.CaptureScreenshot(imagename);
path = Application.persistentDataPath;
isplaneactive = true;
GUI.Label(new Rect(0,0,200,50), path);
}
if(isplaneactive)
{
if(GUI.Button(new Rect(0,0,100,100), "Paste"))
{
this.gameObject.SetActive(false);
PlaneObjet.SetActive(true);
Pasteimage();
}
}
}
IEnumerator TakeScreenshotAndLoadItIntoTexture(string filename)
{
//Application.CaptureScreenshot(filename);
WWW www = new WWW(filename);
GUI.Label(new Rect(0,0,200,50), "image path = "+path);
Debug.Log ( filename );
Debug.Log (www.error.ToString() );
yield return www;
Texture2D texTmp = new Texture2D(1024, 1024, TextureFormat.DXT1, false);
www.LoadImageIntoTexture(texTmp);
m_Texture = texTmp;
PlaneObjet.renderer.material.SetTexture("_MainTex", m_Texture);
}
// Use this for initialization
void Pasteimage()
{
//StartCoroutine(WaitForRequest(www));
path = path+"/"+imagename;
TakeScreenshotAndLoadItIntoTexture(path);
}
}
image is saved but not able to apply on plane......... any advices?? can i know where i'm doing wrong??
If you are just trying to capture the screenshot to use in the game (i.e. you don't need the image on disk), you can use ReadPixels() ins$$anonymous$$d:
http://answers.unity3d.com/questions/649079/how-to-get-a-screenshot-in-game.html
for robertbu's comment. But if you need to save image locally, then:
TakeScreenshotAndLoadItIntoTexture is a coroutine, but you call it as a normal method (i.e. without StartCoroutine)
you should log www.error after yield
AFAI$$anonymous$$ loading local file using www requires file:// prefix in path
Your answer
Follow this Question
Related Questions
Should I load all the database items on my android device? 1 Answer
How to use native android file-open-dialog in Unity? 1 Answer
Add BoxCollider2d and Rigidbody2D with script 0 Answers
Multiple Cars not working 1 Answer
confusion with sprites. 0 Answers