Skip to content

Instantly share code, notes, and snippets.

@anzfactory
Last active November 6, 2015 03:00
Show Gist options
  • Save anzfactory/f6e478c049241840afc0 to your computer and use it in GitHub Desktop.
Save anzfactory/f6e478c049241840afc0 to your computer and use it in GitHub Desktop.
ScrollRectのスクロールチェンジをユーザー操作以外の場合は上部固定にするやつ
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class ScrollController : MonoBehaviour, IInitializePotentialDragHandler, IEndDragHandler
{
public enum ScrollState {
Wait,
BeginDrag,
EndDrag
}
private ScrollState state;
private ScrollRect scrollRect;
private void Awake()
{
this.scrollRect = gameObject.GetComponent<ScrollRect>();
this.state = ScrollState.Wait;
}
private void Update()
{
if (this.state != ScrollState.EndDrag) {
return;
}
// ユーザー操作が終わっても惰性でスクロールは続くので止まるまでチェッキング
else if (this.scrollRect.velocity.Equals(Vector2.zero)) {
this.state = ScrollState.Wait;
}
}
// ScrollRect value changed
public void OnChangedScrollPosition(Vector2 position)
{
if (this.state == ScrollState.Wait) {
// ユーザーによる操作じゃないので止める
this.scrollRect.verticalNormalizedPosition = 1;
}
}
// IInitializePotentialDragHandler
public void OnInitializePotentialDrag(PointerEventData eventData)
{
this.state = ScrollState.BeginDrag;
}
// IEndDragHandler
public void OnEndDrag(PointerEventData data)
{
this.state = ScrollState.EndDrag;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment