From 6796238cecee5e849759fb7cda3d15fe86012582 Mon Sep 17 00:00:00 2001 From: David Gileadi Date: Sun, 23 Jul 2017 22:42:17 -0700 Subject: [PATCH] Recreate sprites instead of modifying the mesh --- Scripts/Controls/SegmentedControl.cs | 80 ++++++++++++---------------- 1 file changed, 34 insertions(+), 46 deletions(-) diff --git a/Scripts/Controls/SegmentedControl.cs b/Scripts/Controls/SegmentedControl.cs index 4486643..cc73298 100644 --- a/Scripts/Controls/SegmentedControl.cs +++ b/Scripts/Controls/SegmentedControl.cs @@ -146,33 +146,50 @@ namespace UnityEngine.UI.Extensions selectedSegment = null; } + private void RecreateSprites() + { + for (int i = 0; i < segments.Length; i++) + { + if (segments[i].image == null) + continue; + + var sprite = segments[i].image.sprite; + if (sprite.border.x == 0 || sprite.border.z == 0) + continue; + + var rect = sprite.rect; + var border = sprite.border; + + if (i > 0) + { + rect.xMin = border.x; + border.x = 0; + } + if (i < segments.Length - 1) + { + rect.xMax = border.z; + border.z = 0; + } + + segments[i].image.sprite = Sprite.Create(sprite.texture, rect, sprite.pivot, sprite.pixelsPerUnit, 0, SpriteMeshType.FullRect, border); + } + } + public void LayoutSegments() { + RecreateSprites(); + RectTransform transform = this.transform as RectTransform; float width = (transform.rect.width / segments.Length) - (separatorWidth * (segments.Length - 1)); for (int i = 0; i < segments.Length; i++) { - float leftX = 0; - float rightX = 0; - if (segments[i].image != null && segments[i].image.hasBorder) - { - if (i > 0) - { - leftX = segments[i].image.sprite.border.x / segments[i].image.pixelsPerUnit; - } - if (i < segments.Length - 1) - { - rightX = segments[i].image.sprite.border.z / segments[i].image.pixelsPerUnit; - } - } - - float insetX = ((width + separatorWidth) * i) - leftX; + float insetX = ((width + separatorWidth) * i); var rectTransform = segments[i].GetComponent(); rectTransform.anchorMin = Vector2.zero; rectTransform.anchorMax = Vector2.zero; - rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, insetX, width + leftX + rightX); + rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, insetX, width); rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 0, transform.rect.height); if (separator && i > 0) @@ -184,7 +201,7 @@ namespace UnityEngine.UI.Extensions sep.rectTransform.SetParent(this.transform, false); sep.rectTransform.anchorMin = Vector2.zero; sep.rectTransform.anchorMax = Vector2.zero; - sep.rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, insetX + leftX - separatorWidth, separatorWidth); + sep.rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, insetX - separatorWidth, separatorWidth); sep.rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 0, transform.rect.height); } // TODO: maybe adjust text position @@ -195,7 +212,6 @@ namespace UnityEngine.UI.Extensions [RequireComponent(typeof(Button))] public class Segment : UIBehaviour, - IMeshModifier, IPointerClickHandler, ISubmitHandler, IPointerEnterHandler, IPointerExitHandler, @@ -402,33 +418,5 @@ namespace UnityEngine.UI.Extensions button.animator.SetTrigger(triggername); } - - public void ModifyMesh(Mesh mesh) - { - throw new NotSupportedException(); - } - - public void ModifyMesh(VertexHelper verts) - { - if (verts.currentIndexCount != 54) - { - return; - } - - List vertexList = new List(); - verts.GetUIVertexStream(vertexList); - - if (!leftmost) - { - vertexList.RemoveRange(0, 18); - } - if (!rightmost) - { - vertexList.RemoveRange(vertexList.Count - 18, 18); - } - - verts.Clear(); - verts.AddUIVertexTriangleStream(vertexList); - } } } \ No newline at end of file