Unity相机渲染

时间:2016-06-01 11:24:57

标签: unity3d

我的相机有问题,我想拍一张截图,但是我收到了这个错误:

  

Flare渲染器更新未找到UnityEngine.Camera:Render()   c__Iterator4:MoveNext()(at   资产/脚本/ ActionCam.cs:43)

我的代码:

public IEnumerator TakeScreenshot() {
        yield return new WaitForEndOfFrame();

        Camera camOV = _Camera;

        RenderTexture currentRT = RenderTexture.active;

        RenderTexture.active = camOV.targetTexture;
        camOV.Render(); // here is the problem...
        Texture2D imageOverview = new Texture2D(camOV.targetTexture.width, camOV.targetTexture.height, TextureFormat.RGB24, false);
        imageOverview.ReadPixels(new Rect(0, 0, camOV.targetTexture.width, camOV.targetTexture.height), 0, 0);
        imageOverview.Apply();

        RenderTexture.active = currentRT;

        byte[] bytes = imageOverview.EncodeToPNG();
        string path = ScreenShotName(Convert.ToInt32(imageOverview.width), Convert.ToInt32(imageOverview.height));

        System.IO.File.WriteAllBytes(path, bytes);
    }

有我的相机设置:

enter image description here

如果我停用“Flare Layer”,我不会收到此错误,但我的屏幕截图或多或少是空的,只有天空盒:

enter image description here

任何想法?

1 个答案:

答案 0 :(得分:0)

我统一使用3种类型的屏幕截图:

  1. Application.CaptureScreenshot(ScreenSgotFile):不再使用,因为我更喜欢将屏幕截图存储在内存中的方法。 (我不需要该文件,因为它附加到MailMessage并由SmtpClient发送)

  2. OnPostRender(必须连接到相机):

  3.     void OnPostRender(){  
            Texture2D ScreenShot=new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);  
            RenderTexture.active=null;  
            ScreenShot.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);  
            ScreenShot.Apply();  
            byte[] PngData=ScreenShot.EncodeToPNG();  
            PngStream = new MemoryStream(PngData);  
            //File.WriteAllBytes(FileShot, PngData);  
    
            // Preview on a sprite  
            Sprite Spr=Sprite.Create(ScreenShot, new Rect(0, 0, ScreenShot.width, ScreenShot.height),Vector2.zero, 100);  
            Preview.sprite=Spr;  
            Preview.gameObject.SetActive(true);  
        }  
    
    1. 相同的代码,但在框架的末尾:
    2.     void Update(){  
              ...  
              StartCoroutine(ScreenShotAtEndOfFrame());  
          }  
      
          public IEnumerator ScreenShotAtEndOfFrame(){  
              yield return new WaitForEndOfFrame();  
      
              Texture2D ScreenShot=new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);  
              RenderTexture.active=null;  `enter code here`
              ScreenShot.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);  
              ScreenShot.Apply();  
              byte[] PngData=ScreenShot.EncodeToPNG();  
              PngStream = new MemoryStream(PngData);  
              //File.WriteAllBytes(FileShot, PngData);  
      
              // Preview on a sprite  
              Sprite Spr=Sprite.Create(ScreenShot, new Rect(0, 0, ScreenShot.width, ScreenShot.height),Vector2.zero, 100);  
              Preview.sprite=Spr;  
              Preview.gameObject.SetActive(true);  
          }