Fixed and tested HSS & VSS use with Scroll COnflict Manager.

HSS & VSS controls now play nicely together
Added new version of Gradient

--HG--
branch : develop_5.3
release
Simon Jackson 2016-11-15 23:48:44 +00:00
parent 1730965a44
commit 76e5049023
5 changed files with 227 additions and 45 deletions

View File

@ -133,8 +133,12 @@ namespace UnityEngine.UI.Extensions
var go = DefaultControls.CreateImage(resources); var go = DefaultControls.CreateImage(resources);
go.layer = gameObject.layer; go.layer = gameObject.layer;
var rt = go.transform as RectTransform; var rt = go.transform as RectTransform;
if (rt) 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.SetParent(rectTransform);
rt.localPosition = Vector3.zero; rt.localPosition = Vector3.zero;
rt.localRotation = Quaternion.identity; rt.localRotation = Quaternion.identity;
@ -146,6 +150,7 @@ namespace UnityEngine.UI.Extensions
var spriteName = match.Groups[1].Value; var spriteName = match.Groups[1].Value;
//var size = float.Parse(match.Groups[2].Value); //var size = float.Parse(match.Groups[2].Value);
var img = m_ImagesPool[m_ImagesVertexIndex.Count - 1]; var img = m_ImagesPool[m_ImagesVertexIndex.Count - 1];
if (img.sprite == null || img.sprite.name != spriteName) if (img.sprite == null || img.sprite.name != spriteName)
{ {
// img.sprite = Resources.Load<Sprite>(spriteName); // img.sprite = Resources.Load<Sprite>(spriteName);
@ -181,7 +186,8 @@ namespace UnityEngine.UI.Extensions
m_ImagesPool.Remove(m_ImagesPool[i]); m_ImagesPool.Remove(m_ImagesPool[i]);
} }
} }
if (culled_ImagesPool.Count > 1) { if (culled_ImagesPool.Count > 1)
{
clearImages = true; clearImages = true;
} }
} }
@ -195,11 +201,13 @@ namespace UnityEngine.UI.Extensions
positions.Clear(); positions.Clear();
UIVertex vert = new UIVertex(); UIVertex vert = new UIVertex();
for (var i = 0; i < m_ImagesVertexIndex.Count; i++) for (var i = 0; i < m_ImagesVertexIndex.Count; i++)
{ {
var endIndex = m_ImagesVertexIndex[i]; var endIndex = m_ImagesVertexIndex[i];
var rt = m_ImagesPool[i].rectTransform; var rt = m_ImagesPool[i].rectTransform;
var size = rt.sizeDelta; var size = rt.sizeDelta;
if (endIndex < toFill.currentVertCount) if (endIndex < toFill.currentVertCount)
{ {
toFill.PopulateUIVertex(ref vert, endIndex); toFill.PopulateUIVertex(ref vert, endIndex);
@ -207,7 +215,9 @@ namespace UnityEngine.UI.Extensions
// Erase the lower left corner of the black specks // Erase the lower left corner of the black specks
toFill.PopulateUIVertex(ref vert, endIndex - 3); toFill.PopulateUIVertex(ref vert, endIndex - 3);
var pos = vert.position; var pos = vert.position;
for (int j = endIndex, m = endIndex - 3; j > m; j--) for (int j = endIndex, m = endIndex - 3; j > m; j--)
{ {
toFill.PopulateUIVertex(ref vert, endIndex); 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 // Hyperlink inside the text is added to surround the vertex index coordinate frame
toFill.PopulateUIVertex(ref vert, hrefInfo.startIndex); toFill.PopulateUIVertex(ref vert, hrefInfo.startIndex);
var pos = vert.position; var pos = vert.position;
var bounds = new Bounds(pos, Vector3.zero); var bounds = new Bounds(pos, Vector3.zero);
for (int i = hrefInfo.startIndex, m = hrefInfo.endIndex; i < m; i++) for (int i = hrefInfo.startIndex, m = hrefInfo.endIndex; i < m; i++)
{ {
if (i >= toFill.currentVertCount) if (i >= toFill.currentVertCount)
@ -311,13 +323,16 @@ namespace UnityEngine.UI.Extensions
} }
} }
int count = 0; int count = 0;
foreach (Match match in s_HrefRegex.Matches(fixedString)) foreach (Match match in s_HrefRegex.Matches(fixedString))
{ {
s_TextBuilder.Append(fixedString.Substring(indexText, match.Index - indexText)); s_TextBuilder.Append(fixedString.Substring(indexText, match.Index - indexText));
s_TextBuilder.Append("<color=" + hyperlinkColor + ">"); // Hyperlink color s_TextBuilder.Append("<color=" + hyperlinkColor + ">"); // Hyperlink color
var group = match.Groups[1]; var group = match.Groups[1];
if(isCreating_m_HrefInfos) {
if (isCreating_m_HrefInfos)
{
var hrefInfo = new HrefInfo var hrefInfo = new HrefInfo
{ {
startIndex = s_TextBuilder.Length * 4, // Hyperlinks in text starting vertex indices startIndex = s_TextBuilder.Length * 4, // Hyperlinks in text starting vertex indices
@ -325,8 +340,10 @@ namespace UnityEngine.UI.Extensions
name = group.Value name = group.Value
}; };
m_HrefInfos.Add(hrefInfo); 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].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; m_HrefInfos[count].endIndex = (s_TextBuilder.Length + match.Groups[2].Length - 1) * 4 + 3;
count++; count++;
@ -359,6 +376,7 @@ namespace UnityEngine.UI.Extensions
foreach (var hrefInfo in m_HrefInfos) foreach (var hrefInfo in m_HrefInfos)
{ {
var boxes = hrefInfo.boxes; var boxes = hrefInfo.boxes;
for (var i = 0; i < boxes.Count; ++i) for (var i = 0; i < boxes.Count; ++i)
{ {
if (boxes[i].Contains(lp)) if (boxes[i].Contains(lp))
@ -388,7 +406,6 @@ namespace UnityEngine.UI.Extensions
public void OnPointerExit(PointerEventData eventData) public void OnPointerExit(PointerEventData eventData)
{ {
//do your stuff when highlighted //do your stuff when highlighted
//selected = false; //selected = false;
if (m_ImagesPool.Count >= 1) if (m_ImagesPool.Count >= 1)
@ -406,6 +423,7 @@ namespace UnityEngine.UI.Extensions
} }
} }
} }
public void OnSelect(BaseEventData eventData) public void OnSelect(BaseEventData eventData)
{ {
//do your stuff when selected //do your stuff when selected
@ -437,9 +455,12 @@ namespace UnityEngine.UI.Extensions
} }
/* TEMPORARY FIX REMOVE IMAGES FROM POOL DELETE LATER SINCE CANNOT DESTROY */ /* TEMPORARY FIX REMOVE IMAGES FROM POOL DELETE LATER SINCE CANNOT DESTROY */
void Update() { void Update()
if (clearImages) { {
for (int i = 0; i < culled_ImagesPool.Count; i++){ if (clearImages)
{
for (int i = 0; i < culled_ImagesPool.Count; i++)
{
DestroyImmediate(culled_ImagesPool[i]); DestroyImmediate(culled_ImagesPool[i]);
} }
culled_ImagesPool.Clear(); culled_ImagesPool.Clear();
@ -450,7 +471,8 @@ namespace UnityEngine.UI.Extensions
} }
// Reseting m_HrefInfos array if there is any change in text // Reseting m_HrefInfos array if there is any change in text
void Reset_m_HrefInfos () { void Reset_m_HrefInfos()
{
previousText = text; previousText = text;
m_HrefInfos.Clear(); m_HrefInfos.Clear();
isCreating_m_HrefInfos = true; isCreating_m_HrefInfos = true;

View File

@ -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
/// <summary>
/// -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.
/// </summary>
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<UIVertex> _vertexList = new List<UIVertex>();
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
}
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 45facfdc6a639f041b007c036dc527b8
timeCreated: 1479244798
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -55,10 +55,10 @@ namespace UnityEngine.UI.Extensions
[SerializeField] [SerializeField]
public int StartingScreen = 1; 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] [SerializeField]
[Range(0, 8)] [Range(1, 8)]
public float PageStep = 0; public float PageStep = 1;
public int CurrentPage public int CurrentPage
{ {
@ -77,26 +77,19 @@ namespace UnityEngine.UI.Extensions
public SelectionChangeEndEvent OnSelectionChangeEndEvent { get { return m_OnSelectionChangeEndEvent; } set { m_OnSelectionChangeEndEvent = value; } } public SelectionChangeEndEvent OnSelectionChangeEndEvent { get { return m_OnSelectionChangeEndEvent; } set { m_OnSelectionChangeEndEvent = value; } }
// Use this for initialization // Use this for initialization
void Start() void Awake()
{ {
_scroll_rect = gameObject.GetComponent<ScrollRect>(); _scroll_rect = gameObject.GetComponent<ScrollRect>();
if (_scroll_rect.horizontalScrollbar || _scroll_rect.verticalScrollbar) 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; _screensContainer = _scroll_rect.content;
DistributePages(); DistributePages();
_lerp = false;
_currentScreen = StartingScreen - 1;
_scroll_rect.horizontalNormalizedPosition = (float)(_currentScreen) / (_screens - 1);
ChangeBulletsInfo(_currentScreen);
if (NextButton) if (NextButton)
NextButton.GetComponent<Button>().onClick.AddListener(() => { NextScreen(); }); NextButton.GetComponent<Button>().onClick.AddListener(() => { NextScreen(); });
@ -104,12 +97,23 @@ namespace UnityEngine.UI.Extensions
PrevButton.GetComponent<Button>().onClick.AddListener(() => { PreviousScreen(); }); PrevButton.GetComponent<Button>().onClick.AddListener(() => { PreviousScreen(); });
} }
void Start()
{
UpdateChildPositions();
_lerp = false;
_currentScreen = StartingScreen - 1;
_scroll_rect.horizontalNormalizedPosition = (float)(_currentScreen) / (_screens - 1);
ChangeBulletsInfo(_currentScreen);
}
void Update() void Update()
{ {
if (_lerp) if (_lerp)
{ {
_screensContainer.localPosition = Vector3.Lerp(_screensContainer.localPosition, _lerp_target, transitionSpeed * Time.deltaTime); _screensContainer.localPosition = Vector3.Lerp(_screensContainer.localPosition, _lerp_target, transitionSpeed * Time.deltaTime);
if (Vector3.Distance(_screensContainer.localPosition, _lerp_target) < 1f) if (Vector3.Distance(_screensContainer.localPosition, _lerp_target) < 0.1f)
{ {
_lerp = false; _lerp = false;
@ -270,7 +274,10 @@ namespace UnityEngine.UI.Extensions
_dimension = currentXPosition + _offset * -1; _dimension = currentXPosition + _offset * -1;
_screensContainer.GetComponent<RectTransform>().offsetMax = new Vector2(_dimension, 0f); _screensContainer.GetComponent<RectTransform>().offsetMax = new Vector2(_dimension, 0f);
}
void UpdateChildPositions()
{
_screens = _screensContainer.childCount; _screens = _screensContainer.childCount;
_positions = new System.Collections.Generic.List<Vector3>(); _positions = new System.Collections.Generic.List<Vector3>();

View File

@ -54,10 +54,10 @@ namespace UnityEngine.UI.Extensions
[SerializeField] [SerializeField]
public int StartingScreen = 1; 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] [SerializeField]
[Range(0, 8)] [Range(1, 8)]
public float PageStep = 0; public float PageStep = 1;
public int CurrentPage public int CurrentPage
{ {
@ -76,26 +76,19 @@ namespace UnityEngine.UI.Extensions
public SelectionChangeEndEvent OnSelectionChangeEndEvent { get { return m_OnSelectionChangeEndEvent; } set { m_OnSelectionChangeEndEvent = value; } } public SelectionChangeEndEvent OnSelectionChangeEndEvent { get { return m_OnSelectionChangeEndEvent; } set { m_OnSelectionChangeEndEvent = value; } }
// Use this for initialization // Use this for initialization
void Start() void Awake()
{ {
_scroll_rect = gameObject.GetComponent<ScrollRect>(); _scroll_rect = gameObject.GetComponent<ScrollRect>();
if (_scroll_rect.horizontalScrollbar || _scroll_rect.verticalScrollbar) 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; _screensContainer = _scroll_rect.content;
DistributePages(); DistributePages();
_lerp = false;
_currentScreen = StartingScreen - 1;
_scroll_rect.verticalNormalizedPosition = (float)(_currentScreen) / (float)(_screens - 1);
ChangeBulletsInfo(_currentScreen);
if (NextButton) if (NextButton)
NextButton.GetComponent<Button>().onClick.AddListener(() => { NextScreen(); }); NextButton.GetComponent<Button>().onClick.AddListener(() => { NextScreen(); });
@ -103,12 +96,23 @@ namespace UnityEngine.UI.Extensions
PrevButton.GetComponent<Button>().onClick.AddListener(() => { PreviousScreen(); }); PrevButton.GetComponent<Button>().onClick.AddListener(() => { PreviousScreen(); });
} }
void Start()
{
UpdateChildPositions();
_lerp = false;
_currentScreen = StartingScreen - 1;
_scroll_rect.verticalNormalizedPosition = (float)(_currentScreen) / (float)(_screens - 1);
ChangeBulletsInfo(_currentScreen);
}
void Update() void Update()
{ {
if (_lerp) if (_lerp)
{ {
_screensContainer.localPosition = Vector3.Lerp(_screensContainer.localPosition, _lerp_target, transitionSpeed * Time.deltaTime); _screensContainer.localPosition = Vector3.Lerp(_screensContainer.localPosition, _lerp_target, transitionSpeed * Time.deltaTime);
if (Vector3.Distance(_screensContainer.localPosition, _lerp_target) < 1f) if (Vector3.Distance(_screensContainer.localPosition, _lerp_target) < 0.1f)
{ {
_lerp = false; _lerp = false;
EndScreenChange(); EndScreenChange();
@ -268,7 +272,10 @@ namespace UnityEngine.UI.Extensions
_dimension = currentYPosition + _offset * -1; _dimension = currentYPosition + _offset * -1;
_screensContainer.GetComponent<RectTransform>().offsetMax = new Vector2(0f, _dimension); _screensContainer.GetComponent<RectTransform>().offsetMax = new Vector2(0f, _dimension);
}
void UpdateChildPositions()
{
_screens = _screensContainer.childCount; _screens = _screensContainer.childCount;
_positions = new System.Collections.Generic.List<Vector3>(); _positions = new System.Collections.Generic.List<Vector3>();
@ -283,6 +290,7 @@ namespace UnityEngine.UI.Extensions
} }
} }
int GetPageforPosition(Vector3 pos) int GetPageforPosition(Vector3 pos)
{ {
for (int i = 0; i < _positions.Count; i++) for (int i = 0; i < _positions.Count; i++)
@ -377,6 +385,7 @@ namespace UnityEngine.UI.Extensions
public void OnEndDrag(PointerEventData eventData) public void OnEndDrag(PointerEventData eventData)
{ {
//transform.GetComponent<RectTransform>().pivot = new Vector2(0.5f, 0.5f);
_startDrag = true; _startDrag = true;
if (_scroll_rect.vertical) if (_scroll_rect.vertical)
{ {