- Home /
why texture2d sprite rendering not showing in game
I create a texture2d from camera data then render it on a game object using sprite renderer. The image shows in the Scene view but does not display in game. Any ideas?
Heres the code
Texture2D tex = new Texture2D(screenWidth, screenHeight, l.transparent ? TextureFormat.ARGB32 : TextureFormat.RGB24, false);
tex.ReadPixels(new Rect(0, 0, screenWidth, screenHeight), 0, 0);
tex.Apply();
//
float renWidth = Screen.width;
float renHeight = Screen.height;
// from texture
//menu.gameMenu.mySpriteRenderer.sprite = UnityEngine.Sprite.Create(tex,
// new Rect(0,0,renWidth,renHeight), new Vector2(0.5f, 0.5f));
// Encode texture into PNG
byte[] bytes = tex.EncodeToPNG();
DestroyImmediate(tex);
//
Texture2D texture = new Texture2D(2, 2);
texture.LoadImage(bytes);
menu.gameMenu.mySpriteRenderer.sprite = UnityEngine.Sprite.Create(texture, new Rect(0,0,renWidth,renHeight), new Vector2(0.5f, 0.5f));
Comment