r/Unity2D • u/Tall-Window-2386 • Apr 30 '23
having issues with creating a sprite from a camera view
idek anymore, I was trying to create a sprite based on a camer view with this code:
public Camera camera;
int height = 1024;
int width = 1024;
int depth = 24;
//method to render from camera
public Sprite CaptureScreen()
{
RenderTexture renderTexture = new RenderTexture(width, height, depth);
Rect rect = new Rect(0,0,width,height);
Texture2D texture = new Texture2D(width, height, TextureFormat.RGBA32, false);
camera.targetTexture = renderTexture;
camera.Render();
RenderTexture currentRenderTexture = RenderTexture.active;
RenderTexture.active = renderTexture;
texture.ReadPixels(rect, 0, 0);
texture.Apply();
camera.targetTexture = null;
RenderTexture.active = currentRenderTexture;
Destroy(renderTexture);
Sprite sprite = Sprite.Create(texture, rect, Vector2.zero);
return sprite;
}
if im being honest i copied this from an article, but im getting the error:
Assets\scripts\windowCode.cs(7,1): error CS0106: The modifier 'public' is not valid for this item
Duplicates
UnityHelp • u/Tall-Window-2386 • Apr 30 '23