Created
July 9, 2021 06:00
-
-
Save fanhexin/aaba01becbe9b60ac5af61796defd544 to your computer and use it in GitHub Desktop.
This file contains hidden or 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; | |
namespace UGuiExtension | |
{ | |
public class CenterTiledImage : Image | |
{ | |
protected override void OnPopulateMesh(VertexHelper toFill) | |
{ | |
if (type != Type.Tiled || | |
overrideSprite == null || | |
hasBorder || | |
overrideSprite.texture.wrapMode != TextureWrapMode.Repeat) | |
{ | |
base.OnPopulateMesh(toFill); | |
return; | |
} | |
Vector2 spriteSize = overrideSprite.rect.size; | |
Rect rect = GetPixelAdjustedRect(); | |
float tileWidth = spriteSize.x / pixelsPerUnit; | |
float tileHeight = spriteSize.y / pixelsPerUnit; | |
float xMin = 0; | |
float xMax = rect.width; | |
float yMin = 0; | |
float yMax = rect.height; | |
toFill.Clear(); | |
if (tileWidth <= 0) | |
tileWidth = xMax - xMin; | |
if (tileHeight <= 0) | |
tileHeight = yMax - yMin; | |
Vector2 uvScale = new Vector2((xMax - xMin) / tileWidth, (yMax - yMin) / tileHeight); | |
if (fillCenter) | |
{ | |
AddQuad(toFill, | |
new Vector2(xMin, yMin) + rect.position, | |
new Vector2(xMax, yMax) + rect.position, | |
color, | |
Vector2.zero -0.5f * (uvScale - Vector2.one), | |
Vector2.one + 0.5f * (uvScale - Vector2.one)); | |
} | |
} | |
static void AddQuad(VertexHelper vertexHelper, Vector2 posMin, Vector2 posMax, Color32 color, Vector2 uvMin, Vector2 uvMax) | |
{ | |
int startIndex = vertexHelper.currentVertCount; | |
vertexHelper.AddVert(new Vector3(posMin.x, posMin.y, 0), color, new Vector2(uvMin.x, uvMin.y)); | |
vertexHelper.AddVert(new Vector3(posMin.x, posMax.y, 0), color, new Vector2(uvMin.x, uvMax.y)); | |
vertexHelper.AddVert(new Vector3(posMax.x, posMax.y, 0), color, new Vector2(uvMax.x, uvMax.y)); | |
vertexHelper.AddVert(new Vector3(posMax.x, posMin.y, 0), color, new Vector2(uvMax.x, uvMin.y)); | |
vertexHelper.AddTriangle(startIndex, startIndex + 1, startIndex + 2); | |
vertexHelper.AddTriangle(startIndex + 2, startIndex + 3, startIndex); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment