- Home /
Question by
elionakopi22 · Jun 25, 2020 at 02:16 PM ·
texturetexture2dreadpixels
Texture2D.ReadPixels working on Editor, but not on Build?
Hello everyone! I have a question that is eating up a lot of my time now. I've tried a lot of things, but nothing seems to get Texture2D.ReadPixels to work on Build. It works perfectly on Editor. Do you have any idea what might be causing this error?
public class Completion : MonoBehaviour
{
public static float completion = 11.2f;
public static float percentage;
public float percent;
public TextMeshProUGUI comp;
public GameObject dialog, me;
void Start()
{
percentage = percent;
comp.text = completion.ToString() + "%";
theShader = Shader.Find("Mobile/Bumped Diffuse");
}
void Update()
{
comp.text = completion.ToString() + "%";
if (completion >= 99.9f)
{
comp.text = "WellDone!";
StartCoroutine(Finish());
if(sth==false)
{
StartCoroutine(Break());
}
StartCoroutine(End());
}
}
IEnumerator Finish()
{
yield return new WaitForSeconds(2);
dialog.SetActive(true);
}
bool grab;
Shader theShader;
public GameObject ecrane;
public GameObject moi;
public GameObject bureak;
Renderer[] m_Display;
bool sth=false;
private void OnPostRender()
{
if (grab)
{
Texture2D texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0, false);
texture.Apply();
if (m_Display != null)
foreach(var r in m_Display)
{
r.material.mainTexture = texture;
r.material.shader = theShader;
}
grab = false;
}
}
IEnumerator Break()
{
sth=true;
yield return new WaitForSeconds(0.1f);
grab = true;
m_Display = ecrane.GetComponentsInChildren<Renderer>();
}
IEnumerator End()
{
yield return new WaitForSeconds(6);
bureak.SetActive(true);
moi.SetActive(false);
}
}
The whole idea of this is to grab a screenshot 0.1f after the game is 100% complete, and apply the texture to a .blend file with many children. Note that this works exactly as expected on the Editor and not on the Build. Is there an error on the code? Please help! Also if it helps, the build is windowed at 1280x720 resolution, if this is relevant.
Comment