Last active
February 4, 2023 07:47
-
-
Save MattRix/336813c8168693284183ec729b0bb405 to your computer and use it in GitHub Desktop.
How to draw a custom mesh in a UI.Graphic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.UI; | |
using System.Collections.Generic; | |
[RequireComponent(typeof(CanvasRenderer))] | |
public class SimpleGraphic : MaskableGraphic //could extend Graphic if you don't need it maskable | |
{ | |
public Texture texture; | |
public override Texture mainTexture | |
{ | |
get {return texture;} | |
} | |
//if you set the texture from code, make sure to call this.SetMaterialDirty(); | |
//if you want to force the vertices to be redrawn, call this.SetVerticesDirty(); | |
protected override void OnPopulateMesh(VertexHelper vh) | |
{ | |
float minX = (0f - rectTransform.pivot.x) * rectTransform.rect.width; | |
float minY = (0f - rectTransform.pivot.y) * rectTransform.rect.height; | |
float maxX = (1f - rectTransform.pivot.x) * rectTransform.rect.width; | |
float maxY = (1f - rectTransform.pivot.y) * rectTransform.rect.height; | |
var color32 = (Color32)color; | |
vh.Clear(); | |
vh.AddVert(new Vector3(minX,minY), color32, new Vector2(0, 0)); | |
vh.AddVert(new Vector3(minX,maxY), color32, new Vector2(0, 1)); | |
vh.AddVert(new Vector3(maxX,maxY), color32, new Vector2(1, 1)); | |
vh.AddVert(new Vector3(maxX,minY), color32, new Vector2(1, 0)); | |
vh.AddTriangle(0, 1, 2); | |
vh.AddTriangle(2, 3, 0); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment