Started 5.1 branch (supports 5.0-5.1)
Added new selection box control. Ready for merging 5.x fixes --HG-- branch : develop-Unity5.1pull/413/head
parent
9584677329
commit
fddbbd0066
|
@ -84,9 +84,9 @@ namespace UnityEngine.UI.Extensions
|
|||
{
|
||||
image.color = color;
|
||||
}
|
||||
else if (renderer)
|
||||
else if (GetComponent<UnityEngine.Renderer>())
|
||||
{
|
||||
renderer.material.color = color;
|
||||
GetComponent<UnityEngine.Renderer>().material.color = color;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -235,7 +235,7 @@ namespace UnityEngine.UI.Extensions
|
|||
} else {
|
||||
//If it doesn't have a rectTransform, we need to get the radius so we can use it as an area around the center to detect a click.
|
||||
//This works because a 2D or 3D renderer will both return a radius
|
||||
var radius = selectable.transform.renderer.bounds.extents.magnitude;
|
||||
var radius = selectable.transform.GetComponent<UnityEngine.Renderer>().bounds.extents.magnitude;
|
||||
|
||||
var selectableScreenPoint = GetScreenPointOfSelectable(selectable);
|
||||
|
||||
|
|
|
@ -2,105 +2,103 @@
|
|||
|
||||
/*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;
|
||||
using System.Collections;
|
||||
using UnityEngine.UI;
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
public class UIScrollToSelection : MonoBehaviour {
|
||||
|
||||
/* ### VARIABLES ============================================================== */
|
||||
|
||||
// settings
|
||||
public float scrollSpeed = 10f;
|
||||
|
||||
[SerializeField]
|
||||
private RectTransform layoutListGroup;
|
||||
|
||||
// temporary variables
|
||||
private RectTransform targetScrollObject;
|
||||
private bool scrollToSelection = true;
|
||||
|
||||
// references
|
||||
private RectTransform scrollWindow;
|
||||
private RectTransform currentCanvas;
|
||||
private ScrollRect targetScrollRect;
|
||||
|
||||
|
||||
/* ### MAIN METHODS =========================================================== */
|
||||
// Use this for initialization
|
||||
private void Start () {
|
||||
targetScrollRect = GetComponent<ScrollRect>();
|
||||
scrollWindow = targetScrollRect.GetComponent<RectTransform>();
|
||||
currentCanvas = transform.root.GetComponent<RectTransform>();
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
private void Update () {
|
||||
ScrollRectToLevelSelection();
|
||||
}
|
||||
|
||||
private void ScrollRectToLevelSelection (){
|
||||
// check main references
|
||||
bool referencesAreIncorrect =
|
||||
(targetScrollRect == null || layoutListGroup == null || scrollWindow == null);
|
||||
if (referencesAreIncorrect == true){
|
||||
return;
|
||||
}
|
||||
|
||||
// get calculation references
|
||||
EventSystem events = EventSystem.current;
|
||||
RectTransform selection = events.currentSelectedGameObject != null ?
|
||||
events.currentSelectedGameObject.GetComponent<RectTransform>() :
|
||||
null;
|
||||
RectTransform lastSelection = events.lastSelectedGameObject != null ?
|
||||
events.lastSelectedGameObject.GetComponent<RectTransform>() :
|
||||
selection;
|
||||
|
||||
if (selection != targetScrollObject)
|
||||
scrollToSelection = true;
|
||||
|
||||
// check if scrolling is possible
|
||||
bool isScrollDirectionUnknown =
|
||||
(selection == null || lastSelection == null || scrollToSelection == false);
|
||||
|
||||
if (isScrollDirectionUnknown == true || selection.transform.parent != layoutListGroup.transform)
|
||||
return;
|
||||
|
||||
// move the current scroll rect to correct position
|
||||
float selectionPos = -selection.anchoredPosition.y;
|
||||
int direction = (int)Mathf.Sign(selection.anchoredPosition.y - lastSelection.anchoredPosition.y);
|
||||
|
||||
float elementHeight = layoutListGroup.sizeDelta.y / layoutListGroup.transform.childCount;
|
||||
float maskHeight = currentCanvas.sizeDelta.y + scrollWindow.sizeDelta.y;
|
||||
float listPixelAnchor = layoutListGroup.anchoredPosition.y;
|
||||
|
||||
// get the element offset value depending on the cursor move direction
|
||||
float offlimitsValue = 0;
|
||||
if (direction > 0 && selectionPos < listPixelAnchor)
|
||||
{
|
||||
offlimitsValue = listPixelAnchor - selectionPos;
|
||||
}
|
||||
if (direction < 0 && selectionPos + elementHeight > listPixelAnchor + maskHeight)
|
||||
{
|
||||
offlimitsValue = (listPixelAnchor + maskHeight) - (selectionPos + elementHeight);
|
||||
}
|
||||
// move the target scroll rect
|
||||
targetScrollRect.verticalNormalizedPosition +=
|
||||
(offlimitsValue / layoutListGroup.sizeDelta.y) * Time.deltaTime * scrollSpeed;
|
||||
// check if we reached our destination
|
||||
if (Mathf.Abs(offlimitsValue) < 2f)
|
||||
scrollToSelection = false;
|
||||
// save last object we were "heading to" to prevent blocking
|
||||
targetScrollObject = selection;
|
||||
}
|
||||
|
||||
/* ### ENUMS ================================================================== */
|
||||
|
||||
|
||||
/* ### CUSTOM TYPES =========================================================== */
|
||||
|
||||
|
||||
and drag'n'drop the RectTransform of the options "container" that we'll be scrolling.*/
|
||||
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
namespace UnityEngine.UI.Extensions
|
||||
{
|
||||
public class UIScrollToSelection : MonoBehaviour
|
||||
{
|
||||
|
||||
/* ### VARIABLES ============================================================== */
|
||||
|
||||
// settings
|
||||
public float scrollSpeed = 10f;
|
||||
|
||||
[SerializeField]
|
||||
private RectTransform layoutListGroup;
|
||||
|
||||
// temporary variables
|
||||
private RectTransform targetScrollObject;
|
||||
private bool scrollToSelection = true;
|
||||
|
||||
// references
|
||||
private RectTransform scrollWindow;
|
||||
private RectTransform currentCanvas;
|
||||
private ScrollRect targetScrollRect;
|
||||
|
||||
private EventSystem events = EventSystem.current;
|
||||
|
||||
/* ### MAIN METHODS =========================================================== */
|
||||
// Use this for initialization
|
||||
private void Start()
|
||||
{
|
||||
targetScrollRect = GetComponent<ScrollRect>();
|
||||
scrollWindow = targetScrollRect.GetComponent<RectTransform>();
|
||||
currentCanvas = transform.root.GetComponent<RectTransform>();
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
private void Update()
|
||||
{
|
||||
ScrollRectToLevelSelection();
|
||||
}
|
||||
|
||||
private void ScrollRectToLevelSelection()
|
||||
{
|
||||
// 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;
|
||||
RectTransform lastSelection = events.lastSelectedGameObject != null ?
|
||||
events.lastSelectedGameObject.GetComponent<RectTransform>() :
|
||||
selection;
|
||||
|
||||
if (selection != targetScrollObject)
|
||||
scrollToSelection = true;
|
||||
|
||||
// check if scrolling is possible
|
||||
bool isScrollDirectionUnknown =
|
||||
(selection == null || lastSelection == null || scrollToSelection == false);
|
||||
|
||||
if (isScrollDirectionUnknown == true || selection.transform.parent != layoutListGroup.transform)
|
||||
return;
|
||||
|
||||
// move the current scroll rect to correct position
|
||||
float selectionPos = -selection.anchoredPosition.y;
|
||||
int direction = (int)Mathf.Sign(selection.anchoredPosition.y - lastSelection.anchoredPosition.y);
|
||||
|
||||
float elementHeight = layoutListGroup.sizeDelta.y / layoutListGroup.transform.childCount;
|
||||
float maskHeight = currentCanvas.sizeDelta.y + scrollWindow.sizeDelta.y;
|
||||
float listPixelAnchor = layoutListGroup.anchoredPosition.y;
|
||||
|
||||
// get the element offset value depending on the cursor move direction
|
||||
float offlimitsValue = 0;
|
||||
if (direction > 0 && selectionPos < listPixelAnchor)
|
||||
{
|
||||
offlimitsValue = listPixelAnchor - selectionPos;
|
||||
}
|
||||
if (direction < 0 && selectionPos + elementHeight > listPixelAnchor + maskHeight)
|
||||
{
|
||||
offlimitsValue = (listPixelAnchor + maskHeight) - (selectionPos + elementHeight);
|
||||
}
|
||||
// move the target scroll rect
|
||||
targetScrollRect.verticalNormalizedPosition +=
|
||||
(offlimitsValue / layoutListGroup.sizeDelta.y) * Time.deltaTime * scrollSpeed;
|
||||
// check if we reached our destination
|
||||
if (Mathf.Abs(offlimitsValue) < 2f)
|
||||
scrollToSelection = false;
|
||||
// save last object we were "heading to" to prevent blocking
|
||||
targetScrollObject = selection;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: a5f5420166e1c6d4f849661cbdbc52f8
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
Loading…
Reference in New Issue