Unity UGUI 图片 轴对称效果 减少资源
制作UI的过程中,为了节省资源,对称的图一般美术切一半给我们
手动拼图 有时会出现拼接处出现裂缝或重叠
调整大小时也不方便 得一块一块调整
所以就用BaseMeshEffect 的ModifyMesh写了一个脚本
效果是
这样调整这种拼接的UI会方便一些
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI;
[AddComponentMenu("UI/Effects/TestImageGhost")]
public class TestImageGhost : BaseMeshEffect
{
public enum Type
{
Double,
Quad,
}
[SerializeField]
private Type m_type = Type.Double;
[SerializeField]
private bool m_UseGraphicAlpha = true;
private Vector2 xy;
public Type GhostType
{
get { return m_type; }
set
{
if (m_type == value)
return;
m_type = value;
if (graphic != null)
graphic.SetVerticesDirty();
}
}
#if UNITY_EDITOR
protected override void OnValidate()
{
xy = this.GetComponent<RectTransform>().sizeDelta;
base.OnValidate();
if (graphic != null)
graphic.SetVerticesDirty();
}
#endif
protected override void OnEnable()
{
base.OnEnable();
if (graphic != null)
graphic.SetVerticesDirty();
xy = this.GetComponent<RectTransform>().sizeDelta;
Debug.Log("dfsdf");
}
public bool useGraphicAlpha
{
get { return m_UseGraphicAlpha; }
set
{
m_UseGraphicAlpha = value;
if (graphic != null)
graphic.SetVerticesDirty();
}
}
protected void ApplyGhostDouble(List<UIVertex> verts, int start, int end, float x1, float y1, float x2, float y2,bool self = false)
{
UIVertex vt;
var neededCpacity = verts.Count * 2;
if (verts.Capacity < neededCpacity)
verts.Capacity = neededCpacity;
for (int i = start; i < end; i++)
{
vt = verts[i];
if(!self)
verts.Add(vt);//添加一遍mesh
Vector3 v = vt.position;
int offset = i % 6;
switch (offset)
{
case 0:
case 1:
case 5: v.x += x1; break;
case 3:
case 4:
case 2: v.x += x2; break;
}
switch (offset)
{
case 1:
case 2:
case 3: v.y += y1; break;
case 0:
case 4:
case 5: v.y += y2; break;
}
vt.position = v;
verts[i] = vt;
}
}
protected void ApplyGhost(List<UIVertex> verts, int start, int end)
{
if (m_type == Type.Double)
{
var neededCpacity = verts.Count * 2;
if (verts.Capacity < neededCpacity)
verts.Capacity = neededCpacity;
ApplyGhostDouble(verts, start, end, 0, 0, -xy.x / 2,0);
start = end;
end = verts.Count;
ApplyGhostDouble(verts, start, end, xy.x, 0, -xy.x / 2, 0,true);
}
else
{
var neededCpacity = verts.Count * 4;
if (verts.Capacity < neededCpacity)
verts.Capacity = neededCpacity;
start = 0;
end = verts.Count;
ApplyGhostDouble(verts, start, end, 0 , 0, -xy.x / 2, xy.y / 2);
start = end;
end = verts.Count;
ApplyGhostDouble(verts, start, end, xy.x ,0, -xy.x / 2, xy.y / 2);
start = end;
end = verts.Count;
ApplyGhostDouble(verts, start, end, 0, -xy.y, -xy.x / 2, xy.y / 2);
start = end;
end = verts.Count;
ApplyGhostDouble(verts, start, end, xy.x, -xy.y, -xy.x / 2, xy.y / 2, true);
}
}
public override void ModifyMesh(VertexHelper vh)
{
if (!IsActive())
return;
xy = GetComponent<RectTransform>().sizeDelta;
List<UIVertex> output = new List<UIVertex>();
vh.GetUIVertexStream(output);
ApplyGhost(output, 0, output.Count);
vh.Clear();
vh.AddUIVertexTriangleStream(output);
}
}
using UnityEngine.UI;
[AddComponentMenu("UI/Effects/TestImageGhost")]
public class TestImageGhost : BaseMeshEffect
{
public enum Type
{
Double,
Quad,
}
[SerializeField]
private Type m_type = Type.Double;
[SerializeField]
private bool m_UseGraphicAlpha = true;
private Vector2 xy;
public Type GhostType
{
get { return m_type; }
set
{
if (m_type == value)
return;
m_type = value;
if (graphic != null)
graphic.SetVerticesDirty();
}
}
#if UNITY_EDITOR
protected override void OnValidate()
{
xy = this.GetComponent<RectTransform>().sizeDelta;
base.OnValidate();
if (graphic != null)
graphic.SetVerticesDirty();
}
#endif
protected override void OnEnable()
{
base.OnEnable();
if (graphic != null)
graphic.SetVerticesDirty();
xy = this.GetComponent<RectTransform>().sizeDelta;
Debug.Log("dfsdf");
}
public bool useGraphicAlpha
{
get { return m_UseGraphicAlpha; }
set
{
m_UseGraphicAlpha = value;
if (graphic != null)
graphic.SetVerticesDirty();
}
}
protected void ApplyGhostDouble(List<UIVertex> verts, int start, int end, float x1, float y1, float x2, float y2,bool self = false)
{
UIVertex vt;
var neededCpacity = verts.Count * 2;
if (verts.Capacity < neededCpacity)
verts.Capacity = neededCpacity;
for (int i = start; i < end; i++)
{
vt = verts[i];
if(!self)
verts.Add(vt);//添加一遍mesh
Vector3 v = vt.position;
int offset = i % 6;
switch (offset)
{
case 0:
case 1:
case 5: v.x += x1; break;
case 3:
case 4:
case 2: v.x += x2; break;
}
switch (offset)
{
case 1:
case 2:
case 3: v.y += y1; break;
case 0:
case 4:
case 5: v.y += y2; break;
}
vt.position = v;
verts[i] = vt;
}
}
protected void ApplyGhost(List<UIVertex> verts, int start, int end)
{
if (m_type == Type.Double)
{
var neededCpacity = verts.Count * 2;
if (verts.Capacity < neededCpacity)
verts.Capacity = neededCpacity;
ApplyGhostDouble(verts, start, end, 0, 0, -xy.x / 2,0);
start = end;
end = verts.Count;
ApplyGhostDouble(verts, start, end, xy.x, 0, -xy.x / 2, 0,true);
}
else
{
var neededCpacity = verts.Count * 4;
if (verts.Capacity < neededCpacity)
verts.Capacity = neededCpacity;
start = 0;
end = verts.Count;
ApplyGhostDouble(verts, start, end, 0 , 0, -xy.x / 2, xy.y / 2);
start = end;
end = verts.Count;
ApplyGhostDouble(verts, start, end, xy.x ,0, -xy.x / 2, xy.y / 2);
start = end;
end = verts.Count;
ApplyGhostDouble(verts, start, end, 0, -xy.y, -xy.x / 2, xy.y / 2);
start = end;
end = verts.Count;
ApplyGhostDouble(verts, start, end, xy.x, -xy.y, -xy.x / 2, xy.y / 2, true);
}
}
public override void ModifyMesh(VertexHelper vh)
{
if (!IsActive())
return;
xy = GetComponent<RectTransform>().sizeDelta;
List<UIVertex> output = new List<UIVertex>();
vh.GetUIVertexStream(output);
ApplyGhost(output, 0, output.Count);
vh.Clear();
vh.AddUIVertexTriangleStream(output);
}
}