Added initial version of the UI Vertical scroller from Mrs. YakaYocha

Complete with component and editor menu options

--HG--
branch : develop_5.3
pull/413/head
Simon (darkside) Jackson 2015-12-23 14:54:32 +00:00
parent 84edb62e91
commit f72eb7268f
20 changed files with 657 additions and 0 deletions

View File

@ -470,6 +470,87 @@ namespace UnityEditor.UI
#endregion
#region UIVertical Scroller
[MenuItem("GameObject/UI/Extensions/UI Vertical Scroller", false)]
static public void AddUIVerticallScroller(MenuCommand menuCommand)
{
GameObject uiVerticalScrollerRoot = CreateUIElementRoot("UI Vertical Scroller", menuCommand, s_ThickGUIElementSize);
GameObject uiScrollerCenter = CreateUIObject("Center", uiVerticalScrollerRoot);
GameObject childContent = CreateUIObject("Content", uiVerticalScrollerRoot);
// Set RectTransform to stretch
RectTransform rectTransformScrollSnapRoot = uiVerticalScrollerRoot.GetComponent<RectTransform>();
rectTransformScrollSnapRoot.anchorMin = new Vector2(0.5f, 0.5f);
rectTransformScrollSnapRoot.anchorMax = new Vector2(0.5f, 0.5f);
rectTransformScrollSnapRoot.anchoredPosition = Vector2.zero;
rectTransformScrollSnapRoot.sizeDelta = new Vector2(500f, 150f);
// Add required ScrollRect
ScrollRect sr = uiVerticalScrollerRoot.AddComponent<ScrollRect>();
sr.vertical = true;
sr.horizontal = false;
sr.movementType = ScrollRect.MovementType.Unrestricted;
var uiscr = uiVerticalScrollerRoot.AddComponent<UIVerticalScroller>();
//Setup container center point
RectTransform rectTransformCenter = uiScrollerCenter.GetComponent<RectTransform>();
rectTransformCenter.anchorMin = new Vector2(0f, 0.3f);
rectTransformCenter.anchorMax = new Vector2(1f, 0.6f);
rectTransformCenter.sizeDelta = Vector2.zero;
uiscr._center = uiScrollerCenter.GetComponent<RectTransform>();
//Setup Content container
RectTransform rectTransformContent = childContent.GetComponent<RectTransform>();
rectTransformContent.anchorMin = Vector2.zero;
rectTransformContent.anchorMax = new Vector2(1f, 1f);
rectTransformContent.sizeDelta = Vector2.zero;
sr.content = rectTransformContent;
// Add sample children
for (int i = 0; i < 10; i++)
{
GameObject childPage = CreateUIObject("Page_" + i, childContent);
GameObject childText = CreateUIObject("Text", childPage);
//Setup 1st Child
Image pageImage = childPage.AddComponent<Image>();
pageImage.sprite = AssetDatabase.GetBuiltinExtraResource<Sprite>(kStandardSpritePath);
pageImage.type = Image.Type.Sliced;
pageImage.color = s_DefaultSelectableColor;
RectTransform rectTransformPage = childPage.GetComponent<RectTransform>();
rectTransformPage.anchorMin = new Vector2(0f, 0.5f);
rectTransformPage.anchorMax = new Vector2(1f, 0.5f);
rectTransformPage.sizeDelta = new Vector2(0f, 80f);
rectTransformPage.pivot = new Vector2(0.5f, 0.5f);
rectTransformPage.localPosition = new Vector3(0, 80 * i, 0);
childPage.AddComponent<Button>();
var childCG = childPage.AddComponent<CanvasGroup>();
childCG.interactable = false;
//Setup Text on Item
Text text = childText.AddComponent<Text>();
text.text = "Item_" + i;
text.alignment = TextAnchor.MiddleCenter;
text.color = new Color(0.196f, 0.196f, 0.196f);
//Setup Text on Item
RectTransform rectTransformPageText = childText.GetComponent<RectTransform>();
rectTransformPageText.anchorMin = new Vector2(0.5f, 0.5f);
rectTransformPageText.anchorMax = new Vector2(0.5f, 0.5f);
rectTransformPageText.pivot = new Vector2(0.5f, 0.5f);
}
Selection.activeGameObject = uiVerticalScrollerRoot;
}
#endregion
[MenuItem("GameObject/UI/Extensions/UI Button", false)]
static public void AddUIButton(MenuCommand menuCommand)
{

View File

@ -0,0 +1,188 @@
/// Credit Mrs. YakaYocha
/// Sourced from - https://www.youtube.com/channel/UCHp8LZ_0-iCvl-5pjHATsgw
/// Please donate: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=RJ8D9FRFQF9VS
using UnityEngine.Events;
namespace UnityEngine.UI.Extensions
{
[RequireComponent(typeof(ScrollRect))]
[AddComponentMenu("Layout/Extensions/Vertical Scroller")]
public class UIVerticalScroller : MonoBehaviour
{
[Tooltip("Scrollable area (content of desired ScrollRect)")]
public RectTransform _scrollingPanel;
[Tooltip("Elements to populate inside the scroller")]
public GameObject[] _arrayOfElements;
[Tooltip("Center display area (position of zoomed content)")]
public RectTransform _center;
[Tooltip("Select the item to be in center on start. (optional)")]
public int StartingIndex = -1;
[Tooltip("Button to go to the next page. (optional)")]
public GameObject ScrollUpButton;
[Tooltip("Button to go to the previous page. (optional)")]
public GameObject ScrollDownButton;
[Tooltip("Event fired when a specific item is clicked, exposes index number of item. (optional)")]
public UnityEvent<int> ButtonClicked;
private float[] distReposition;
private float[] distance;
//private int elementsDistance;
private int minElementsNum;
private int elementLength;
private int elementHalfLength;
private float deltaY;
private string result;
public UIVerticalScroller() { }
public UIVerticalScroller(RectTransform scrollingPanel, GameObject[] arrayOfElements, RectTransform center)
{
_scrollingPanel = scrollingPanel;
_arrayOfElements = arrayOfElements;
_center = center;
}
public void Awake()
{
var scrollRect = GetComponent<ScrollRect>();
if (!_scrollingPanel)
{
_scrollingPanel = scrollRect.content;
}
if (!_center)
{
Debug.LogError("Please define the RectTransform for the Center viewport of the scrollable area");
}
if (_arrayOfElements == null || _arrayOfElements.Length == 0)
{
var childCount = scrollRect.content.childCount;
if (childCount > 0)
{
_arrayOfElements = new GameObject[childCount];
for (int i = 0; i < childCount; i++)
{
_arrayOfElements[i] = scrollRect.content.GetChild(i).gameObject;
}
}
}
}
public void Start()
{
if (_arrayOfElements.Length < 1)
{
Debug.Log("No child content found, exiting..");
return;
}
elementLength = _arrayOfElements.Length;
distance = new float[elementLength];
distReposition = new float[elementLength];
//get distance between buttons
//elementsDistance = (int)Mathf.Abs(_arrayOfElements[1].GetComponent<RectTransform>().anchoredPosition.y - _arrayOfElements[0].GetComponent<RectTransform>().anchoredPosition.y);
deltaY = _arrayOfElements[0].GetComponent<RectTransform>().rect.height * elementLength / 3 * 2;
Vector2 startPosition = new Vector2(_scrollingPanel.anchoredPosition.x, -deltaY);
_scrollingPanel.anchoredPosition = startPosition;
for (var i = 0; i < _arrayOfElements.Length; i++)
{
AddListener(_arrayOfElements[i], i);
}
if (ScrollUpButton)
ScrollUpButton.GetComponent<Button>().onClick.AddListener(() => { ScrollUp(); });
if (ScrollDownButton)
ScrollDownButton.GetComponent<Button>().onClick.AddListener(() => { ScrollDown(); });
if (StartingIndex > -1)
{
StartingIndex = StartingIndex > _arrayOfElements.Length ? _arrayOfElements.Length - 1 : StartingIndex;
SnapToElement(StartingIndex);
}
}
private void AddListener(GameObject button, int index)
{
button.GetComponent<Button>().onClick.AddListener(() => DoSomething(index));
}
private void DoSomething(int index)
{
if (ButtonClicked != null)
{
ButtonClicked.Invoke(index);
}
}
public void Update()
{
if (_arrayOfElements.Length < 1)
{
return;
}
for (var i = 0; i < elementLength; i++)
{
distReposition[i] = _center.GetComponent<RectTransform>().position.y - _arrayOfElements[i].GetComponent<RectTransform>().position.y;
distance[i] = Mathf.Abs(distReposition[i]);
//Magnifying effect
float scale = Mathf.Max(0.7f, 1 / (1 + distance[i] / 200));
_arrayOfElements[i].GetComponent<RectTransform>().transform.localScale = new Vector3(scale, scale, 1f);
}
float minDistance = Mathf.Min(distance);
for (var i = 0; i < elementLength; i++)
{
_arrayOfElements[i].GetComponent<CanvasGroup>().interactable = false;
if (minDistance == distance[i])
{
minElementsNum = i;
_arrayOfElements[i].GetComponent<CanvasGroup>().interactable = true;
result = _arrayOfElements[i].GetComponentInChildren<Text>().text;
}
}
ScrollingElements(-_arrayOfElements[minElementsNum].GetComponent<RectTransform>().anchoredPosition.y);
}
private void ScrollingElements(float position)
{
float newY = Mathf.Lerp(_scrollingPanel.anchoredPosition.y, position, Time.deltaTime * 1f);
Vector2 newPosition = new Vector2(_scrollingPanel.anchoredPosition.x, newY);
_scrollingPanel.anchoredPosition = newPosition;
}
public string GetResults()
{
return result;
}
public void SnapToElement(int element)
{
float deltaElementPositionY = _arrayOfElements[0].GetComponent<RectTransform>().rect.height * element;
Vector2 newPosition = new Vector2(_scrollingPanel.anchoredPosition.x, -deltaElementPositionY);
_scrollingPanel.anchoredPosition = newPosition;
}
public void ScrollUp()
{
float deltaUp = _arrayOfElements[0].GetComponent<RectTransform>().rect.height / 1.2f;
Vector2 newPositionUp = new Vector2(_scrollingPanel.anchoredPosition.x, _scrollingPanel.anchoredPosition.y - deltaUp);
_scrollingPanel.anchoredPosition = Vector2.Lerp(_scrollingPanel.anchoredPosition, newPositionUp, 1);
}
public void ScrollDown()
{
float deltaDown = _arrayOfElements[0].GetComponent<RectTransform>().rect.height / 1.2f;
Vector2 newPositionDown = new Vector2(_scrollingPanel.anchoredPosition.x, _scrollingPanel.anchoredPosition.y + deltaDown);
_scrollingPanel.anchoredPosition = newPositionDown;
}
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: b9f902652954d44beb3a916831d27435
timeCreated: 1449409849
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: c94ebd5b05e589d45b7d99b1dfb64135
folderAsset: yes
timeCreated: 1450872931
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 6a0cecb173b794c3bb33e379defdeeb0
folderAsset: yes
timeCreated: 1449409914
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: ec6f9b1230e4a4896b7be2e60d3be758
timeCreated: 1448815288
licenseType: Free
NativeFormatImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 6d45e8f0aa19044ffb3553ca52febef2
timeCreated: 1448814882
licenseType: Free
NativeFormatImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 65ce75600991941569988ab4538a21a6
timeCreated: 1448814778
licenseType: Free
NativeFormatImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 573635efe02d54e20a59cd3023afa514
folderAsset: yes
timeCreated: 1449409946
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: dc87f62a4e5c94019904b8b6037818b5
timeCreated: 1449409834
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: f1caec3cabf7c4eaaa5b2ad30cf018a9
folderAsset: yes
timeCreated: 1449409928
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,231 @@
/// <summary>
/// Brought you by Mrs. YakaYocha
/// https://www.youtube.com/channel/UCHp8LZ_0-iCvl-5pjHATsgw
///
/// Please donate: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=RJ8D9FRFQF9VS
/// </summary>
using UnityEngine;
using UnityEngine.UI;
using System;
using UnityEngine.UI.Extensions;
public class ScrollingCalendar : MonoBehaviour
{
public RectTransform monthsScrollingPanel;
public RectTransform yearsScrollingPanel;
public RectTransform daysScrollingPanel;
public GameObject yearsButtonPrefab;
public GameObject monthsButtonPrefab;
public GameObject daysButtonPrefab;
private GameObject[] monthsButtons;
private GameObject[] yearsButtons;
private GameObject[] daysButtons;
public RectTransform monthCenter;
public RectTransform yearsCenter;
public RectTransform daysCenter;
UIVerticalScroller yearsVerticalScroller;
UIVerticalScroller monthsVerticalScroller;
UIVerticalScroller daysVerticalScroller;
public InputField inputFieldDays;
public InputField inputFieldMonths;
public InputField inputFieldYears;
public Text dateText;
private int daysSet;
private int monthsSet;
private int yearsSet;
private void InitializeYears()
{
int currentYear = int.Parse(System.DateTime.Now.ToString("yyyy"));
int[] arrayYears = new int[currentYear+1 - 1900];
yearsButtons = new GameObject[arrayYears.Length];
for (int i = 0; i < arrayYears.Length; i++)
{
arrayYears[i] = 1900 + i;
GameObject clone = (GameObject)Instantiate(yearsButtonPrefab, new Vector3(0, i*80, 0), Quaternion.Euler(new Vector3(0, 0, 0))) as GameObject;
clone.transform.SetParent(yearsScrollingPanel);
clone.transform.localScale = new Vector3(1, 1, 1);
clone.GetComponentInChildren<Text>().text = "" + arrayYears[i];
clone.name = "Year_" + arrayYears[i];
clone.AddComponent<CanvasGroup>();
yearsButtons[i] = clone;
}
}
//Initialize Months
private void InitializeMonths()
{
int[] months = new int[12];
monthsButtons = new GameObject[months.Length];
for (int i = 0; i < months.Length; i++)
{
string month = "";
months[i] = i;
GameObject clone = (GameObject)Instantiate(monthsButtonPrefab, new Vector3(0, i * 80, 0), Quaternion.Euler(new Vector3(0, 0, 0))) as GameObject;
clone.transform.SetParent(monthsScrollingPanel);
clone.transform.localScale = new Vector3(1, 1, 1);
switch(i)
{
case 0:
month = "Jan";
break;
case 1:
month = "Feb";
break;
case 2:
month = "Mar";
break;
case 3:
month = "Apr";
break;
case 4:
month = "May";
break;
case 5:
month = "Jun";
break;
case 6:
month = "Jul";
break;
case 7:
month = "Aug";
break;
case 8:
month = "Sep";
break;
case 9:
month = "Oct";
break;
case 10:
month = "Nov";
break;
case 11:
month = "Dec";
break;
}
clone.GetComponentInChildren<Text>().text = month;
clone.name = "Month_" + months[i];
clone.AddComponent<CanvasGroup>();
monthsButtons[i] = clone;
}
}
private void InitializeDays()
{
int[] days = new int[31];
daysButtons = new GameObject[days.Length];
for (var i = 0; i < days.Length; i++)
{
days[i] = i+1;
GameObject clone = (GameObject)Instantiate(daysButtonPrefab, new Vector3(0, i * 80, 0), Quaternion.Euler(new Vector3(0, 0, 0))) as GameObject;
clone.transform.SetParent(daysScrollingPanel);
clone.transform.localScale = new Vector3(1, 1, 1);
clone.GetComponentInChildren<Text>().text = "" + days[i];
clone.name = "Day_" + days[i];
clone.AddComponent<CanvasGroup>();
daysButtons[i] = clone;
}
}
// Use this for initialization
public void Awake()
{
InitializeYears();
InitializeMonths();
InitializeDays();
//Yes Unity complains about this but it doesn't matter in this case.
monthsVerticalScroller = new UIVerticalScroller(monthsScrollingPanel, monthsButtons, monthCenter);
yearsVerticalScroller = new UIVerticalScroller (yearsScrollingPanel, yearsButtons, yearsCenter);
daysVerticalScroller = new UIVerticalScroller (daysScrollingPanel, daysButtons, daysCenter);
monthsVerticalScroller.Start();
yearsVerticalScroller.Start();
daysVerticalScroller.Start();
}
public void SetDate()
{
daysSet = int.Parse(inputFieldDays.text) - 1;
monthsSet = int.Parse(inputFieldMonths.text) - 1;
yearsSet = int.Parse(inputFieldYears.text) - 1900;
daysVerticalScroller.SnapToElement(daysSet);
monthsVerticalScroller.SnapToElement(monthsSet);
yearsVerticalScroller.SnapToElement(yearsSet);
}
void Update ()
{
monthsVerticalScroller.Update();
yearsVerticalScroller.Update();
daysVerticalScroller.Update();
string dayString = daysVerticalScroller.GetResults();
string monthString = monthsVerticalScroller.GetResults();
string yearsString = yearsVerticalScroller.GetResults();
if (dayString.EndsWith("1") && dayString != "11")
dayString = dayString + "st";
else if (dayString.EndsWith("2") && dayString != "12")
dayString = dayString + "nd";
else if (dayString.EndsWith("3") && dayString != "13")
dayString = dayString + "rd";
else
dayString = dayString + "th";
dateText.text = monthString + " " + dayString + " " + yearsString;
}
public void DaysScrollUp()
{
daysVerticalScroller.ScrollUp();
}
public void DaysScrollDown()
{
daysVerticalScroller.ScrollDown();
}
public void MonthsScrollUp()
{
monthsVerticalScroller.ScrollUp();
}
public void MonthsScrollDown()
{
monthsVerticalScroller.ScrollDown();
}
public void YearsScrollUp()
{
yearsVerticalScroller.ScrollUp();
}
public void YearsScrollDown()
{
yearsVerticalScroller.ScrollDown();
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: e0185937f87604a7497a35019c125107
timeCreated: 1449409849
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 7ffd04bf6a590424fb623c185d6f6ec5
folderAsset: yes
timeCreated: 1450208395
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

View File

@ -0,0 +1,56 @@
fileFormatVersion: 2
guid: 1e2d444ae9d5a4ff1b47ffec28d5165c
timeCreated: 1450208403
licenseType: Free
TextureImporter:
fileIDToRecycleName: {}
serializedVersion: 2
mipmaps:
mipMapMode: 0
enableMipMap: 1
linearTexture: 0
correctGamma: 0
fadeOut: 0
borderMipMap: 0
mipMapFadeDistanceStart: 1
mipMapFadeDistanceEnd: 3
bumpmap:
convertToNormalMap: 0
externalNormalMap: 0
heightScale: .25
normalMapFilter: 0
isReadable: 0
grayScaleToAlpha: 0
generateCubemap: 0
cubemapConvolution: 0
cubemapConvolutionSteps: 8
cubemapConvolutionExponent: 1.5
seamlessCubemap: 0
textureFormat: -1
maxTextureSize: 2048
textureSettings:
filterMode: -1
aniso: -1
mipBias: -1
wrapMode: 1
nPOTScale: 0
lightmap: 0
rGBM: 0
compressionQuality: 50
allowsAlphaSplitting: 0
spriteMode: 1
spriteExtrude: 1
spriteMeshType: 1
alignment: 0
spritePivot: {x: .5, y: .5}
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
spritePixelsToUnits: 100
alphaIsTransparency: 1
textureType: 8
buildTargetSettings: []
spriteSheet:
sprites: []
spritePackingTag:
userData:
assetBundleName:
assetBundleVariant: