2015-10-02 02:29:58 +08:00
|
|
|
/// Credit zero3growlithe
|
|
|
|
/// sourced from: http://forum.unity3d.com/threads/scripts-useful-4-6-scripts-collection.264161/page-2#post-2011648
|
|
|
|
/// Update by xesenix - based on UIScrollToSelection centers on selected element in scrollrect which can move in XY
|
|
|
|
/// you can restrict movement by locking axis on ScrollRect component
|
|
|
|
|
|
|
|
/*USAGE:
|
|
|
|
Simply place the script on the ScrollRect that contains the selectable children we'll be scroling to
|
|
|
|
and drag'n'drop the RectTransform of the options "container" that we'll be scrolling.*/
|
|
|
|
|
|
|
|
using UnityEngine.EventSystems;
|
|
|
|
|
|
|
|
namespace UnityEngine.UI.Extensions
|
|
|
|
{
|
2017-06-26 05:36:09 +08:00
|
|
|
[AddComponentMenu("UI/Extensions/UI ScrollTo Selection XY")]
|
2015-10-02 02:29:58 +08:00
|
|
|
[RequireComponent(typeof(ScrollRect))]
|
|
|
|
public class UIScrollToSelectionXY : MonoBehaviour
|
|
|
|
{
|
|
|
|
|
|
|
|
#region Variables
|
|
|
|
|
|
|
|
// settings
|
|
|
|
public float scrollSpeed = 10f;
|
|
|
|
|
|
|
|
[SerializeField]
|
2018-01-20 20:08:44 +08:00
|
|
|
private RectTransform layoutListGroup = null;
|
2015-10-02 02:29:58 +08:00
|
|
|
|
|
|
|
// temporary variables
|
|
|
|
private RectTransform targetScrollObject;
|
|
|
|
private bool scrollToSelection = true;
|
|
|
|
|
|
|
|
// references
|
2019-03-08 02:11:51 +08:00
|
|
|
private RectTransform scrollWindow = null;
|
|
|
|
private ScrollRect targetScrollRect = null;
|
2015-10-02 02:29:58 +08:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
// Use this for initialization
|
|
|
|
private void Start()
|
|
|
|
{
|
|
|
|
targetScrollRect = GetComponent<ScrollRect>();
|
|
|
|
scrollWindow = targetScrollRect.GetComponent<RectTransform>();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
private void Update()
|
|
|
|
{
|
|
|
|
ScrollRectToLevelSelection();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void ScrollRectToLevelSelection()
|
|
|
|
{
|
|
|
|
// FIX: if you dont do that here events can have null value
|
|
|
|
var events = EventSystem.current;
|
|
|
|
|
|
|
|
// check main references
|
|
|
|
bool referencesAreIncorrect =
|
|
|
|
(targetScrollRect == null || layoutListGroup == null || scrollWindow == null);
|
|
|
|
if (referencesAreIncorrect == true)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get calculation references
|
|
|
|
RectTransform selection = events.currentSelectedGameObject != null ?
|
|
|
|
events.currentSelectedGameObject.GetComponent<RectTransform>() :
|
|
|
|
null;
|
|
|
|
|
|
|
|
if (selection != targetScrollObject)
|
|
|
|
{
|
|
|
|
scrollToSelection = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if scrolling is possible
|
|
|
|
bool isScrollDirectionUnknown = (selection == null || scrollToSelection == false);
|
|
|
|
|
|
|
|
if (isScrollDirectionUnknown == true || selection.transform.parent != layoutListGroup.transform)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool finishedX = false, finishedY = false;
|
|
|
|
|
|
|
|
if (targetScrollRect.vertical)
|
|
|
|
{
|
|
|
|
// move the current scroll rect to correct position
|
|
|
|
float selectionPos = -selection.anchoredPosition.y;
|
|
|
|
|
|
|
|
float listPixelAnchor = layoutListGroup.anchoredPosition.y;
|
|
|
|
|
|
|
|
// get the element offset value depending on the cursor move direction
|
|
|
|
float offlimitsValue = 0;
|
|
|
|
|
|
|
|
offlimitsValue = listPixelAnchor - selectionPos;
|
|
|
|
// move the target scroll rect
|
|
|
|
targetScrollRect.verticalNormalizedPosition += (offlimitsValue / layoutListGroup.sizeDelta.y) * Time.deltaTime * scrollSpeed;
|
|
|
|
|
|
|
|
finishedY = Mathf.Abs(offlimitsValue) < 2f;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (targetScrollRect.horizontal)
|
|
|
|
{
|
|
|
|
// move the current scroll rect to correct position
|
|
|
|
float selectionPos = -selection.anchoredPosition.x;
|
|
|
|
|
|
|
|
float listPixelAnchor = layoutListGroup.anchoredPosition.x;
|
|
|
|
|
|
|
|
// get the element offset value depending on the cursor move direction
|
|
|
|
float offlimitsValue = 0;
|
|
|
|
|
|
|
|
offlimitsValue = listPixelAnchor - selectionPos;
|
|
|
|
// move the target scroll rect
|
|
|
|
targetScrollRect.horizontalNormalizedPosition += (offlimitsValue / layoutListGroup.sizeDelta.x) * Time.deltaTime * scrollSpeed;
|
|
|
|
|
|
|
|
finishedX = Mathf.Abs(offlimitsValue) < 2f;
|
|
|
|
}
|
|
|
|
// check if we reached our destination
|
|
|
|
if (finishedX && finishedY) {
|
|
|
|
scrollToSelection = false;
|
|
|
|
}
|
|
|
|
// save last object we were "heading to" to prevent blocking
|
|
|
|
targetScrollObject = selection;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|