UGUI教程之-自定义UGUI 扩展集(一)

 

UGUI中我们常用的控件比如Button、Label、ToggleGroup,往往我们会基于这些最基本的控件来实现我们的高级控件,那么本篇文章我们来学习下我们自定义的UGUI拓展控件。

1、UIButton 单独可使用

这个脚本可以 提供外部监听按下/点击/释放的3个事件(参数附带着数据)。  源官方Button只有 点击事件


using UnityEngine.Events;
using UnityEngine.EventSystems;

namespace UnityEngine.UI.Extensions
{
/// <summary>
/// UIButton
/// </summary>
[AddComponentMenu("UI/Extensions/UI Button")]
public class UIButton : Button, IPointerDownHandler, IPointerUpHandler
{
#region Sub-Classes
[System.Serializable]
public class UIButtonEvent : UnityEvent<PointerEventData.InputButton> { }
#endregion

#region Events
public UIButtonEvent OnButtonClick;
public UIButtonEvent OnButtonPress;
public UIButtonEvent OnButtonRelease;
#endregion

public override void OnPointerClick(PointerEventData eventData)
{
base.OnSubmit(eventData);

if (OnButtonClick != null)
{
OnButtonClick.Invoke(eventData.button);
}
}
void IPointerDownHandler.OnPointerDown(PointerEventData eventData)
{
DoStateTransition(SelectionState.Pressed, false);

if (OnButtonPress != null)
{
OnButtonPress.Invoke(eventData.button);
}
}
void IPointerUpHandler.OnPointerUp(PointerEventData eventData)
{
DoStateTransition(SelectionState.Normal, false);

if (OnButtonRelease != null)
{
OnButtonRelease.Invoke(eventData.button);
}
}
}
}

2、UIFlippable  单独可使用

我需要一些时间现在翻转精灵和使用scale解决方案被证明是一个糟糕的主意,当使用布局网格等,所以试着写一个小脚本来翻转顶点相反。它目前做这份工作对我来说,我想分享它所以它在这儿……
这种翻转还有两种实现方式: 设置 Rotation x/y =>180,    Scale x/y =>-1;


using System.Collections.Generic;

namespace UnityEngine.UI.Extensions
{
[RequireComponent(typeof(RectTransform), typeof(Graphic)), DisallowMultipleComponent]
[AddComponentMenu("UI/Effects/Extensions/Flippable")]
public class UIFlippable : MonoBehaviour, IMeshModifier
{
[SerializeField] private bool m_Horizontal = false;
[SerializeField] private bool m_Veritical = false;

/// <summary>
/// 获取或设置一个值,该值指示是否应水平翻转
/// </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>
/// 获取或设置一个值,该值指示是否应垂直翻转
/// </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; }
}

protected void OnValidate()
{
this.GetComponent<Graphic>().SetVerticesDirty();
}

// 从mesh 得到 顶点集
public void ModifyMesh(/*List<UIVertex> verts*/ Mesh mesh)
{
List<UIVertex> verts = new List<UIVertex> ();
using (VertexHelper vertexHelper = new VertexHelper (mesh))
{
vertexHelper.GetUIVertexStream (verts);
}

RectTransform rt = this.transform as RectTransform;

for (int i = 0; i < verts.Count; ++i)
{
UIVertex v = verts[i];

// Modify positions
v.position = new Vector3(
(this.m_Horizontal ? (v.position.x + (rt.rect.center.x - v.position.x) * 2) : v.position.x),
(this.m_Veritical ?  (v.position.y + (rt.rect.center.y - v.position.y) * 2) : v.position.y),
v.position.z
);

// Apply
verts[i] = v;
}

// 在合成mesh
using (VertexHelper vertexHelper2 = new VertexHelper ())
{
vertexHelper2.AddUIVertexTriangleStream (verts);
vertexHelper2.FillMesh (mesh);
}
}
}
}

3、uGUITools   单独可使用

让你更了解 锚点、RectTransform等,   Editor脚本


using UnityEditor;
namespace UnityEngine.UI.Extensions
{
public class uGUITools : MonoBehaviour
{
[MenuItem("uGUI/Anchors to Corners %[")]
static void AnchorsToCorners()
{
foreach (Transform transform in Selection.transforms)
{
RectTransform t = transform as RectTransform;
RectTransform pt = Selection.activeTransform.parent as RectTransform;

if (t == null || pt == null) return;

Vector2 newAnchorsMin = new Vector2(t.anchorMin.x + t.offsetMin.x / pt.rect.width,
t.anchorMin.y + t.offsetMin.y / pt.rect.height);
Vector2 newAnchorsMax = new Vector2(t.anchorMax.x + t.offsetMax.x / pt.rect.width,
t.anchorMax.y + t.offsetMax.y / pt.rect.height);

t.anchorMin = newAnchorsMin;
t.anchorMax = newAnchorsMax;
t.offsetMin = t.offsetMax = new Vector2(0, 0);
}
}

[MenuItem("uGUI/Corners to Anchors %]")]
static void CornersToAnchors()
{
foreach (Transform transform in Selection.transforms)
{
RectTransform t = transform as RectTransform;

if (t == null) return;

t.offsetMin = t.offsetMax = new Vector2(0, 0);
}
}

[MenuItem("uGUI/Mirror Horizontally Around Anchors %;")]
static void MirrorHorizontallyAnchors()
{
MirrorHorizontally(false);
}

[MenuItem("uGUI/Mirror Horizontally Around Parent Center %:")]
static void MirrorHorizontallyParent()
{
MirrorHorizontally(true);
}

static void MirrorHorizontally(bool mirrorAnchors)
{
foreach (Transform transform in Selection.transforms)
{
RectTransform t = transform as RectTransform;
RectTransform pt = Selection.activeTransform.parent as RectTransform;

if (t == null || pt == null) return;

if (mirrorAnchors)
{
Vector2 oldAnchorMin = t.anchorMin;
t.anchorMin = new Vector2(1 - t.anchorMax.x, t.anchorMin.y);
t.anchorMax = new Vector2(1 - oldAnchorMin.x, t.anchorMax.y);
}

Vector2 oldOffsetMin = t.offsetMin;
t.offsetMin = new Vector2(-t.offsetMax.x, t.offsetMin.y);
t.offsetMax = new Vector2(-oldOffsetMin.x, t.offsetMax.y);

t.localScale = new Vector3(-t.localScale.x, t.localScale.y, t.localScale.z);
}
}

[MenuItem("uGUI/Mirror Vertically Around Anchors %'")]
static void MirrorVerticallyAnchors()
{
MirrorVertically(false);
}

[MenuItem("uGUI/Mirror Vertically Around Parent Center %\"")]
static void MirrorVerticallyParent()
{
MirrorVertically(true);
}

static void MirrorVertically(bool mirrorAnchors)
{
foreach (Transform transform in Selection.transforms)
{
RectTransform t = transform as RectTransform;
RectTransform pt = Selection.activeTransform.parent as RectTransform;

if (t == null || pt == null) return;

if (mirrorAnchors)
{
Vector2 oldAnchorMin = t.anchorMin;
t.anchorMin = new Vector2(t.anchorMin.x, 1 - t.anchorMax.y);
t.anchorMax = new Vector2(t.anchorMax.x, 1 - oldAnchorMin.y);
}

Vector2 oldOffsetMin = t.offsetMin;
t.offsetMin = new Vector2(t.offsetMin.x, -t.offsetMax.y);
t.offsetMax = new Vector2(t.offsetMax.x, -oldOffsetMin.y);

t.localScale = new Vector3(t.localScale.x, -t.localScale.y, t.localScale.z);
}
}
}
}

本篇UGUI教程关于UGUI的拓展介绍到此结束,下篇我们来学习ToolTip 、ReturnKeyTriggersButton、NicerOutline等