Unity UGUI 图片 轴对称效果 减少资源
2016-04-24 21:31
477 查看
制作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);
}
}
手动拼图 有时会出现拼接处出现裂缝或重叠
调整大小时也不方便 得一块一块调整
所以就用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);
}
}
相关文章推荐
- Unity常用脚本类的继承关系
- Unity基础知识结构总结
- Root Motion深度解析[Unity]
- Unity Shader 三 卡通特效
- Unity学习笔记四 - Survival Shooter Tutorial
- unity3D 5中如何修改及显示Text的值
- Unity协程介绍以及使用
- 用Unity3D内部频谱分析方法做音乐视觉特效的原理说明
- unity3d 三分钟实现简单的赛车漂移
- 关于unity中的update、Lateupdate和FixedUpdate
- Unity3D 中 Generic 动画导入设置和 Root Motion 之间的关系
- Unity序列化和持久化
- Unity Shaders and Effects Cookbook (4-5)Cubemap与菲涅尔反射
- 关于Euler angles(欧拉角)的问题
- 理解和使用 Unity UI 系统(canvas和CanvasScaler )
- Unity脚本执行顺序
- blenderImportToUnity
- MAC下双开UNITY
- unity Tween相关
- Unity3D使用NGUI实现简单背包功能