Skip to content

Commit

Permalink
First Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Toru Omura committed Apr 14, 2016
1 parent 63d19c8 commit 1f359d5
Show file tree
Hide file tree
Showing 24 changed files with 166 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Assets/Editor.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions Assets/Editor/SnapScrollRectEditor.cs
@@ -0,0 +1,10 @@
using UnityEngine;
using UnityEditor;

[CustomEditor( typeof(SnapScrollRect))]
public sealed class SnapScrollRectEditor : Editor {

public override void OnInspectorGUI(){
DrawDefaultInspector ();
}
}
12 changes: 12 additions & 0 deletions Assets/Editor/SnapScrollRectEditor.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Assets/Main.unity
Binary file not shown.
8 changes: 8 additions & 0 deletions Assets/Main.unity.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

113 changes: 113 additions & 0 deletions Assets/SnapScrollRect.cs
@@ -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);
}
}
12 changes: 12 additions & 0 deletions Assets/SnapScrollRect.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added ProjectSettings/AudioManager.asset
Binary file not shown.
Binary file added ProjectSettings/ClusterInputManager.asset
Binary file not shown.
Binary file added ProjectSettings/DynamicsManager.asset
Binary file not shown.
Binary file added ProjectSettings/EditorBuildSettings.asset
Binary file not shown.
Binary file added ProjectSettings/EditorSettings.asset
Binary file not shown.
Binary file added ProjectSettings/GraphicsSettings.asset
Binary file not shown.
Binary file added ProjectSettings/InputManager.asset
Binary file not shown.
Binary file added ProjectSettings/NavMeshAreas.asset
Binary file not shown.
Binary file added ProjectSettings/NetworkManager.asset
Binary file not shown.
Binary file added ProjectSettings/Physics2DSettings.asset
Binary file not shown.
Binary file added ProjectSettings/ProjectSettings.asset
Binary file not shown.
2 changes: 2 additions & 0 deletions ProjectSettings/ProjectVersion.txt
@@ -0,0 +1,2 @@
m_EditorVersion: 5.3.4f1
m_StandardAssetsVersion: 0
Binary file added ProjectSettings/QualitySettings.asset
Binary file not shown.
Binary file added ProjectSettings/TagManager.asset
Binary file not shown.
Binary file added ProjectSettings/TimeManager.asset
Binary file not shown.
Binary file added ProjectSettings/UnityAdsSettings.asset
Binary file not shown.
Binary file added ProjectSettings/UnityConnectSettings.asset
Binary file not shown.

0 comments on commit 1f359d5

Please sign in to comment.