Wrong Coloring (Sometimes) when capturing "Screen"-shot
I have a modular ShipEditor in my game where you are able to piece your ship together to then later use it, when you start the scene where you edit ships you get a Panel with Previews of all your creations, these creations are the typicall "Create a camera/rendertexture, camera.render(), Texture.readpixels(), destroy camera" and the output would be something like this:
but for some reason sometimes (!) it looks some sort of this: the colors seem to be different everytime.
I call this in the first frame of the Playmode, that might be a part of the problem, here is the code:
private RenderTexture GetPreview(Ship ship)
{
RenderTexture preview = new RenderTexture(128, 128, 1);
preview.format = RenderTextureFormat.ARGBFloat;
//build ship
GameObject chassis = Instantiate(AllChassis[ship.data.chassisReference].shipObject, transform);
bool armourIsCenter = false;
if (ship.data.selectedArmour != -1)
{
armourIsCenter = true;
Instantiate(AllChassis[ship.data.chassisReference].chassisArmors[ship.data.selectedArmour].armorObject, AllChassis[ship.data.chassisReference].armorSlot.position, Quaternion.Euler(AllChassis[ship.data.chassisReference].armorSlot.direction), chassis.transform);
}
for (int i = 0; i < ship.data.filledModuleSlots.Length; i++)
{
if (ship.data.filledModuleSlots[i] == -1)
continue;
Instantiate(AllModules[ship.data.filledModuleSlots[i]].model, AllChassis[ship.data.chassisReference].moduleSlots[i].position, Quaternion.Euler(AllChassis[ship.data.chassisReference].moduleSlots[i].direction), chassis.transform);
}
for (int i = 0; i < chassis.transform.childCount; i++)
{
chassis.transform.GetChild(i).gameObject.layer = 15;
}
//position camera
previewScreenshotCamera.transform.position = (armourIsCenter ? AllChassis[ship.data.chassisReference].armorSlot.position : Vector3.zero) + previewScreenshotCamera.transform.forward * -20;
//screen shot
previewScreenshotCamera.gameObject.SetActive(true);
previewScreenshotCamera.targetTexture = preview;
previewScreenshotCamera.Render();
previewScreenshotCamera.gameObject.SetActive(false);
previewScreenshotCamera.targetTexture = null;
DestroyImmediate(chassis); //needs to be immediate or a ghost of this will appear in next preview
return preview;
}
Answer by Santifocus · Sep 17, 2019 at 01:19 PM
Forgot to post the fix that i found out a while ago, for anyone who might encounter this problem. Apparently Unity does some stuff with the camera in the first frame-Awake, therefore you should only do screenshots in Start or later. Could be considered a bug not sure if it will be fixed tho.
Your answer
Follow this Question
Related Questions
ScreenCapture.CaptureScreenshotAsTexture() is making a milky white tinted screenshot. 1 Answer
Trying to Take a Snapshot of Only a Portion of the Screen In-Game 0 Answers
HDRP: Camera solid color background 0 Answers
how can i store a screenshot into android mobile? 0 Answers
Rendering Camera to PNG produces completely grey image. 1 Answer