2017-09-29 15 views
0

나는 스크린 샷을 찍으려고 시도하고 바로 다음에 어떤 종류의 미리보기를 보여주기 위해 그것을 사용하고 어떤 때는 작동하고 어떤 때는 그렇지 않은지, 나는 현재 나는이 컴퓨터의 단결이없는없는 직장에서 그래서 나는 즉시 다시 작성하려고합니다 (이 일부 구문 여기에 실수가있을 수 있습니다)ScreenCapture.CaptureScreenshot을 사용하여 스크린 샷 캡처 및 저장

public GameObject screenshotPreview; 

public void TakeScreenshot() { 

     string imageName = "screenshot.png"; 

     // Take the screenshot 
     ScreenCapture.CaptureScreenshot (imageName); 

     // Read the data from the file 
     byte[] data = File.ReadAllBytes(Application.persistentDataPath + "/" + imageName); 

     // Create the texture 
     Texture2D screenshotTexture = new Texture2D(Screen.width, Screen.height); 

     // Load the image 
     screenshotTexture.LoadImage(data); 

     // Create a sprite 
     Sprite screenshotSprite = Sprite.Create (screenshotTexture, new Rect(0, 0, Screen.width, Screen.height), new Vector2(0.5f, 0.5f)); 

     // Set the sprite to the screenshotPreview 
     screenshotPreview.GetComponent<Image>().sprite = screenshotSprite; 

} 

지금까지 내가 읽은대로, ScreenCapture.CaptureScreenshot은 비동기 적이 지 않으므로 데이터를로드하려고 시도하기 전에 이미지를 작성해야합니다.하지만 문제는 이전에 말한 것처럼 작동하지 않으며 빨간색 물음표가있는 8x8 텍스처가로드됩니다. , 분명히로드되지 않은 텍스처이지만 파일 거기에 있어야했기 때문에 왜 제대로로드되지 않는지 이해할 수 없습니다.

내가 시도한 또 다른 한 가지 (역겨운 것이지만 지치고 아이디어가 부족함)는 업데이트 방법을 사용하여 잠시 기다린 다음 데이터를로드하고 텍스처를 만들고 스프라이트를 만들고 표시하지만 이전보다 덜 자주 실패하지만 여전히 실패합니다. 파일이 작성된 경우에도 작성된 파일이 완성되지 않았다고 믿을 수 있습니다. 이 문제를 해결하려면? 모든 조언을 부탁드립니다.

추가 정보로서이 프로젝트는 iOS 장치에서 실행되고 있습니다.

답변

0

ScreenCapture.CaptureScreenshot 기능에는 많은 문제가있는 것으로 알려져 있습니다. Here은 그 중 하나입니다.

안드로이드에이 기능을 즉시 반환 : 여기

doc에서 인용 한 것입니다. 결과 스크린 샷 은 나중에 사용할 수 있습니다.

iOS 동작은 문서화되어 있지 않지만 동작이 iOS에서 동일하다고 가정 할 수 있습니다. 스크린 샷을 읽은 후 몇 프레임 기다렸다가 읽기 /로드를 시도합니다.

public IEnumerator TakeScreenshot() 
{ 

    string imageName = "screenshot.png"; 

    // Take the screenshot 
    ScreenCapture.CaptureScreenshot(imageName); 

    //Wait for 4 frames 
    for (int i = 0; i < 5; i++) 
    { 
     yield return null; 
    } 

    // Read the data from the file 
    byte[] data = File.ReadAllBytes(Application.persistentDataPath + "/" + imageName); 

    // Create the texture 
    Texture2D screenshotTexture = new Texture2D(Screen.width, Screen.height); 

    // Load the image 
    screenshotTexture.LoadImage(data); 

    // Create a sprite 
    Sprite screenshotSprite = Sprite.Create(screenshotTexture, new Rect(0, 0, Screen.width, Screen.height), new Vector2(0.5f, 0.5f)); 

    // Set the sprite to the screenshotPreview 
    screenshotPreview.GetComponent<Image>().sprite = screenshotSprite; 

} 

이 기능을 호출하려면 StartCoroutine(TakeScreenshot());을 사용해야합니다. 문제가 해결되지 않은 경우


은에서 모두이 기능을 사용하지 마십시오.

IEnumerator captureScreenshot() 
{ 
    yield return new WaitForEndOfFrame(); 

    string path = Application.persistentDataPath + "Screenshots/" 
      + "_" + screenshotCount + "_" + Screen.width + "X" + Screen.height + "" + ".png"; 

    Texture2D screenImage = new Texture2D(Screen.width, Screen.height); 
    //Get Image from screen 
    screenImage.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0); 
    screenImage.Apply(); 
    //Convert to png 
    byte[] imageBytes = screenImage.EncodeToPNG(); 

    //Save image to file 
    System.IO.File.WriteAllBytes(path, imageBytes); 
} 
+0

난 그냥 [여기]를 읽고 있었다 (https://developer.vuforia.com/forum/faq/unity-how-can-i-capture- : 여기 유니티에서 화면을 캡처하고 저장하는 또 다른 방법입니다 스크린 샷) 그 방법에 대해하지만 당신이 그것을 제안했다는 사실은 나에 대해 기분이 나아지게하고 나는 내일 한 번 그에게 기회를 줄 것이다. 고마워! –

0

비동기가 아닌 문서에는 아무 것도 없습니다. 사실 Android의 경우 (올바르게 읽는 경우) 비동기라고 명시되어 있습니다.

즉, 파일을 찾을 수없는 동안 나는 멈추려고합니다. 코 루틴에서 던져라. (! file.found) yield? 또한 파일이 나타나기 전에 (몇 초 또는 프레임) 소요되는 시간을 확인하기 위해 일부 디버그 검사를 던져 볼 수도 있습니다.