I try to find the area of the UI images that are seen by the camera in game view. My thoughts is to color the background in 1 color and calculate the area occupied by other UI elements rendered in front of background, by subtracting the background total area from the overall camera view area. Any ideas how can I do this?
Asked
Active
Viewed 470 times
1
-
Have a look at Unity's `ComputeShader`. You can aggregate pretty much anything with superb performance. If you don't need the speed, just read the pixels on the CPU from the `Texture2D` interface: https://docs.unity3d.com/ScriptReference/Texture2D.ReadPixels.html – One Full Time Equivalent Jul 19 '22 at 13:37
-
To answer this we'd need to know if the canvas is screen space, overlay or world space. Also, if screen or world space do you want the area in world space or pixels? – Absinthe Jul 19 '22 at 13:39
-
Thanks for your replies. I found that I can work out the images pixel colors using Texture2D.GetPixels. This however does not take into considereation the UI overlay. I use screen space canvas. Will try to do this with overlay. I do not care whether the area is pixels or world space right now. I will check both and decide which one to use later on – Tim M Jul 19 '22 at 14:43
-
Why not simply use one Camera for the Background and another one for only rendering the UI via Layers? The URP Camera stack makes this actually not even expensive ... Or since your canvas seems to be screenspace overlay (=> pixel space => world positions == pixel positions) you can simply use [`RectTransform.GetWorldCorners`](https://docs.unity3d.com/ScriptReference/RectTransform.GetWorldCorners.html) ;) And evtl clamp them between (0,0) - (Screen.width, Screen.height) – derHugo Jul 19 '22 at 15:54
-
Note though that this also takes rotated corners ... if you want non-rotated rects anyway you could also simply use the Bounds – derHugo Jul 19 '22 at 15:58