Last active
December 1, 2024 10:12
-
-
Save shivaduke28/728f7217b2f37d0784d042b52d65dad9 to your computer and use it in GitHub Desktop.
Unity uGUI frame line
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
// This code is released under CC0 1.0 Universal (Public Domain Dedication). | |
// https://creativecommons.org/publicdomain/zero/1.0/ | |
using UnityEngine; | |
using UnityEngine.UI; | |
namespace Rector.UI.Components | |
{ | |
public sealed class FrameView : Graphic | |
{ | |
[SerializeField, Range(0, 100f)] float lineWidthVertical = 1f; | |
[SerializeField, Range(0, 100f)] float lineWidthHorizontal = 1f; | |
[SerializeField] bool uniform = true; | |
protected override void OnPopulateMesh(VertexHelper vh) | |
{ | |
var left = 0f; | |
var right = 1f; | |
var bottom = 0f; | |
var top = 1f; | |
var rectTrans = rectTransform; | |
var pivot = rectTrans.pivot; | |
var rect = rectTrans.rect; | |
var w = rect.width; | |
var h = rect.height; | |
left -= pivot.x; | |
right -= pivot.x; | |
bottom -= pivot.y; | |
top -= pivot.y; | |
left *= w; | |
right *= w; | |
top *= h; | |
bottom *= h; | |
vh.Clear(); | |
var hor = uniform ? lineWidthVertical : lineWidthHorizontal; | |
AddQuad(vh, new Vector2(left, bottom), new Vector2(left + hor, top), 0); | |
AddQuad(vh, new Vector2(left + hor, bottom), new Vector2(right - hor, bottom + lineWidthVertical), 4); | |
AddQuad(vh, new Vector2(right - hor, bottom), new Vector2(right, top), 8); | |
AddQuad(vh, new Vector2(left + hor, top - lineWidthVertical), new Vector2(right - lineWidthVertical, top), 12); | |
} | |
void AddQuad(VertexHelper vh, Vector2 leftBottom, Vector2 rightTop, int offset) | |
{ | |
var vert = UIVertex.simpleVert; | |
vert.color = color; | |
vert.position = leftBottom; | |
vh.AddVert(vert); | |
vert.position = new Vector2(rightTop.x, leftBottom.y); | |
vh.AddVert(vert); | |
vert.position = rightTop; | |
vh.AddVert(vert); | |
vert.position = new Vector2(leftBottom.x, rightTop.y); | |
vh.AddVert(vert); | |
vh.AddTriangle(0 + offset, 1 + offset, 2 + offset); | |
vh.AddTriangle(2 + offset, 3 + offset, 0 + offset); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment