Minor VSS/HSS bugfixes and enhancements, resolving #114

release
Simon Jackson 2017-03-12 15:18:36 +00:00
parent ec31c8b5d6
commit f89702ddc6
3 changed files with 72 additions and 41 deletions

View File

@ -37,6 +37,7 @@ namespace UnityEngine.UI.Extensions
_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) < 0.1f) if (Vector3.Distance(_screensContainer.localPosition, _lerp_target) < 0.1f)
{ {
_screensContainer.localPosition = _lerp_target;
_lerp = false; _lerp = false;
EndScreenChange(); EndScreenChange();
} }
@ -192,6 +193,16 @@ namespace UnityEngine.UI.Extensions
} }
} }
private void OnEnable()
{
InitialiseChildObjects();
DistributePages();
if (MaskArea) UpdateVisible();
if(JumpOnEnable) SetScrollContainerPosition();
GoToScreen(StartingScreen);
}
#region Interfaces #region Interfaces
/// <summary> /// <summary>
/// Release screen to swipe /// Release screen to swipe

View File

@ -90,7 +90,10 @@ namespace UnityEngine.UI.Extensions
} }
} }
[Tooltip("(Experimental)\nBy default, child array objects will use the parent transform\nHowever you can disable this for some interesting effects")] [Tooltip("By default the container will lerp to the start when enabled in the scene, this option overrides this and forces it to simply jump without lerping")]
public bool JumpOnEnable = false;
[Tooltip("(Experimental)\nBy default, child array objects will use the parent transform\nHowever you can disable this for some interesting effects")]
public bool UseParentTransform = true; public bool UseParentTransform = true;
[Tooltip("Scroll Snap children. (optional)\nEither place objects in the scene as children OR\nPrefabs in this array, NOT BOTH")] [Tooltip("Scroll Snap children. (optional)\nEither place objects in the scene as children OR\nPrefabs in this array, NOT BOTH")]
@ -112,7 +115,7 @@ 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 Awake() void Awake()
{ {
_scroll_rect = gameObject.GetComponent<ScrollRect>(); _scroll_rect = gameObject.GetComponent<ScrollRect>();
@ -127,20 +130,8 @@ namespace UnityEngine.UI.Extensions
} }
_screensContainer = _scroll_rect.content; _screensContainer = _scroll_rect.content;
if (ChildObjects != null && ChildObjects.Length > 0)
{
if (_screensContainer.transform.childCount > 0)
{
Debug.LogError("ScrollRect Content has children, this is not supported when using managed Child Objects\n Either remove the ScrollRect Content children or clear the ChildObjects array");
return;
}
InitialiseChildObjectsFromArray(); InitialiseChildObjects();
}
else
{
InitialiseChildObjectsFromScene();
}
if (NextButton) if (NextButton)
NextButton.GetComponent<Button>().onClick.AddListener(() => { NextScreen(); }); NextButton.GetComponent<Button>().onClick.AddListener(() => { NextScreen(); });
@ -149,7 +140,25 @@ namespace UnityEngine.UI.Extensions
PrevButton.GetComponent<Button>().onClick.AddListener(() => { PreviousScreen(); }); PrevButton.GetComponent<Button>().onClick.AddListener(() => { PreviousScreen(); });
} }
internal void InitialiseChildObjectsFromScene() internal void InitialiseChildObjects()
{
if (ChildObjects != null && ChildObjects.Length > 0)
{
if (_screensContainer.transform.childCount > 0)
{
Debug.LogError("ScrollRect Content has children, this is not supported when using managed Child Objects\n Either remove the ScrollRect Content children or clear the ChildObjects array");
return;
}
InitialiseChildObjectsFromArray();
}
else
{
InitialiseChildObjectsFromScene();
}
}
internal void InitialiseChildObjectsFromScene()
{ {
int childCount = _screensContainer.childCount; int childCount = _screensContainer.childCount;
ChildObjects = new GameObject[childCount]; ChildObjects = new GameObject[childCount];
@ -172,7 +181,7 @@ namespace UnityEngine.UI.Extensions
{ {
child = GameObject.Instantiate(ChildObjects[i]); child = GameObject.Instantiate(ChildObjects[i]);
//Optionally, use original GO transform when initialising, by default will use parent RectTransform position/rotation //Optionally, use original GO transform when initialising, by default will use parent RectTransform position/rotation
if (UseParentTransform) if (UseParentTransform)
{ {
childRect = child.GetComponent<RectTransform>(); childRect = child.GetComponent<RectTransform>();
childRect.rotation = _screensContainer.rotation; childRect.rotation = _screensContainer.rotation;
@ -192,7 +201,7 @@ namespace UnityEngine.UI.Extensions
internal void UpdateVisible() internal void UpdateVisible()
{ {
//If there are no objects in the scene or a mask, exit //If there are no objects in the scene or a mask, exit
if (!MaskArea || ChildObjects == null || ChildObjects.Length < 1 || _screensContainer.childCount < 1) if (!MaskArea || ChildObjects == null || ChildObjects.Length < 1 || _screensContainer.childCount < 1)
{ {
return; return;
} }
@ -201,21 +210,21 @@ namespace UnityEngine.UI.Extensions
_halfNoVisibleItems = (int)Math.Round(_maskSize / (_childSize * MaskBuffer), MidpointRounding.AwayFromZero) / 2; _halfNoVisibleItems = (int)Math.Round(_maskSize / (_childSize * MaskBuffer), MidpointRounding.AwayFromZero) / 2;
_bottomItem = _topItem = 0; _bottomItem = _topItem = 0;
//work out how many items below the current page can be visible //work out how many items below the current page can be visible
for (int i = _halfNoVisibleItems + 1; i > 0; i--) for (int i = _halfNoVisibleItems + 1; i > 0; i--)
{ {
_bottomItem = _currentPage - i < 0 ? 0 : i; _bottomItem = _currentPage - i < 0 ? 0 : i;
if (_bottomItem > 0) break; if (_bottomItem > 0) break;
} }
//work out how many items above the current page can be visible //work out how many items above the current page can be visible
for (int i = _halfNoVisibleItems + 1; i > 0; i--) for (int i = _halfNoVisibleItems + 1; i > 0; i--)
{ {
_topItem = _screensContainer.childCount - _currentPage - i < 0 ? 0 : i; _topItem = _screensContainer.childCount - _currentPage - i < 0 ? 0 : i;
if (_topItem > 0) break; if (_topItem > 0) break;
} }
//Set the active items active //Set the active items active
for (int i = CurrentPage - _bottomItem; i < CurrentPage + _topItem; i++) for (int i = CurrentPage - _bottomItem; i < CurrentPage + _topItem; i++)
{ {
try try
{ {
@ -234,7 +243,7 @@ namespace UnityEngine.UI.Extensions
} }
//Function for switching screens with buttons //Function for switching screens with buttons
public void NextScreen() public void NextScreen()
{ {
if (_currentPage < _screens - 1) if (_currentPage < _screens - 1)
{ {
@ -248,7 +257,7 @@ namespace UnityEngine.UI.Extensions
} }
//Function for switching screens with buttons //Function for switching screens with buttons
public void PreviousScreen() public void PreviousScreen()
{ {
if (_currentPage > 0) if (_currentPage > 0)
{ {
@ -266,7 +275,7 @@ namespace UnityEngine.UI.Extensions
/// *Note, this is based on a 0 starting index - 0 to x /// *Note, this is based on a 0 starting index - 0 to x
/// </summary> /// </summary>
/// <param name="screenIndex">0 starting index of page to jump to</param> /// <param name="screenIndex">0 starting index of page to jump to</param>
public void GoToScreen(int screenIndex) public void GoToScreen(int screenIndex)
{ {
if (screenIndex <= _screens - 1 && screenIndex >= 0) if (screenIndex <= _screens - 1 && screenIndex >= 0)
{ {
@ -284,11 +293,11 @@ namespace UnityEngine.UI.Extensions
/// </summary> /// </summary>
/// <param name="pos">Position to test, normally the Scroll Rect container Local position</param> /// <param name="pos">Position to test, normally the Scroll Rect container Local position</param>
/// <returns>Closest Page number (zero indexed array value)</returns> /// <returns>Closest Page number (zero indexed array value)</returns>
internal int GetPageforPosition(Vector3 pos) internal int GetPageforPosition(Vector3 pos)
{ {
return _isVertical ? return _isVertical ?
-(int)Math.Round((pos.y - _scrollStartPosition) / _childSize) : -(int)Math.Round((pos.y - _scrollStartPosition) / _childSize) :
-(int)Math.Round((pos.x - _scrollStartPosition) / _childSize); -(int)Math.Round((pos.x - _scrollStartPosition) / _childSize);
} }
/// <summary> /// <summary>
@ -296,11 +305,11 @@ namespace UnityEngine.UI.Extensions
/// </summary> /// </summary>
/// <param name="pos">Position to test, normally the Scroll Rect container Local position</param> /// <param name="pos">Position to test, normally the Scroll Rect container Local position</param>
/// <returns>True / False, is the position in the bounds of a page</returns> /// <returns>True / False, is the position in the bounds of a page</returns>
internal bool IsRectSettledOnaPage(Vector3 pos) internal bool IsRectSettledOnaPage(Vector3 pos)
{ {
return _isVertical ? return _isVertical ?
-((pos.y - _scrollStartPosition) / _childSize) == -(int)Math.Round((pos.y - _scrollStartPosition) / _childSize) : -((pos.y - _scrollStartPosition) / _childSize) == -(int)Math.Round((pos.y - _scrollStartPosition) / _childSize) :
-((pos.x - _scrollStartPosition) / _childSize) == -(int)Math.Round((pos.x - _scrollStartPosition) / _childSize); -((pos.x - _scrollStartPosition) / _childSize) == -(int)Math.Round((pos.x - _scrollStartPosition) / _childSize);
} }
/// <summary> /// <summary>
@ -308,7 +317,7 @@ namespace UnityEngine.UI.Extensions
/// </summary> /// </summary>
/// <param name="page">Page that the position is required for (Zero indexed array value)</param> /// <param name="page">Page that the position is required for (Zero indexed array value)</param>
/// <param name="target">Outputs the local position for the selected page</param> /// <param name="target">Outputs the local position for the selected page</param>
internal void GetPositionforPage(int page, ref Vector3 target) internal void GetPositionforPage(int page, ref Vector3 target)
{ {
_childPos = -_childSize * page; _childPos = -_childSize * page;
if (_isVertical) if (_isVertical)
@ -324,7 +333,7 @@ namespace UnityEngine.UI.Extensions
/// <summary> /// <summary>
/// Updates the _Lerp target to the closest page and updates the pagination bullets. Each control's update loop will then handle the move. /// Updates the _Lerp target to the closest page and updates the pagination bullets. Each control's update loop will then handle the move.
/// </summary> /// </summary>
internal void ScrollToClosestElement() internal void ScrollToClosestElement()
{ {
_lerp = true; _lerp = true;
CurrentPage = GetPageforPosition(_screensContainer.localPosition); CurrentPage = GetPageforPosition(_screensContainer.localPosition);
@ -335,7 +344,7 @@ namespace UnityEngine.UI.Extensions
/// <summary> /// <summary>
/// notifies pagination indicator and navigation buttons of a screen change /// notifies pagination indicator and navigation buttons of a screen change
/// </summary> /// </summary>
internal void OnCurrentScreenChange(int currentScreen) internal void OnCurrentScreenChange(int currentScreen)
{ {
ChangeBulletsInfo(currentScreen); ChangeBulletsInfo(currentScreen);
ToggleNavigationButtons(currentScreen); ToggleNavigationButtons(currentScreen);
@ -345,14 +354,14 @@ namespace UnityEngine.UI.Extensions
/// changes the bullets on the bottom of the page - pagination /// changes the bullets on the bottom of the page - pagination
/// </summary> /// </summary>
/// <param name="targetScreen"></param> /// <param name="targetScreen"></param>
private void ChangeBulletsInfo(int targetScreen) private void ChangeBulletsInfo(int targetScreen)
{ {
if (Pagination) if (Pagination)
for (int i = 0; i < Pagination.transform.childCount; i++) for (int i = 0; i < Pagination.transform.childCount; i++)
{ {
Pagination.transform.GetChild(i).GetComponent<Toggle>().isOn = (targetScreen == i) Pagination.transform.GetChild(i).GetComponent<Toggle>().isOn = (targetScreen == i)
? true ? true
: false; : false;
} }
} }
@ -360,7 +369,7 @@ namespace UnityEngine.UI.Extensions
/// disables the page navigation buttons when at the first or last screen /// disables the page navigation buttons when at the first or last screen
/// </summary> /// </summary>
/// <param name="targetScreen"></param> /// <param name="targetScreen"></param>
private void ToggleNavigationButtons(int targetScreen) { private void ToggleNavigationButtons(int targetScreen) {
if (PrevButton) { if (PrevButton) {
PrevButton.GetComponent<Button>().interactable = targetScreen > 0; PrevButton.GetComponent<Button>().interactable = targetScreen > 0;
} }
@ -406,7 +415,7 @@ namespace UnityEngine.UI.Extensions
/// <summary> /// <summary>
/// Event fires when the user starts to change the page, either via swipe or button. /// Event fires when the user starts to change the page, either via swipe or button.
/// </summary> /// </summary>
internal void StartScreenChange() internal void StartScreenChange()
{ {
OnSelectionChangeStartEvent.Invoke(); OnSelectionChangeStartEvent.Invoke();
} }
@ -414,7 +423,7 @@ namespace UnityEngine.UI.Extensions
/// <summary> /// <summary>
/// Event fires when the currently viewed page changes, also updates while the scroll is moving /// Event fires when the currently viewed page changes, also updates while the scroll is moving
/// </summary> /// </summary>
internal void ScreenChange() internal void ScreenChange()
{ {
OnSelectionPageChangedEvent.Invoke(_currentPage); OnSelectionPageChangedEvent.Invoke(_currentPage);
} }
@ -422,7 +431,7 @@ namespace UnityEngine.UI.Extensions
/// <summary> /// <summary>
/// Event fires when control settles on a page, outputs the new page number /// Event fires when control settles on a page, outputs the new page number
/// </summary> /// </summary>
internal void EndScreenChange() internal void EndScreenChange()
{ {
OnSelectionChangeEndEvent.Invoke(_currentPage); OnSelectionChangeEndEvent.Invoke(_currentPage);
_settled = true; _settled = true;
@ -433,7 +442,7 @@ namespace UnityEngine.UI.Extensions
/// Touch screen to start swiping /// Touch screen to start swiping
/// </summary> /// </summary>
/// <param name="eventData"></param> /// <param name="eventData"></param>
public void OnBeginDrag(PointerEventData eventData) public void OnBeginDrag(PointerEventData eventData)
{ {
_pointerDown = true; _pointerDown = true;
_settled = false; _settled = false;
@ -445,7 +454,7 @@ namespace UnityEngine.UI.Extensions
/// While dragging do /// While dragging do
/// </summary> /// </summary>
/// <param name="eventData"></param> /// <param name="eventData"></param>
public void OnDrag(PointerEventData eventData) public void OnDrag(PointerEventData eventData)
{ {
_lerp = false; _lerp = false;
} }

View File

@ -37,6 +37,7 @@ namespace UnityEngine.UI.Extensions
_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) < 0.1f) if (Vector3.Distance(_screensContainer.localPosition, _lerp_target) < 0.1f)
{ {
_screensContainer.localPosition = _lerp_target;
_lerp = false; _lerp = false;
EndScreenChange(); EndScreenChange();
} }
@ -192,6 +193,16 @@ namespace UnityEngine.UI.Extensions
} }
} }
private void OnEnable()
{
InitialiseChildObjects();
DistributePages();
if (MaskArea) UpdateVisible();
if (JumpOnEnable) SetScrollContainerPosition();
GoToScreen(StartingScreen);
}
#region Interfaces #region Interfaces
/// <summary> /// <summary>
/// Release screen to swipe /// Release screen to swipe