DebugsOnScrollListener.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.EventSystems;
  4. // Listens to scroll events on the scroll rect that debug items are stored
  5. // and decides whether snap to bottom should be true or not
  6. //
  7. // Procedure: if, after a user input (drag or scroll), scrollbar is at the bottom, then
  8. // snap to bottom shall be true, otherwise it shall be false
  9. namespace IngameDebugConsole
  10. {
  11. public class DebugsOnScrollListener : MonoBehaviour, IScrollHandler, IBeginDragHandler, IEndDragHandler
  12. {
  13. public ScrollRect debugsScrollRect;
  14. public DebugLogManager debugLogManager;
  15. public void OnScroll( PointerEventData data )
  16. {
  17. if( IsScrollbarAtBottom() )
  18. debugLogManager.SetSnapToBottom( true );
  19. else
  20. debugLogManager.SetSnapToBottom( false );
  21. }
  22. public void OnBeginDrag( PointerEventData data )
  23. {
  24. debugLogManager.SetSnapToBottom( false );
  25. }
  26. public void OnEndDrag( PointerEventData data )
  27. {
  28. if( IsScrollbarAtBottom() )
  29. debugLogManager.SetSnapToBottom( true );
  30. else
  31. debugLogManager.SetSnapToBottom( false );
  32. }
  33. public void OnScrollbarDragStart( BaseEventData data )
  34. {
  35. debugLogManager.SetSnapToBottom( false );
  36. }
  37. public void OnScrollbarDragEnd( BaseEventData data )
  38. {
  39. if( IsScrollbarAtBottom() )
  40. debugLogManager.SetSnapToBottom( true );
  41. else
  42. debugLogManager.SetSnapToBottom( false );
  43. }
  44. private bool IsScrollbarAtBottom()
  45. {
  46. float scrollbarYPos = debugsScrollRect.verticalNormalizedPosition;
  47. if( scrollbarYPos <= 1E-6f )
  48. return true;
  49. return false;
  50. }
  51. }
  52. }