Skip to main content
1 of 2
tigrou
  • 3.3k
  • 1
  • 28
  • 44

How to clip what is outside scroll view when using a custom UI element?

I have created a custom UI element (that use CanvasRenderer).

Here is code :

Mesh mesh = new Mesh(); Rect drawArea = GetComponent<RectTransform>().rect; using (VertexHelper helper = new VertexHelper()) { helper.AddVert(new Vector3(drawArea.xMin, drawArea.yMin), Color.white, Vector2.zero); helper.AddVert(new Vector3(drawArea.xMin, drawArea.yMax), Color.white, Vector2.zero); helper.AddVert(new Vector3(drawArea.xMax, drawArea.yMax), Color.white, Vector2.zero); helper.AddVert(new Vector3(drawArea.xMax, drawArea.yMin), Color.white, Vector2.zero); helper.AddTriangle(0, 1, 2); helper.AddTriangle(2, 3, 0); helper.FillMesh(mesh); } var canvas = GetComponent<CanvasRenderer>(); canvas.SetMesh(mesh); canvas.SetMaterial(Material, null); 

It creates a simple rectangle that is same size as rect transform.

It works great. However if I put this inside a scroll view, everything that is outside scroll area / viewport is rendered (while it should be clipped).

The scrolling works as it should (rectangle get scrolled by changing position when scrollbar is used).

I have tried to call canvas.EnableRectClipping(...); but without success.

tigrou
  • 3.3k
  • 1
  • 28
  • 44