ScrollInformation.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine.UI;
  4. using UnityEngine;
  5. using System;
  6. using Sirenix.OdinInspector;
  7. using UnityEngine.EventSystems;
  8. public class ScrollInformation : MonoBehaviour,IPointerEnterHandler,IPointerExitHandler
  9. {
  10. [LabelText("速度")]
  11. public float Speed;
  12. [LabelText("要滚动的目标")]
  13. public RectTransform Information;
  14. //移动物体与父物体宽度差值
  15. float tmpWidth = 0;
  16. /// <summary>
  17. /// 开启移动
  18. /// </summary>
  19. bool openMove;
  20. void Start()
  21. {
  22. RectTransform tmpParentRectTransform = transform.GetComponent<RectTransform>();
  23. tmpWidth = Information.rect.width - tmpParentRectTransform.rect.width;
  24. }
  25. void FixedUpdate()
  26. {
  27. ScrollResult();
  28. }
  29. void ScrollResult()
  30. {
  31. if (!openMove) return;
  32. if (tmpWidth > 0)
  33. {
  34. float tmpX = Information.anchoredPosition.x - Time.fixedDeltaTime * Speed;
  35. Information.SetLocalPositionX(tmpX);
  36. if (Information.anchoredPosition.x < -tmpWidth)
  37. {
  38. Information.SetLocalPositionX(0);
  39. }
  40. }
  41. }
  42. public void OnPointerEnter(PointerEventData eventData)
  43. {
  44. openMove = true;
  45. }
  46. public void OnPointerExit(PointerEventData eventData)
  47. {
  48. openMove = false;
  49. Information.SetLocalPositionX(0);
  50. }
  51. }