From c0f0786227c1cf17bb990755cd63c5e2c23cfe0f Mon Sep 17 00:00:00 2001 From: "Simon (darkside) Jackson" Date: Thu, 10 Sep 2015 18:50:23 +0100 Subject: [PATCH] Resolved FillVBO to PopulateMesh errors Added ButtonHelp event to UIButton Fixed issue #21, changed the base control for the LineRenderers from Graphic to MaskableGraphic --HG-- branch : develop_5.2 --- Scripts/ImageExtended.cs | 14 ++++++++++++- Scripts/UIButton.cs | 27 +++++++++++++++++++++++- Scripts/UILineRenderer.cs | 36 +++++++++++++++++++------------- Scripts/UILineTextureRenderer.cs | 22 ++++++++++++------- 4 files changed, 76 insertions(+), 23 deletions(-) diff --git a/Scripts/ImageExtended.cs b/Scripts/ImageExtended.cs index d88f7f8..87a82d9 100644 --- a/Scripts/ImageExtended.cs +++ b/Scripts/ImageExtended.cs @@ -253,8 +253,14 @@ namespace UnityEngine.UI.Extensions /// Update the UI renderer mesh. /// - protected override void OnFillVBO(List vbo) + protected override void OnPopulateMesh(Mesh toFill) { + List vbo = new List(); + using (var helper = new VertexHelper(toFill)) + { + helper.GetUIVertexStream(vbo); + } + switch (type) { case Type.Simple: @@ -270,6 +276,12 @@ namespace UnityEngine.UI.Extensions GenerateFilledSprite(vbo, m_PreserveAspect); break; } + + using (var helper = new VertexHelper()) + { + helper.AddUIVertexTriangleStream(vbo); + helper.FillMesh(toFill); + } } #region Various fill functions diff --git a/Scripts/UIButton.cs b/Scripts/UIButton.cs index f912d6c..a2836a7 100644 --- a/Scripts/UIButton.cs +++ b/Scripts/UIButton.cs @@ -1,5 +1,6 @@ /// Credit AriathTheWise /// Sourced from - http://forum.unity3d.com/threads/scripts-useful-4-6-scripts-collection.264161/page-2#post-1796783 +/// Extended to include a HELD state that continually fires while the button is held down. using UnityEngine.Events; using UnityEngine.EventSystems; @@ -18,10 +19,18 @@ namespace UnityEngine.UI.Extensions #endregion #region Events + [Tooltip("Event that fires when a button is clicked")] public UIButtonEvent OnButtonClick; + [Tooltip("Event that fires when a button is initially pressed down")] public UIButtonEvent OnButtonPress; + [Tooltip("Event that fires when a button is released")] public UIButtonEvent OnButtonRelease; + [Tooltip("Event that continually fires while a button is held down")] + public UIButtonEvent OnButtonHeld; #endregion + + private bool _pressed; + private PointerEventData _heldEventData; public override void OnPointerClick(PointerEventData eventData) { @@ -42,6 +51,8 @@ namespace UnityEngine.UI.Extensions { OnButtonPress.Invoke(eventData.button); } + _pressed = true; + _heldEventData = eventData; } @@ -53,6 +64,20 @@ namespace UnityEngine.UI.Extensions { OnButtonRelease.Invoke(eventData.button); } - } + _pressed = false; + _heldEventData = null; + } + + void Update() + { + if (!_pressed) + return; + + if (OnButtonHeld != null) + { + OnButtonHeld.Invoke(_heldEventData.button); + } + + } } } \ No newline at end of file diff --git a/Scripts/UILineRenderer.cs b/Scripts/UILineRenderer.cs index c2d0369..910bc57 100644 --- a/Scripts/UILineRenderer.cs +++ b/Scripts/UILineRenderer.cs @@ -6,7 +6,7 @@ using System.Collections.Generic; namespace UnityEngine.UI.Extensions { [AddComponentMenu("UI/Extensions/UILineRenderer")] - public class UILineRenderer : Graphic + public class UILineRenderer : MaskableGraphic { [SerializeField] Texture m_Texture; @@ -65,7 +65,7 @@ namespace UnityEngine.UI.Extensions } } - protected override void OnFillVBO(List vbo) + protected override void OnPopulateMesh(Mesh toFill) { // requires sets of quads if (Points == null || Points.Length < 2) @@ -107,7 +107,8 @@ namespace UnityEngine.UI.Extensions offsetY += Margin.y / 2f; } - vbo.Clear(); + toFill.Clear(); + var vbo = new VertexHelper(toFill); Vector2 prevV1 = Vector2.zero; Vector2 prevV2 = Vector2.zero; @@ -143,31 +144,38 @@ namespace UnityEngine.UI.Extensions Vector2[] uvs = new[] { uvTopCenter, uvBottomCenter, uvBottomCenter, uvTopCenter }; if (i > 1) - SetVbo(vbo, new[] { prevV1, prevV2, v1, v2 }, uvs); + vbo.AddUIVertexQuad(SetVbo(new[] { prevV1, prevV2, v1, v2 }, uvs)); if (i == 1) uvs = new[] { uvTopLeft, uvBottomLeft, uvBottomCenter, uvTopCenter }; else if (i == TempPoints.Length - 1) uvs = new[] { uvTopCenter, uvBottomCenter, uvBottomRight, uvTopRight }; - SetVbo(vbo, new[] { v1, v2, v3, v4 }, uvs); + vbo.AddUIVertexQuad(SetVbo(new[] { v1, v2, v3, v4 }, uvs)); prevV1 = v3; prevV2 = v4; } + + if (vbo.currentVertCount > 3) + { + vbo.FillMesh(toFill); + } } - protected void SetVbo(List vbo, Vector2[] vertices, Vector2[] uvs) + protected UIVertex[] SetVbo(Vector2[] vertices, Vector2[] uvs) { - for (int i = 0; i < vertices.Length; i++) - { - var vert = UIVertex.simpleVert; - vert.color = color; - vert.position = vertices[i]; - vert.uv0 = uvs[i]; - vbo.Add(vert); - } + UIVertex[] vbo = new UIVertex[4]; + for (int i = 0; i < vertices.Length; i++) + { + var vert = UIVertex.simpleVert; + vert.color = color; + vert.position = vertices[i]; + vert.uv0 = uvs[i]; + vbo[i] = vert; + } + return vbo; } public Vector3 RotatePointAroundPivot(Vector3 point, Vector3 pivot, Vector3 angles) diff --git a/Scripts/UILineTextureRenderer.cs b/Scripts/UILineTextureRenderer.cs index dcacf05..5695bf9 100644 --- a/Scripts/UILineTextureRenderer.cs +++ b/Scripts/UILineTextureRenderer.cs @@ -7,7 +7,7 @@ using System.Collections.Generic; namespace UnityEngine.UI.Extensions { [AddComponentMenu("UI/Extensions/UILineTextureRenderer")] - public class UILineTextureRenderer : Graphic + public class UILineTextureRenderer : MaskableGraphic { [SerializeField] Texture m_Texture; @@ -66,7 +66,7 @@ namespace UnityEngine.UI.Extensions } } - protected override void OnFillVBO(List vbo) + protected override void OnPopulateMesh(Mesh toFill) { // requires sets of quads if (Points == null || Points.Length < 2) @@ -108,7 +108,8 @@ namespace UnityEngine.UI.Extensions offsetY += Margin.y / 2f; } - vbo.Clear(); + toFill.Clear(); + var vbo = new VertexHelper(toFill); Vector2 prevV1 = Vector2.zero; Vector2 prevV2 = Vector2.zero; @@ -144,31 +145,38 @@ namespace UnityEngine.UI.Extensions Vector2[] uvs = new[] { uvTopCenter, uvBottomCenter, uvBottomCenter, uvTopCenter }; if (i > 1) - SetVbo(vbo, new[] { prevV1, prevV2, v1, v2 }, uvs); + vbo.AddUIVertexQuad(SetVbo(new[] { prevV1, prevV2, v1, v2 }, uvs)); if (i == 1) uvs = new[] { uvTopLeft, uvBottomLeft, uvBottomCenter, uvTopCenter }; else if (i == TempPoints.Length - 1) uvs = new[] { uvTopCenter, uvBottomCenter, uvBottomRight, uvTopRight }; - SetVbo(vbo, new[] { v1, v2, v3, v4 }, uvs); + vbo.AddUIVertexQuad(SetVbo(new[] { v1, v2, v3, v4 }, uvs)); prevV1 = v3; prevV2 = v4; } + + if (vbo.currentVertCount > 3) + { + vbo.FillMesh(toFill); + } } - protected void SetVbo(List vbo, Vector2[] vertices, Vector2[] uvs) + protected UIVertex[] SetVbo(Vector2[] vertices, Vector2[] uvs) { + UIVertex[] vbo = new UIVertex[4]; for (int i = 0; i < vertices.Length; i++) { var vert = UIVertex.simpleVert; vert.color = color; vert.position = vertices[i]; vert.uv0 = uvs[i]; - vbo.Add(vert); + vbo[i] = vert; } + return vbo; } public Vector3 RotatePointAroundPivot(Vector3 point, Vector3 pivot, Vector3 angles)