- Home /
SreenShot, Color become a mess
Hello guys, I am a new in unity. I hope this question is not too silly for you. I want to make a screenshot just before I set the "TimeScale" to 0. So I choosed to use "Texture2D.ReadPixels" and "Texture2D.Apply" to returned and saved the "Texture2D". And then I used "GUI.DrawTexture" to draw the thing I had just saved. The code is work. No compile error at all. But here comes the problem. the color of the thing I had drew becomes a mess. The link at the bootom will show how terrifying it is. I googled as many as I could, but it still confuse me. Please Help!
https://drive.google.com/file/d/1N4Cgv4AOFGuiMCTwqw5abl2Y_XWT2SEL/view?usp=sharing
There are countless things that could be wrong in your code. First of all you can not call ReadPixels whenever you want. If you read from screen you should do that either in OnPostRender or in a coroutine after WaitForEndOfFrame.. Next you said you use GUI.DrawTexture. This can only be used in OnGUI. It's not clear how you setup your Texture2D. Does it have the right size? What texture format do you use?
Answer by Han-Wen · Jul 11, 2019 at 01:20 AM
@Bunny83 Sorry, I didn't express clearly enough, here are my code. which script is attached to my camera.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Screenshot : MonoBehaviour
{
GameObject Ray;
Rect rect;
bool shot = false;
Texture2D _image_tem;
private void Start()
{
rect = new Rect(Screen.width * 0f, Screen.height * 0f, Screen.width * 0.8f, Screen.height * 0.8f);
Ray = GameObject.FindGameObjectWithTag("Player");
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.J) && Character_CheckObject.isGameStop)
{
shot = false;
}
}
private void OnPostRender()
{
if (Ray.GetComponent<Character_CheckObject>().GetShot())
{
_image_tem = CaptureScreenshot(rect);
shot = true;
Ray.GetComponent<Character_CheckObject>().SetShot_False();
}
}
private void OnGUI()
{
if (shot)
{
GUI.DrawTexture(new Rect(10, 10, 400, 300), _image_tem);
Debug.Log(123);
}
}
public Texture2D CaptureScreenshot(Rect rect)
{
Texture2D screenshot = new Texture2D((int)rect.width,(int)rect.height,TextureFormat.RGB24,false);
screenshot.ReadPixels(rect, 0, 0);
screenshot.Apply();
return screenshot;
}
}