Recreate sprites instead of modifying the mesh

pull/413/head
David Gileadi 2017-07-23 22:42:17 -07:00
parent 680076b631
commit 6796238cec
1 changed files with 34 additions and 46 deletions

View File

@ -146,33 +146,50 @@ namespace UnityEngine.UI.Extensions
selectedSegment = null; 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() public void LayoutSegments()
{ {
RecreateSprites();
RectTransform transform = this.transform as RectTransform; RectTransform transform = this.transform as RectTransform;
float width = (transform.rect.width / segments.Length) - (separatorWidth * (segments.Length - 1)); float width = (transform.rect.width / segments.Length) - (separatorWidth * (segments.Length - 1));
for (int i = 0; i < segments.Length; i++) for (int i = 0; i < segments.Length; i++)
{ {
float leftX = 0; float insetX = ((width + separatorWidth) * i);
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;
var rectTransform = segments[i].GetComponent<RectTransform>(); var rectTransform = segments[i].GetComponent<RectTransform>();
rectTransform.anchorMin = Vector2.zero; rectTransform.anchorMin = Vector2.zero;
rectTransform.anchorMax = 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); rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 0, transform.rect.height);
if (separator && i > 0) if (separator && i > 0)
@ -184,7 +201,7 @@ namespace UnityEngine.UI.Extensions
sep.rectTransform.SetParent(this.transform, false); sep.rectTransform.SetParent(this.transform, false);
sep.rectTransform.anchorMin = Vector2.zero; sep.rectTransform.anchorMin = Vector2.zero;
sep.rectTransform.anchorMax = 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); sep.rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 0, transform.rect.height);
} }
// TODO: maybe adjust text position // TODO: maybe adjust text position
@ -195,7 +212,6 @@ namespace UnityEngine.UI.Extensions
[RequireComponent(typeof(Button))] [RequireComponent(typeof(Button))]
public class Segment : public class Segment :
UIBehaviour, UIBehaviour,
IMeshModifier,
IPointerClickHandler, IPointerClickHandler,
ISubmitHandler, ISubmitHandler,
IPointerEnterHandler, IPointerExitHandler, IPointerEnterHandler, IPointerExitHandler,
@ -402,33 +418,5 @@ namespace UnityEngine.UI.Extensions
button.animator.SetTrigger(triggername); button.animator.SetTrigger(triggername);
} }
public void ModifyMesh(Mesh mesh)
{
throw new NotSupportedException();
}
public void ModifyMesh(VertexHelper verts)
{
if (verts.currentIndexCount != 54)
{
return;
}
List<UIVertex> vertexList = new List<UIVertex>();
verts.GetUIVertexStream(vertexList);
if (!leftmost)
{
vertexList.RemoveRange(0, 18);
}
if (!rightmost)
{
vertexList.RemoveRange(vertexList.Count - 18, 18);
}
verts.Clear();
verts.AddUIVertexTriangleStream(vertexList);
}
} }
} }