2017-04-07 26 views
1

写真を撮影し、記念碑などの異なる背景を置くプログラムを作成しようとしています。これまでのところ、私はそれは常にすべての黒終わるので、私はスクリーンショットや写真を取ることができない、私は写真をユニティで撮る#

webcamTexture = new WebCamTexture(); 
rawImage.texture = webcamTexture; 
rawImage.material.mainTexture = webcamTexture; 
webcamTexture.Play(); 
Texture2D PhotoTaken = new Texture2D (webcamTexture.width, webcamTexture.height); 
PhotoTaken.SetPixels (webcamTexture.GetPixels()); 
PhotoTaken.Apply(); 

しかし、このコードでプロジェクトを開始するときに、カメラを向けることができました。私は別のコードを試しましたが、何も動作していません。誰かが助けてくれますか?おかげで、いくつかの試行後

EDIT

が、これは私が持っているコードです:

WebCamTexture webcamTexture; 
public RawImage rawImage; 


void Start() { 
    webcamTexture = new WebCamTexture(); 
    rawImage.texture = webcamTexture; 
    rawImage.material.mainTexture = webcamTexture; 
    webcamTexture.Play(); 

    RenderTexture texture= new RenderTexture(webcamTexture.width, webcamTexture.height,0); 
    Graphics.Blit(webcamTexture, texture); 

    Button btn = yourButton.GetComponent<Button>(); 
    btn.onClick.AddListener(OnClick); 

} 

public IEnumerator Coroutine(){ 

    yield return new WaitForEndOfFrame(); 
} 

public void OnClick() { 

    var width = 767; 
    var height = 575; 

    Texture2D texture = new Texture2D(width, height); 
    texture.ReadPixels(new Rect(0, 0, width, height), 0, 0); 
    texture.Apply(); 

    // Encode texture into PNG 
    var bytes = texture.EncodeToPNG(); 
    //Destroy(texture); 


    File.WriteAllBytes (Application.dataPath + "/../SavedScreen.png", bytes); 

    } 

とスクリーンショットが撮影されたこの次のコードで、それは全部の写真をとり、そしてません画面のちょうど少し。あなたは、画面の特定の部分にスクリーンショットを取るためにUnity

Application.CaptureScreenshot("Screenshot.png"); 

Reference

EDIT 1
に、このようなスクリーンショットを撮ることができ

void Start() 
{ 
    // Set the playback framerate! 
    // (real time doesn't influence time anymore) 
    Time.captureFramerate = frameRate; 

    // Find a folder that doesn't exist yet by appending numbers! 
    realFolder = folder; 
    int count = 1; 
    while (System.IO.Directory.Exists(realFolder)) 
    { 
     realFolder = folder + count; 
     count++; 
    } 
    // Create the folder 
    System.IO.Directory.CreateDirectory(realFolder); 
} 

void Update() 
{ 
    // name is "realFolder/shot 0005.png" 
    var name = string.Format("{0}/shot {1:D04}.png", realFolder, Time.frameCount); 

    // Capture the screenshot 
    Application.CaptureScreenshot(name, sizeMultiplier); 
} 

}

答えて

2

次のスクリプトを使用します。

var width = 400; 
var height = 300; 
var startX = 200; 
var startY = 100; 
var tex = new Texture2D (width, height, TextureFormat.RGB24, false); 

tex.ReadPixels (Rect(startX, startY, width, height), 0, 0); 
tex.Apply(); 

// Encode texture into PNG 
var bytes = tex.EncodeToPNG(); 
Destroy(tex); 

File.WriteAllBytes(Application.dataPath + "/../SavedScreen.png", bytes); 

Reference

+0

私は画面全体のスクリーンショットに表示したくない場合は?ちょうど1つの部分 – Dmgm

+0

私が本当にばかばかしいように見えたら私はすみませますが、私は本当にこれに新しいです。しかし、それは動作しません。 Rectの部分にエラーがあり、たくさんのOSが試行した後、私は何をすべきか分からない。 – Dmgm

+1

質問を試したコードで更新できますか? – Hristo

関連する問題