diff --git a/Scripts/Controls/TextPic.cs b/Scripts/Controls/TextPic.cs index c56e592..a80a6ed 100644 --- a/Scripts/Controls/TextPic.cs +++ b/Scripts/Controls/TextPic.cs @@ -13,7 +13,7 @@ namespace UnityEngine.UI.Extensions // Image according to the label inside the name attribute to load, read from the Resources directory. The size of the image is controlled by the size property. // Use: [AddComponentMenu("UI/Extensions/TextPic")] - + [ExecuteInEditMode] // Needed for culling images that are not used // public class TextPic : Text, IPointerClickHandler, IPointerExitHandler, IPointerEnterHandler, ISelectHandler { @@ -81,7 +81,7 @@ namespace UnityEngine.UI.Extensions //private bool selected = false; private List positions = new List(); - + /** * Little heck to support multiple hrefs with same name */ @@ -103,7 +103,7 @@ namespace UnityEngine.UI.Extensions iconList.Add(icon.name, icon.sprite); } } - Reset_m_HrefInfos (); + Reset_m_HrefInfos(); } protected void UpdateQuadImage() @@ -133,8 +133,12 @@ namespace UnityEngine.UI.Extensions var go = DefaultControls.CreateImage(resources); go.layer = gameObject.layer; var rt = go.transform as RectTransform; + if (rt) { + rt.anchorMin = new Vector2(0, 1); + rt.anchorMax = new Vector2(0, 1); + rt.pivot = new Vector2(0.5f, 0.5f); rt.SetParent(rectTransform); rt.localPosition = Vector3.zero; rt.localRotation = Quaternion.identity; @@ -146,6 +150,7 @@ namespace UnityEngine.UI.Extensions var spriteName = match.Groups[1].Value; //var size = float.Parse(match.Groups[2].Value); var img = m_ImagesPool[m_ImagesVertexIndex.Count - 1]; + if (img.sprite == null || img.sprite.name != spriteName) { // img.sprite = Resources.Load(spriteName); @@ -181,7 +186,8 @@ namespace UnityEngine.UI.Extensions m_ImagesPool.Remove(m_ImagesPool[i]); } } - if (culled_ImagesPool.Count > 1) { + if (culled_ImagesPool.Count > 1) + { clearImages = true; } } @@ -195,11 +201,13 @@ namespace UnityEngine.UI.Extensions positions.Clear(); UIVertex vert = new UIVertex(); + for (var i = 0; i < m_ImagesVertexIndex.Count; i++) { var endIndex = m_ImagesVertexIndex[i]; var rt = m_ImagesPool[i].rectTransform; var size = rt.sizeDelta; + if (endIndex < toFill.currentVertCount) { toFill.PopulateUIVertex(ref vert, endIndex); @@ -207,7 +215,9 @@ namespace UnityEngine.UI.Extensions // Erase the lower left corner of the black specks toFill.PopulateUIVertex(ref vert, endIndex - 3); + var pos = vert.position; + for (int j = endIndex, m = endIndex - 3; j > m; j--) { toFill.PopulateUIVertex(ref vert, endIndex); @@ -233,8 +243,10 @@ namespace UnityEngine.UI.Extensions // Hyperlink inside the text is added to surround the vertex index coordinate frame toFill.PopulateUIVertex(ref vert, hrefInfo.startIndex); + var pos = vert.position; var bounds = new Bounds(pos, Vector3.zero); + for (int i = hrefInfo.startIndex, m = hrefInfo.endIndex; i < m; i++) { if (i >= toFill.currentVertCount) @@ -297,7 +309,7 @@ namespace UnityEngine.UI.Extensions protected string GetOutputText() { s_TextBuilder.Length = 0; - + var indexText = 0; fixedString = this.text; if (inspectorIconList != null && inspectorIconList.Length > 0) @@ -311,13 +323,16 @@ namespace UnityEngine.UI.Extensions } } int count = 0; + foreach (Match match in s_HrefRegex.Matches(fixedString)) { s_TextBuilder.Append(fixedString.Substring(indexText, match.Index - indexText)); s_TextBuilder.Append(""); // Hyperlink color var group = match.Groups[1]; - if(isCreating_m_HrefInfos) { + + if (isCreating_m_HrefInfos) + { var hrefInfo = new HrefInfo { startIndex = s_TextBuilder.Length * 4, // Hyperlinks in text starting vertex indices @@ -325,8 +340,10 @@ namespace UnityEngine.UI.Extensions name = group.Value }; m_HrefInfos.Add(hrefInfo); - } else { - if(m_HrefInfos.Count > 0) { + } + else { + if (m_HrefInfos.Count > 0) + { m_HrefInfos[count].startIndex = s_TextBuilder.Length * 4; // Hyperlinks in text starting vertex indices; m_HrefInfos[count].endIndex = (s_TextBuilder.Length + match.Groups[2].Length - 1) * 4 + 3; count++; @@ -338,9 +355,9 @@ namespace UnityEngine.UI.Extensions indexText = match.Index + match.Length; } // we should create array only once or if there is any change in the text - if(isCreating_m_HrefInfos) + if (isCreating_m_HrefInfos) isCreating_m_HrefInfos = false; - + s_TextBuilder.Append(fixedString.Substring(indexText, fixedString.Length - indexText)); return s_TextBuilder.ToString(); @@ -359,6 +376,7 @@ namespace UnityEngine.UI.Extensions foreach (var hrefInfo in m_HrefInfos) { var boxes = hrefInfo.boxes; + for (var i = 0; i < boxes.Count; ++i) { if (boxes[i].Contains(lp)) @@ -388,7 +406,6 @@ namespace UnityEngine.UI.Extensions public void OnPointerExit(PointerEventData eventData) { - //do your stuff when highlighted //selected = false; if (m_ImagesPool.Count >= 1) @@ -406,6 +423,7 @@ namespace UnityEngine.UI.Extensions } } } + public void OnSelect(BaseEventData eventData) { //do your stuff when selected @@ -435,22 +453,26 @@ namespace UnityEngine.UI.Extensions public readonly List boxes = new List(); } - + /* TEMPORARY FIX REMOVE IMAGES FROM POOL DELETE LATER SINCE CANNOT DESTROY */ - void Update() { - if (clearImages) { - for (int i = 0; i < culled_ImagesPool.Count; i++){ + void Update() + { + if (clearImages) + { + for (int i = 0; i < culled_ImagesPool.Count; i++) + { DestroyImmediate(culled_ImagesPool[i]); } culled_ImagesPool.Clear(); clearImages = false; } - if( previousText != text) - Reset_m_HrefInfos (); + if (previousText != text) + Reset_m_HrefInfos(); } - + // Reseting m_HrefInfos array if there is any change in text - void Reset_m_HrefInfos () { + void Reset_m_HrefInfos() + { previousText = text; m_HrefInfos.Clear(); isCreating_m_HrefInfos = true; diff --git a/Scripts/Effects/Gradient2.cs b/Scripts/Effects/Gradient2.cs new file mode 100644 index 0000000..6eec2d6 --- /dev/null +++ b/Scripts/Effects/Gradient2.cs @@ -0,0 +1,132 @@ +/// Credit Brad Nelson (playemgames - bitbucket) +/// Modified Gradient effect script from http://answers.unity3d.com/questions/1086415/gradient-text-in-unity-522-basevertexeffect-is-obs.html +/// +/// -Uses Unity's Gradient class to define the color +/// -Offset is now limited to -1,1 +/// -Multiple color blend modes +/// +/// Remember that the colors are applied per-vertex so if you have multiple points on your gradient where the color changes and there aren't enough vertices, you won't see all of the colors. +/// +using System.Collections.Generic; + +namespace UnityEngine.UI.Extensions +{ + [AddComponentMenu("UI/Effects/Extensions/Gradient2")] + public class Gradient2 : BaseMeshEffect { + [SerializeField] + Type _gradientType; + + [SerializeField] + Blend _blendMode = Blend.Multiply; + + [SerializeField] + [Range(-1, 1)] + float _offset = 0f; + + [SerializeField] + UnityEngine.Gradient _effectGradient = new UnityEngine.Gradient() { colorKeys = new GradientColorKey[] { new GradientColorKey(Color.black, 0), new GradientColorKey(Color.white, 1) } }; + + #region Properties + public Blend BlendMode { + get { return _blendMode; } + set { _blendMode = value; } + } + + public UnityEngine.Gradient EffectGradient { + get { return _effectGradient; } + set { _effectGradient = value; } + } + + public Type GradientType { + get { return _gradientType; } + set { _gradientType = value; } + } + + public float Offset { + get { return _offset; } + set { _offset = value; } + } + #endregion + + public override void ModifyMesh(VertexHelper helper) { + if(!IsActive() || helper.currentVertCount == 0) + return; + + List _vertexList = new List(); + + helper.GetUIVertexStream(_vertexList); + + int nCount = _vertexList.Count; + switch(GradientType) { + case Type.Horizontal: { + float left = _vertexList[0].position.x; + float right = _vertexList[0].position.x; + float x = 0f; + + for(int i = nCount - 1; i >= 1; --i) { + x = _vertexList[i].position.x; + + if(x > right) right = x; + else if(x < left) left = x; + } + + float width = 1f / (right - left); + UIVertex vertex = new UIVertex(); + + for(int i = 0; i < helper.currentVertCount; i++) { + helper.PopulateUIVertex(ref vertex, i); + + vertex.color = BlendColor(vertex.color, EffectGradient.Evaluate((vertex.position.x - left) * width - Offset)); + + helper.SetUIVertex(vertex, i); + } + } + break; + + case Type.Vertical: { + float bottom = _vertexList[0].position.y; + float top = _vertexList[0].position.y; + float y = 0f; + + for(int i = nCount - 1; i >= 1; --i) { + y = _vertexList[i].position.y; + + if(y > top) top = y; + else if(y < bottom) bottom = y; + } + + float height = 1f / (top - bottom); + UIVertex vertex = new UIVertex(); + + for(int i = 0; i < helper.currentVertCount; i++) { + helper.PopulateUIVertex(ref vertex, i); + + vertex.color = BlendColor(vertex.color, EffectGradient.Evaluate((vertex.position.y - bottom) * height - Offset)); + + helper.SetUIVertex(vertex, i); + } + } + break; + } + } + + Color BlendColor(Color colorA, Color colorB) { + switch(BlendMode) { + default: return colorB; + case Blend.Add: return colorA + colorB; + case Blend.Multiply: return colorA * colorB; + } + } + + public enum Type { + Horizontal, + Vertical + } + + public enum Blend { + Override, + Add, + Multiply + } + } +} \ No newline at end of file diff --git a/Scripts/Effects/Gradient2.cs.meta b/Scripts/Effects/Gradient2.cs.meta new file mode 100644 index 0000000..7bab72d --- /dev/null +++ b/Scripts/Effects/Gradient2.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 45facfdc6a639f041b007c036dc527b8 +timeCreated: 1479244798 +licenseType: Pro +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Scripts/Layout/HorizontalScrollSnap.cs b/Scripts/Layout/HorizontalScrollSnap.cs index dc4becc..59e773a 100644 --- a/Scripts/Layout/HorizontalScrollSnap.cs +++ b/Scripts/Layout/HorizontalScrollSnap.cs @@ -55,10 +55,10 @@ namespace UnityEngine.UI.Extensions [SerializeField] public int StartingScreen = 1; - [Tooltip("The distance between two pages based on page height, by default 3 times the width of the control")] + [Tooltip("The distance between two pages based on page height, by default pages are next to each other")] [SerializeField] - [Range(0, 8)] - public float PageStep = 0; + [Range(1, 8)] + public float PageStep = 1; public int CurrentPage { @@ -77,26 +77,19 @@ namespace UnityEngine.UI.Extensions public SelectionChangeEndEvent OnSelectionChangeEndEvent { get { return m_OnSelectionChangeEndEvent; } set { m_OnSelectionChangeEndEvent = value; } } // Use this for initialization - void Start() + void Awake() { _scroll_rect = gameObject.GetComponent(); if (_scroll_rect.horizontalScrollbar || _scroll_rect.verticalScrollbar) { - Debug.LogWarning("Warning, using scrollbors with the Scroll Snap controls is not advised as it causes unpredictable results"); + Debug.LogWarning("Warning, using scrollbars with the Scroll Snap controls is not advised as it causes unpredictable results"); } _screensContainer = _scroll_rect.content; DistributePages(); - _lerp = false; - _currentScreen = StartingScreen - 1; - - _scroll_rect.horizontalNormalizedPosition = (float)(_currentScreen) / (_screens - 1); - - ChangeBulletsInfo(_currentScreen); - if (NextButton) NextButton.GetComponent