Skip to content

Instantly share code, notes, and snippets.

@IvanMurzak
Last active April 3, 2018 17:38
Show Gist options
  • Save IvanMurzak/2bad3244b5dd6dec266ceae0eb25d960 to your computer and use it in GitHub Desktop.
Save IvanMurzak/2bad3244b5dd6dec266ceae0eb25d960 to your computer and use it in GitHub Desktop.
ColorByScroll.cs --- Coloring NGUI Sprite by ScrollView percent, using gradient. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ExtentionsUIScrollView.cs --- NGUI - UIScrollView extention. It helps getting horizontal, vertical percent from UIScrollView. Ju…
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ColorByScroll : MonoBehaviour
{
public UIScrollView scrollView;
public Gradient gradient;
private UI2DSprite ui2DSprite;
private UISprite uiSprite;
public Axis axis = Axis.HORIZONTAL;
public enum Axis { HORIZONTAL, VERTICAL }
void Start()
{
ui2DSprite = GetComponent<UI2DSprite>();
uiSprite = GetComponent<UISprite>();
scrollView.panel.onClipMove += UpdateColor;
}
private void UpdateColor(UIPanel panel)
{
float percent = axis == Axis.HORIZONTAL ?
scrollView.PercentHorizontal() :
scrollView.PercentVertical();
var color = gradient.Evaluate(percent);
if (ui2DSprite != null) ui2DSprite.color = color;
if (uiSprite != null) uiSprite.color = color;
}
}
using UnityEngine;
public static class ExtentionsUIScrollView
{
public static float PercentHorizontal(this UIScrollView scrollView)
{
Bounds b = scrollView.bounds;
Vector2 bmin = b.min;
Vector2 bmax = b.max;
Vector4 clip = scrollView.panel.finalClipRegion;
int intViewSize = Mathf.RoundToInt(clip.z);
if ((intViewSize & 1) != 0) intViewSize -= 1;
float halfViewSize = intViewSize * 0.5f;
halfViewSize = Mathf.Round(halfViewSize);
if (scrollView.panel.clipping == UIDrawCall.Clipping.SoftClip)
halfViewSize -= scrollView.panel.clipSoftness.x;
float contentSize = bmax.x - bmin.x;
float viewSize = halfViewSize * 2f;
float contentMin = bmin.x;
float viewMin = clip.x - halfViewSize;
contentMin = viewMin - contentMin;
float max = contentSize - viewSize;
return Mathf.Max(0, Mathf.Min(1f, contentMin / max));
}
public static float PercentVertical(this UIScrollView scrollView)
{
Bounds b = scrollView.bounds;
Vector2 bmin = b.min;
Vector2 bmax = b.max;
Vector4 clip = scrollView.panel.finalClipRegion;
int intViewSize = Mathf.RoundToInt(clip.w);
if ((intViewSize & 1) != 0) intViewSize -= 1;
float halfViewSize = intViewSize * 0.5f;
halfViewSize = Mathf.Round(halfViewSize);
if (scrollView.panel.clipping == UIDrawCall.Clipping.SoftClip)
halfViewSize -= scrollView.panel.clipSoftness.y;
float contentSize = bmax.y - bmin.y;
float viewSize = halfViewSize * 2f;
float contentMin = bmin.y;
float viewMin = clip.y - halfViewSize;
contentMin = viewMin - contentMin;
float max = contentSize - viewSize;
return Mathf.Max(0, Mathf.Min(1f, contentMin / max));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment