In HDRP using Screen Space - Overlay Canvas, How can I edit UI.Image vertices at runtime?

In HDRP using Screen Space - Overlay Canvas, How can I edit UI.Image vertices at runtime?

I am attempting to create a minimap in HDRP by using a Image in a screen space - overlay canvas, I would like to manipulate the vertices of the UI.Image mesh at runtime. Most examples i have come across utilise a OnPopulateMesh(VertexHelper vh) override.

Problem with the below, is as soon as I call the CanvasRenderer.SetMesh() method, unity crashes. The goal is to skew and rotate the Image vertices at runtime to simulate a minimap compass.

Any advice is greatly appreciated.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Test : Image
{
    Mesh m;
    CanvasRenderer canvasEle;
    void Update()
    {
	canvasEle = transform.GetComponent<CanvasRenderer>();
        m = canvasEle.GetMesh();
        if (m != null)
        {
            Vector3[] verts = new Vector3[m.vertices.Length];
            for(int i = 0; i < m.vertices.Length; i++)
            {
                verts[i] = m.vertices[i];
            }
            verts[0].x = (m.vertices[0].x + 50);
            m.vertices = verts;
            m.RecalculateBounds();
        }
        canvasEle.SetMesh(m);
    }
}

Haven’t tested this, but I’m theorizing the problem may be that the mesh in the CanvasRenderer is an internal Unity resource you should not be attempting to modify.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Test : Image
{
    Mesh m;
    CanvasRenderer canvasEle;
	
	void Start()
	{
	   canvasEle = this.GetComponent<CanvasRenderer>();
	   // create a deep clone of the CanvasRenderer mesh as it is likely an internal unity resource which should not be directly edited
	   m = new Mesh
	   {
		   name = $"{m.name}_{this.GetInstanceID()}_",
		   vertices = m.vertices.ToArray(),
		   uv = mesh.uv.ToArray(),
		   triangles = mesh.triangles.ToArray(),
		   colors = mesh.colors.ToArray()
	   };
	}
	
    void Update()
    {
        if (m != null)
        {
            Vector3[] verts = new Vector3[m.vertices.Length];
            for(int i = 0; i < m.vertices.Length; i++)
            {
                verts[i] = m.vertices[i];
            }
            verts[0].x = (m.vertices[0].x + 50);
            m.vertices = verts;
            m.RecalculateBounds();
        }
        canvasEle.SetMesh(m);
    }
}