Skip to content

Commit

Permalink
Scroll to Index
Browse files Browse the repository at this point in the history
  • Loading branch information
qiankanglai committed Jan 3, 2018
1 parent 8f73498 commit 512c83c
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 8 deletions.
6 changes: 0 additions & 6 deletions Assets/Scripts/EasyObjectPool/EasyObjectPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using UnityEngine;
using System.Collections.Generic;


namespace SG
{
[DisallowMultipleComponent]
Expand All @@ -33,15 +32,12 @@ class Pool

//the root obj for unused obj
private GameObject rootObj;
private float lastUsedTime = -1;
private PoolInflationType inflationType;
private string poolName;
private int objectsInUse = 0;

public Pool(string poolName, GameObject poolObjectPrefab, GameObject rootPoolObj, int initialCount, PoolInflationType type)
{
lastUsedTime = Time.time;

if (poolObjectPrefab == null)
{
#if UNITY_EDITOR
Expand Down Expand Up @@ -92,8 +88,6 @@ private void populatePool(int initialCount)
//o(1)
public GameObject NextAvailableObject(bool autoActive)
{
lastUsedTime = Time.time;

PoolObject po = null;
if (availableObjStack.Count > 1)
{
Expand Down
15 changes: 15 additions & 0 deletions Assets/Scripts/Editor/LoopScrollRectInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
[CustomEditor(typeof(LoopScrollRect), true)]
public class LoopScrollRectInspector : Editor
{
int index = 0;
float speed = 1000;
public override void OnInspectorGUI ()
{
base.OnInspectorGUI();
EditorGUILayout.Space();

LoopScrollRect scroll = (LoopScrollRect)target;
GUI.enabled = Application.isPlaying;

EditorGUILayout.BeginHorizontal();
if(GUILayout.Button("Clear"))
{
Expand All @@ -29,5 +33,16 @@ public override void OnInspectorGUI ()
scroll.RefillCellsFromEnd();
}
EditorGUILayout.EndHorizontal();

EditorGUIUtility.labelWidth = 45;
float w = (EditorGUIUtility.currentViewWidth - 100) / 2;
EditorGUILayout.BeginHorizontal();
index = EditorGUILayout.IntField("Index", index, GUILayout.Width(w));
speed = EditorGUILayout.FloatField("Speed", speed, GUILayout.Width(w));
if(GUILayout.Button("Scroll", GUILayout.Width(45)))
{
scroll.SrollToCell(index, speed);
}
EditorGUILayout.EndHorizontal();
}
}
2 changes: 1 addition & 1 deletion Assets/Scripts/LoopHorizontalScrollRect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected override float GetSize(RectTransform item)

protected override float GetDimension(Vector2 vector)
{
return vector.x;
return -vector.x;
}

protected override Vector2 GetVector(float value)
Expand Down
94 changes: 93 additions & 1 deletion Assets/Scripts/LoopScrollRect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using UnityEngine.Events;
using UnityEngine.EventSystems;
using System;
using System.Collections;

namespace UnityEngine.UI
{
Expand Down Expand Up @@ -68,7 +69,7 @@ protected float contentSpacing
m_GridLayout = content.GetComponent<GridLayoutGroup>();
if (m_GridLayout != null)
{
m_ContentSpacing = GetDimension(m_GridLayout.spacing);
m_ContentSpacing = Mathf.Abs(GetDimension(m_GridLayout.spacing));
}
}
return m_ContentSpacing;
Expand Down Expand Up @@ -296,6 +297,69 @@ public void ClearCells()
}
}

public void SrollToCell(int index, float speed)
{
if(totalCount >= 0 && (index < 0 || index >= totalCount))
{
Debug.LogWarningFormat("invalid index {0}", index);
return;
}
if(speed <= 0)
{
Debug.LogWarningFormat("invalid speed {0}", speed);
return;
}
StopAllCoroutines();
StartCoroutine(ScrollToCellCoroutine(index, speed));
}

IEnumerator ScrollToCellCoroutine(int index, float speed)
{
bool needMoving = true;
while(needMoving)
{
yield return null;
if(!m_Dragging)
{
float move = 0;
if(index < itemTypeStart)
{
move = -Time.deltaTime * speed;
}
else if(index >= itemTypeEnd)
{
move = Time.deltaTime * speed;
}
else
{
m_ViewBounds = new Bounds(viewRect.rect.center, viewRect.rect.size);
var m_ItemBounds = GetBounds4Item(index);
var offset = 0.0f;
if (directionSign == -1)
offset = reverseDirection ? (m_ViewBounds.min.y - m_ItemBounds.min.y) : (m_ViewBounds.max.y - m_ItemBounds.max.y);
else if (directionSign == 1)
offset = reverseDirection ? (m_ItemBounds.max.x - m_ViewBounds.max.x) : (m_ItemBounds.min.x - m_ViewBounds.min.x);
float maxMove = Time.deltaTime * speed;
if(Mathf.Abs(offset) < maxMove)
{
needMoving = false;
move = offset;
}
else
move = Mathf.Sign(offset) * maxMove;
}
if (move != 0)
{
Vector2 offset = GetVector(move);
content.anchoredPosition += offset;
m_PrevPosition += offset;
m_ContentStartPosition += offset;
}
}
}
StopMovement();
}

public void RefreshCells()
{
if (Application.isPlaying && this.isActiveAndEnabled)
Expand Down Expand Up @@ -1153,6 +1217,34 @@ private Bounds GetBounds()
return bounds;
}

private Bounds GetBounds4Item(int index)
{
if (m_Content == null)
return new Bounds();

var vMin = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
var vMax = new Vector3(float.MinValue, float.MinValue, float.MinValue);

var toLocal = viewRect.worldToLocalMatrix;
int offset = index - itemTypeStart;
if (offset < 0 || offset >= m_Content.childCount)
return new Bounds();
var rt = m_Content.GetChild(offset) as RectTransform;
if (rt == null)
return new Bounds();
rt.GetWorldCorners(m_Corners);
for (int j = 0; j < 4; j++)
{
Vector3 v = toLocal.MultiplyPoint3x4(m_Corners[j]);
vMin = Vector3.Min(v, vMin);
vMax = Vector3.Max(v, vMax);
}

var bounds = new Bounds(vMin, Vector3.zero);
bounds.Encapsulate(vMax);
return bounds;
}

private Vector2 CalculateOffset(Vector2 delta)
{
Vector2 offset = Vector2.zero;
Expand Down

0 comments on commit 512c83c

Please sign in to comment.