Merge with develop-Unity5.1

pull/413/head
Simon (darkside) Jackson 2015-08-26 10:08:44 +01:00
commit e654fb5434
4 changed files with 110 additions and 104 deletions

View File

@ -84,9 +84,9 @@ namespace UnityEngine.UI.Extensions
{ {
image.color = color; image.color = color;
} }
else if (renderer) else if (GetComponent<UnityEngine.Renderer>())
{ {
renderer.material.color = color; GetComponent<UnityEngine.Renderer>().material.color = color;
} }

View File

@ -235,7 +235,7 @@ namespace UnityEngine.UI.Extensions
} else { } 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. //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 //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); var selectableScreenPoint = GetScreenPointOfSelectable(selectable);

View File

@ -2,105 +2,103 @@
/*USAGE: /*USAGE:
Simply place the script on the ScrollRect that contains the selectable children we'll be scroling to 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.*/ and drag'n'drop the RectTransform of the options "container" that we'll be scrolling.*/
using UnityEngine; using UnityEngine.EventSystems;
using System.Collections;
using UnityEngine.UI; namespace UnityEngine.UI.Extensions
using UnityEngine.EventSystems; {
public class UIScrollToSelection : MonoBehaviour
public class UIScrollToSelection : MonoBehaviour { {
/* ### VARIABLES ============================================================== */ /* ### VARIABLES ============================================================== */
// settings // settings
public float scrollSpeed = 10f; public float scrollSpeed = 10f;
[SerializeField] [SerializeField]
private RectTransform layoutListGroup; private RectTransform layoutListGroup;
// temporary variables // temporary variables
private RectTransform targetScrollObject; private RectTransform targetScrollObject;
private bool scrollToSelection = true; private bool scrollToSelection = true;
// references // references
private RectTransform scrollWindow; private RectTransform scrollWindow;
private RectTransform currentCanvas; private RectTransform currentCanvas;
private ScrollRect targetScrollRect; private ScrollRect targetScrollRect;
private EventSystem events = EventSystem.current;
/* ### MAIN METHODS =========================================================== */
// Use this for initialization /* ### MAIN METHODS =========================================================== */
private void Start () { // Use this for initialization
targetScrollRect = GetComponent<ScrollRect>(); private void Start()
scrollWindow = targetScrollRect.GetComponent<RectTransform>(); {
currentCanvas = transform.root.GetComponent<RectTransform>(); targetScrollRect = GetComponent<ScrollRect>();
} scrollWindow = targetScrollRect.GetComponent<RectTransform>();
currentCanvas = transform.root.GetComponent<RectTransform>();
// Update is called once per frame }
private void Update () {
ScrollRectToLevelSelection(); // Update is called once per frame
} private void Update()
{
private void ScrollRectToLevelSelection (){ ScrollRectToLevelSelection();
// check main references }
bool referencesAreIncorrect =
(targetScrollRect == null || layoutListGroup == null || scrollWindow == null); private void ScrollRectToLevelSelection()
if (referencesAreIncorrect == true){ {
return; // check main references
} bool referencesAreIncorrect =
(targetScrollRect == null || layoutListGroup == null || scrollWindow == null);
// get calculation references if (referencesAreIncorrect == true)
EventSystem events = EventSystem.current; {
RectTransform selection = events.currentSelectedGameObject != null ? return;
events.currentSelectedGameObject.GetComponent<RectTransform>() : }
null;
RectTransform lastSelection = events.lastSelectedGameObject != null ? // get calculation references
events.lastSelectedGameObject.GetComponent<RectTransform>() : RectTransform selection = events.currentSelectedGameObject != null ?
selection; events.currentSelectedGameObject.GetComponent<RectTransform>() :
null;
if (selection != targetScrollObject) RectTransform lastSelection = events.lastSelectedGameObject != null ?
scrollToSelection = true; events.lastSelectedGameObject.GetComponent<RectTransform>() :
selection;
// check if scrolling is possible
bool isScrollDirectionUnknown = if (selection != targetScrollObject)
(selection == null || lastSelection == null || scrollToSelection == false); scrollToSelection = true;
if (isScrollDirectionUnknown == true || selection.transform.parent != layoutListGroup.transform) // check if scrolling is possible
return; bool isScrollDirectionUnknown =
(selection == null || lastSelection == null || scrollToSelection == false);
// move the current scroll rect to correct position
float selectionPos = -selection.anchoredPosition.y; if (isScrollDirectionUnknown == true || selection.transform.parent != layoutListGroup.transform)
int direction = (int)Mathf.Sign(selection.anchoredPosition.y - lastSelection.anchoredPosition.y); return;
float elementHeight = layoutListGroup.sizeDelta.y / layoutListGroup.transform.childCount; // move the current scroll rect to correct position
float maskHeight = currentCanvas.sizeDelta.y + scrollWindow.sizeDelta.y; float selectionPos = -selection.anchoredPosition.y;
float listPixelAnchor = layoutListGroup.anchoredPosition.y; int direction = (int)Mathf.Sign(selection.anchoredPosition.y - lastSelection.anchoredPosition.y);
// get the element offset value depending on the cursor move direction float elementHeight = layoutListGroup.sizeDelta.y / layoutListGroup.transform.childCount;
float offlimitsValue = 0; float maskHeight = currentCanvas.sizeDelta.y + scrollWindow.sizeDelta.y;
if (direction > 0 && selectionPos < listPixelAnchor) float listPixelAnchor = layoutListGroup.anchoredPosition.y;
{
offlimitsValue = listPixelAnchor - selectionPos; // get the element offset value depending on the cursor move direction
} float offlimitsValue = 0;
if (direction < 0 && selectionPos + elementHeight > listPixelAnchor + maskHeight) if (direction > 0 && selectionPos < listPixelAnchor)
{ {
offlimitsValue = (listPixelAnchor + maskHeight) - (selectionPos + elementHeight); offlimitsValue = listPixelAnchor - selectionPos;
} }
// move the target scroll rect if (direction < 0 && selectionPos + elementHeight > listPixelAnchor + maskHeight)
targetScrollRect.verticalNormalizedPosition += {
(offlimitsValue / layoutListGroup.sizeDelta.y) * Time.deltaTime * scrollSpeed; offlimitsValue = (listPixelAnchor + maskHeight) - (selectionPos + elementHeight);
// check if we reached our destination }
if (Mathf.Abs(offlimitsValue) < 2f) // move the target scroll rect
scrollToSelection = false; targetScrollRect.verticalNormalizedPosition +=
// save last object we were "heading to" to prevent blocking (offlimitsValue / layoutListGroup.sizeDelta.y) * Time.deltaTime * scrollSpeed;
targetScrollObject = selection; // check if we reached our destination
} if (Mathf.Abs(offlimitsValue) < 2f)
scrollToSelection = false;
/* ### ENUMS ================================================================== */ // save last object we were "heading to" to prevent blocking
targetScrollObject = selection;
}
/* ### CUSTOM TYPES =========================================================== */ }
} }

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: a5f5420166e1c6d4f849661cbdbc52f8
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData: