2015-02-03 03:25:41 +08:00
|
|
|
/// Credit ChoMPHi
|
|
|
|
/// Sourced from - http://forum.unity3d.com/threads/script-flippable-for-ui-graphics.291711/
|
2015-02-03 07:07:31 +08:00
|
|
|
|
|
|
|
namespace UnityEngine.UI.Extensions
|
2015-02-03 03:25:41 +08:00
|
|
|
{
|
2015-02-03 07:07:31 +08:00
|
|
|
[RequireComponent(typeof(RectTransform), typeof(Graphic)), DisallowMultipleComponent]
|
|
|
|
[AddComponentMenu("UI/Effects/Extensions/Flippable")]
|
2017-07-02 22:36:39 +08:00
|
|
|
public class UIFlippable : BaseMeshEffect
|
2015-02-03 07:07:31 +08:00
|
|
|
{
|
2015-02-03 03:25:41 +08:00
|
|
|
[SerializeField] private bool m_Horizontal = false;
|
|
|
|
[SerializeField] private bool m_Veritical = false;
|
2017-07-02 22:36:39 +08:00
|
|
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
protected override void Awake()
|
|
|
|
{
|
|
|
|
OnValidate();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-02-03 03:25:41 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets a value indicating whether this <see cref="UnityEngine.UI.UIFlippable"/> should be flipped horizontally.
|
|
|
|
/// </summary>
|
|
|
|
/// <value><c>true</c> if horizontal; otherwise, <c>false</c>.</value>
|
|
|
|
public bool horizontal
|
|
|
|
{
|
|
|
|
get { return this.m_Horizontal; }
|
|
|
|
set { this.m_Horizontal = value; }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets a value indicating whether this <see cref="UnityEngine.UI.UIFlippable"/> should be flipped vertically.
|
|
|
|
/// </summary>
|
|
|
|
/// <value><c>true</c> if vertical; otherwise, <c>false</c>.</value>
|
|
|
|
public bool vertical
|
|
|
|
{
|
|
|
|
get { return this.m_Veritical; }
|
|
|
|
set { this.m_Veritical = value; }
|
|
|
|
}
|
2017-07-02 22:36:39 +08:00
|
|
|
|
|
|
|
public override void ModifyMesh(VertexHelper verts)
|
2015-02-03 03:25:41 +08:00
|
|
|
{
|
|
|
|
RectTransform rt = this.transform as RectTransform;
|
|
|
|
|
2015-10-11 18:33:43 +08:00
|
|
|
for (int i = 0; i < verts.currentVertCount; ++i)
|
2015-02-03 03:25:41 +08:00
|
|
|
{
|
2015-10-11 18:33:43 +08:00
|
|
|
UIVertex uiVertex = new UIVertex();
|
|
|
|
verts.PopulateUIVertex(ref uiVertex,i);
|
|
|
|
|
2015-02-03 03:25:41 +08:00
|
|
|
// Modify positions
|
2015-10-11 18:33:43 +08:00
|
|
|
uiVertex.position = new Vector3(
|
|
|
|
(this.m_Horizontal ? (uiVertex.position.x + (rt.rect.center.x - uiVertex.position.x) * 2) : uiVertex.position.x),
|
|
|
|
(this.m_Veritical ? (uiVertex.position.y + (rt.rect.center.y - uiVertex.position.y) * 2) : uiVertex.position.y),
|
|
|
|
uiVertex.position.z
|
2015-02-03 03:25:41 +08:00
|
|
|
);
|
2015-10-11 18:33:43 +08:00
|
|
|
|
2015-02-03 03:25:41 +08:00
|
|
|
// Apply
|
2015-10-11 18:33:43 +08:00
|
|
|
verts.SetUIVertex(uiVertex, i);
|
2015-02-03 03:25:41 +08:00
|
|
|
}
|
2015-10-11 18:33:43 +08:00
|
|
|
}
|
2015-09-10 05:31:17 +08:00
|
|
|
|
2017-07-02 22:36:39 +08:00
|
|
|
#if UNITY_EDITOR
|
|
|
|
protected override void OnValidate()
|
2015-10-11 18:33:43 +08:00
|
|
|
{
|
2017-07-02 22:36:39 +08:00
|
|
|
var components = gameObject.GetComponents(typeof(BaseMeshEffect));
|
|
|
|
foreach (var comp in components)
|
|
|
|
{
|
|
|
|
if (comp.GetType() != typeof(UIFlippable))
|
|
|
|
{
|
|
|
|
UnityEditorInternal.ComponentUtility.MoveComponentUp(this);
|
|
|
|
}
|
|
|
|
else break;
|
|
|
|
}
|
|
|
|
this.GetComponent<Graphic>().SetVerticesDirty();
|
|
|
|
base.OnValidate();
|
2015-02-03 03:25:41 +08:00
|
|
|
}
|
2017-07-02 22:36:39 +08:00
|
|
|
#endif
|
2015-02-03 03:25:41 +08:00
|
|
|
}
|
|
|
|
}
|