Created
February 26, 2014 09:54
-
-
Save TarasOsiris/9226829 to your computer and use it in GitHub Desktop.
Put this script on game object with UIDragScrollView to get scale and alpha highlighting effect. Might be not very efficient but it works ok.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System; | |
namespace Com.Nravo.NGUI { | |
[RequireComponent(typeof(UIDragScrollView))] | |
public class UiHighlightCenteredItem : MonoBehaviour { | |
// coefs to monitor | |
// zero means target is centered, 1 means target is max away | |
[Range(0.0f, 1.0f)] public float distanceFromCenterHorizontalCoef; | |
[Range(0.0f, 1.0f)] public float distanceFromCenterVerticalCoef; | |
[Range(0.0f, 1.0f)] public float minAlpha; | |
[Range(0.0f, 1.0f)] public float maxAlpha; | |
[Range(0.0f, 1.0f)] public float minScale; | |
[Range(0.0f, 1.0f)] public float maxScale; | |
private const float Delta = 0.01f; // to avoid small values close to zero | |
private UIGrid.Arrangement _arrangement; | |
private UIScrollView _scrollView; | |
private float _cellWidthHorizontal; | |
private float _cellWidthVertical; | |
private UIWidget[] _childWidgets; | |
private Transform _itemTransform; | |
private Transform _scrollViewTransform; | |
void Start() { | |
_scrollView = gameObject.GetComponent<UIDragScrollView>().scrollView; | |
_arrangement = _scrollView.GetComponentInChildren<UIGrid>().arrangement; | |
// Cache transform | |
_itemTransform = transform; | |
_scrollViewTransform = _scrollView.transform; | |
_childWidgets = gameObject.GetComponentsInChildren<UIWidget>(); | |
foreach(var widget in _childWidgets) { widget.alpha = minAlpha; } | |
_cellWidthHorizontal = _scrollView.GetComponentInChildren<UIGrid>().cellWidth; | |
_cellWidthVertical = _scrollView.GetComponentInChildren<UIGrid>().cellHeight; | |
} | |
void Update() { | |
UpdateDistanceFromCenterCoef(); | |
AdjustOpacity(); | |
AdjustScale(); | |
} | |
private void UpdateDistanceFromCenterCoef() { | |
float distanceFromGridCenter = CalculateDistanceFromGridCenter(); | |
if (distanceFromGridCenter < Delta && distanceFromGridCenter > -Delta) { distanceFromGridCenter = 0.0f; } | |
float cellSize = _arrangement == UIGrid.Arrangement.Horizontal ? _cellWidthHorizontal : _cellWidthVertical; | |
distanceFromCenterHorizontalCoef = Mathf.Clamp01(Math.Abs(distanceFromGridCenter / cellSize)); | |
if (distanceFromCenterHorizontalCoef >= 1.0f) { return; } | |
} | |
private float CalculateDistanceFromGridCenter() { | |
float distance = Math.Abs(_itemTransform.localPosition.y) - Math.Abs(_scrollViewTransform.localPosition.y); | |
if (_arrangement == UIGrid.Arrangement.Horizontal) { | |
return Math.Abs(_itemTransform.localPosition.x) - Math.Abs(_scrollViewTransform.transform.localPosition.x); | |
} | |
distance = CorrectCoefIfCloseToZero(distance); | |
return distance; | |
} | |
private static float CorrectCoefIfCloseToZero(float distanceFromGridCenter) { | |
if (distanceFromGridCenter < Delta && distanceFromGridCenter > -Delta) { return 0.0f; } | |
return distanceFromGridCenter; | |
} | |
#region adjust_opacity_and_scale | |
private void AdjustOpacity() { | |
if (maxAlpha < minAlpha) { throw new InvalidOperationException("max alpha can't be bigger than min"); } | |
float alphaDelta = maxAlpha - minAlpha; | |
foreach(var widget in _childWidgets) { widget.alpha = maxAlpha - distanceFromCenterHorizontalCoef * alphaDelta; } | |
} | |
private void AdjustScale() { | |
if (maxScale < minScale) { throw new InvalidOperationException("max alpha can't be bigger than min"); } | |
float scaleDelta = maxScale - minScale; | |
float currentScale = maxScale - distanceFromCenterHorizontalCoef * scaleDelta; | |
gameObject.transform.localScale = new Vector2(currentScale, currentScale); | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment