ButtonInsideScrollList.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="ButtonInsideScrollList.cs" company="Exit Games GmbH">
  3. // Part of: Photon Unity Utilities,
  4. // </copyright>
  5. // <summary>
  6. // Used on Buttons inside UI lists to prevent scrollRect parent to scroll when down on buttons.
  7. // </summary>
  8. // <author>developer@exitgames.com</author>
  9. // --------------------------------------------------------------------------------------------------------------------
  10. using UnityEngine;
  11. using UnityEngine.EventSystems;
  12. using UnityEngine.UI;
  13. namespace Photon.Pun.UtilityScripts
  14. {
  15. /// <summary>
  16. /// Button inside scroll list will stop scrolling ability of scrollRect container, so that when pressing down on a button and draggin up and down will not affect scrolling.
  17. /// this doesn't do anything if no scrollRect component found in Parent Hierarchy.
  18. /// </summary>
  19. public class ButtonInsideScrollList : MonoBehaviour, IPointerDownHandler, IPointerUpHandler {
  20. ScrollRect scrollRect;
  21. // Use this for initialization
  22. void Start () {
  23. scrollRect = GetComponentInParent<ScrollRect>();
  24. }
  25. #region IPointerDownHandler implementation
  26. void IPointerDownHandler.OnPointerDown (PointerEventData eventData)
  27. {
  28. if (scrollRect !=null)
  29. {
  30. scrollRect.StopMovement();
  31. scrollRect.enabled = false;
  32. }
  33. }
  34. #endregion
  35. #region IPointerUpHandler implementation
  36. void IPointerUpHandler.OnPointerUp (PointerEventData eventData)
  37. {
  38. if (scrollRect !=null && !scrollRect.enabled)
  39. {
  40. scrollRect.enabled = true;
  41. }
  42. }
  43. #endregion
  44. }
  45. }