Guest User

UIMeshRenderer.cs

a guest
Mar 28th, 2026
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.78 KB | None | 0 0
  1. //Custom UI Meshes script by @MinionsArt
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. [ExecuteAlways]
  5. [RequireComponent(typeof(CanvasRenderer))]
  6. public class UIMeshRenderer : MaskableGraphic
  7. {
  8.     [SerializeField]
  9.     Mesh mesh;
  10.     [SerializeField]
  11.     bool preserveAspect;
  12.  
  13.     protected override void OnEnable()
  14.     {
  15.         base.OnEnable();
  16.         SetVerticesDirty();
  17.         SetMaterialDirty();
  18.     }
  19.  
  20.     protected override void OnRectTransformDimensionsChange()
  21.     {
  22.         base.OnRectTransformDimensionsChange();
  23.         SetVerticesDirty();
  24.         SetMaterialDirty();
  25.     }
  26.  
  27.     protected override void OnPopulateMesh(VertexHelper vh)
  28.     {
  29.         vh.Clear();
  30.         if (mesh == null) return;
  31.  
  32.         Rect rect = rectTransform.rect;
  33.  
  34.         // handle preserving aspect
  35.         if (preserveAspect && mesh.bounds.extents.sqrMagnitude > 0f)
  36.         {
  37.             float meshRatio = mesh.bounds.extents.x / mesh.bounds.extents.y;
  38.             float rectRatio = rect.width / rect.height;
  39.  
  40.             if (meshRatio > rectRatio)
  41.             {
  42.                 float oldHeight = rect.height;
  43.                 rect.height = rect.width / meshRatio;
  44.                 rect.y += (oldHeight - rect.height) * rectTransform.pivot.y;
  45.             }
  46.             else
  47.             {
  48.                 float oldWidth = rect.width;
  49.                 rect.width = rect.height * meshRatio;
  50.                 rect.x += (oldWidth - rect.width) * rectTransform.pivot.x;
  51.             }
  52.         }
  53.         // scale to rect transform size
  54.         float scaleX = rect.width / mesh.bounds.size.x;
  55.         float scaleY = rect.height / mesh.bounds.size.y;
  56.         // mesh center in case of offcenter pivot
  57.         Vector3 center = mesh.bounds.center;
  58.  
  59.         Vector3[] verts = mesh.vertices;
  60.         Vector2[] uvs = mesh.uv;
  61.         // check if we have vertex colors assigned
  62.         Color32[] colors = mesh.colors32.Length == verts.Length ? mesh.colors32 : null;
  63.  
  64.  
  65.  
  66.         for (int i = 0; i < verts.Length; i++)
  67.         {
  68.             // use scale on all vertices and offset
  69.             Vector3 vert = verts[i] - center;
  70.             vert.x *= scaleX;
  71.             vert.y *= scaleY;
  72.             vert.z *= scaleX;
  73.  
  74.             // use color assigned in graphic if theres no vertex color
  75.             Color32 col = colors != null ? colors[i] : (Color32)this.color;
  76.             // set new vertices
  77.             vh.AddVert(vert, col, uvs[i]);
  78.         }
  79.         // set triangles
  80.         int[] tris = mesh.triangles;
  81.         for (int i = 0; i < tris.Length; i += 3)
  82.             vh.AddTriangle(tris[i], tris[i + 1], tris[i + 2]);
  83.     }
  84.  
  85. #if UNITY_EDITOR
  86.     protected override void OnValidate()
  87.     {
  88.         base.OnValidate();
  89.         SetVerticesDirty();
  90.         SetMaterialDirty();
  91.  
  92.     }
  93. #endif
  94. }
Advertisement
Add Comment
Please, Sign In to add comment