Created
May 4, 2018 11:32
-
-
Save xiaobin83/3f265e7b5964d840e4cf60a7bd4ccaf2 to your computer and use it in GitHub Desktop.
monitor op on scroll rect for behaviors like pull-down-reloading
This file contains hidden or 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
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.UI; | |
public class ScrollRectHelper : MonoBehaviour | |
{ | |
public ScrollRect scrollRect; | |
lua.LuaFunction eventDelegate; | |
UnityEngine.Events.UnityAction<Vector2> onValueChanged; | |
int movingCheckSize; | |
void Awake() | |
{ | |
onValueChanged = new UnityEngine.Events.UnityAction<Vector2>(OnValueChanged); | |
scrollRect.onValueChanged.AddListener(onValueChanged); | |
} | |
public void Setup(lua.LuaFunction eventDelegate, int movingCheckSize = 100) | |
{ | |
this.movingCheckSize = movingCheckSize; | |
if (this.eventDelegate != null) | |
this.eventDelegate.Dispose(); | |
if (eventDelegate != null) | |
this.eventDelegate = eventDelegate.Retain(); | |
else | |
this.eventDelegate = null; | |
} | |
Vector3[] temp1 = new Vector3[4]; | |
Bounds? viewportBounds; | |
/* | |
001| 010 | 100 | |
----+---------+---- | |
001 000| 010 000 | 100 000 | |
----+---------+---- | |
001 000 000| | 100 000 000 | |
010 000 000 | |
*/ | |
const int bLeft = 1; | |
const int bCenter = 2; | |
const int bRight = 4; | |
public int TestAgainstViewport(RectTransform rect) | |
{ | |
if (!viewportBounds.HasValue) | |
{ | |
var b = new Bounds(); | |
scrollRect.viewport.GetWorldCorners(temp1); | |
b.Encapsulate(temp1[0]); | |
b.Encapsulate(temp1[1]); | |
b.Encapsulate(temp1[2]); | |
b.Encapsulate(temp1[3]); | |
viewportBounds = b; | |
} | |
rect.GetWorldCorners(temp1); | |
var center = (temp1[0] + temp1[1] + temp1[2] + temp1[3]) / 4; | |
var min = viewportBounds.Value.min; | |
var max = viewportBounds.Value.max; | |
int result = 0; | |
if (center.x < min.x) | |
{ | |
result = bLeft; | |
} | |
else if (center.x > max.x) | |
{ | |
result = bRight; | |
} | |
else | |
{ | |
result = bCenter; | |
} | |
if (center.y < min.y) | |
{ | |
result = result << 6; | |
} | |
else if (center.y > max.y) | |
{ | |
// nothing here | |
} | |
else | |
{ | |
result = result << 3; | |
} | |
return result; | |
} | |
void OnDestroy() | |
{ | |
if (eventDelegate != null) | |
eventDelegate.Dispose(); | |
eventDelegate = null; | |
scrollRect.onValueChanged.RemoveListener(onValueChanged); | |
} | |
public void MoveToHead() | |
{ | |
if (scrollRect.vertical) | |
{ | |
scrollRect.verticalNormalizedPosition = 1; | |
} | |
else if (scrollRect.horizontal) | |
{ | |
} | |
} | |
Vector2? prevValue; | |
Vector2? markedValue; | |
void OnValueChanged(Vector2 value) | |
{ | |
if (eventDelegate == null) return; | |
if (scrollRect.vertical) | |
{ | |
if (markedValue.HasValue) | |
{ | |
var sizeY = scrollRect.content.sizeDelta.y; | |
var curPos = sizeY * value.y; | |
var markedPos = sizeY * markedValue.Value.y; | |
if (Mathf.Abs(curPos - markedPos) > movingCheckSize) | |
{ | |
if (value.y < prevValue.Value.y) | |
{ | |
var height = scrollRect.content.sizeDelta.y * value.y; | |
if (height < movingCheckSize) | |
{ | |
eventDelegate.Invoke("touchEnd"); | |
} | |
} | |
else if (value.y > prevValue.Value.y) | |
{ | |
var height = scrollRect.content.sizeDelta.y * (1 - value.y); | |
if (height < movingCheckSize) | |
{ | |
eventDelegate.Invoke("touchHead"); | |
} | |
} | |
if (value.y < prevValue.Value.y) | |
{ | |
eventDelegate.Invoke("movingTowardsEnd"); | |
} | |
else | |
{ | |
eventDelegate.Invoke("movingTowardsHead"); | |
} | |
markedValue = value; | |
} | |
} | |
else | |
{ | |
markedValue = value; | |
} | |
prevValue = value; | |
} | |
else if (scrollRect.horizontal) | |
{ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment