Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Toru Omura
committed
Apr 14, 2016
1 parent
63d19c8
commit 1f359d5
Showing
24 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using UnityEngine; | ||
using UnityEditor; | ||
|
||
[CustomEditor( typeof(SnapScrollRect))] | ||
public sealed class SnapScrollRectEditor : Editor { | ||
|
||
public override void OnInspectorGUI(){ | ||
DrawDefaultInspector (); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
using UnityEngine; | ||
using System.Collections; | ||
using UnityEngine.UI; | ||
using UnityEngine.EventSystems; | ||
|
||
public class SnapScrollRect : ScrollRect { | ||
|
||
public int hIndex; | ||
public int vIndex; | ||
|
||
public int hPageNum = 3; | ||
public int vPageNum = 0; | ||
public float smoothness = 10f; | ||
public float scrollWeight = 0.1f; | ||
|
||
private Vector2 targetPosition; | ||
private float hPerPage; | ||
private float vPerPage; | ||
private bool dragging; | ||
private bool forcePositionUpdate = false; | ||
|
||
public void ScrollTo( int x, int y) | ||
{ | ||
hIndex = x; | ||
vIndex = y; | ||
forcePositionUpdate = true; | ||
} | ||
|
||
protected override void Awake() | ||
{ | ||
base.Awake(); | ||
hPerPage = 1f / (float)(hPageNum - 1); | ||
vPerPage = 1f / (float)(hPageNum - 1); | ||
} | ||
|
||
protected override void Start() | ||
{ | ||
base.Start(); | ||
targetPosition = GetSnapPosition(); | ||
} | ||
|
||
public override void OnBeginDrag(PointerEventData eventData) | ||
{ | ||
base.OnBeginDrag(eventData); | ||
dragging = true; | ||
} | ||
|
||
public override void OnEndDrag(PointerEventData eventData) | ||
{ | ||
base.OnEndDrag(eventData); | ||
UpdateIndex(); | ||
targetPosition = GetSnapPosition(); | ||
dragging = false; | ||
} | ||
|
||
void Update() | ||
{ | ||
if ( !dragging && normalizedPosition != targetPosition ) | ||
{ | ||
normalizedPosition = Vector2.Lerp(normalizedPosition, targetPosition, smoothness * Time.deltaTime); | ||
|
||
if ( Vector2.Distance(normalizedPosition, targetPosition) < 0.009f ) | ||
{ | ||
normalizedPosition = targetPosition; | ||
} | ||
} | ||
|
||
if ( forcePositionUpdate ) | ||
{ | ||
forcePositionUpdate = false; | ||
targetPosition = GetSnapPosition(); | ||
} | ||
} | ||
|
||
void UpdateIndex() | ||
{ | ||
float xPage, yPage = -1; | ||
|
||
if (horizontal && hPageNum > 0) | ||
{ | ||
xPage = (normalizedPosition.x / hPerPage); | ||
float diff = xPage - (1 * hIndex); | ||
|
||
if ( diff >= scrollWeight ) | ||
{ | ||
hIndex++; | ||
} | ||
else if ( diff <= -scrollWeight ) | ||
{ | ||
hIndex--; | ||
} | ||
} | ||
|
||
if (vertical && vPageNum > 0) | ||
{ | ||
yPage = normalizedPosition.y / vPerPage; | ||
float diff = yPage - (1 * vIndex); | ||
|
||
if ( diff >= scrollWeight ) | ||
{ | ||
vIndex++; | ||
} else if ( diff <= -scrollWeight ) | ||
{ | ||
vIndex--; | ||
} | ||
} | ||
} | ||
|
||
Vector2 GetSnapPosition() | ||
{ | ||
return new Vector2(horizontal && hPageNum > 0 ? hIndex * hPerPage : normalizedPosition.x, vertical && vPageNum > 0 ? vIndex * vPerPage : normalizedPosition.y); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
m_EditorVersion: 5.3.4f1 | ||
m_StandardAssetsVersion: 0 |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.