当前位置:   article > 正文

unity 鼠标拖动UI 滚轮缩放大小_ue4 鼠标缩放ui大小

ue4 鼠标缩放ui大小

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. using UnityEngine.UI;
  7. /// <summary>
  8. /// 鼠标拖动UI 滚轮缩放大小
  9. /// </summary>
  10. public class PageDragToZoom : MonoBehaviour, IPointerExitHandler, IPointerEnterHandler, IBeginDragHandler, IDragHandler
  11. {
  12. /// <summary>
  13. /// 缩放速度
  14. /// </summary>
  15. private float wheelSpeed = 0.1f;
  16. /// <summary>
  17. /// 是否缩放(鼠标进入退出 判断)
  18. /// </summary>
  19. public bool isTrue;
  20. Vector3 _originScale;//初始大小
  21. Vector3 _originPoint;//初始位置
  22. public Button _closeButton;//复位
  23. Vector3 offPos;
  24. Vector3 arragedPos;
  25. //Vector3 _scaleOne;
  26. //public Transform _ThisTransform;
  27. void Start()
  28. {
  29. _originPoint = this.transform.position;
  30. _originScale = this.transform.localScale;
  31. _closeButton.onClick.AddListener(CloseImage);
  32. //_scaleOne = transform.localScale;
  33. }
  34. void Update()
  35. {
  36. //UI最大可以放到两倍
  37. if (transform.localScale.x >= 2 || transform.localScale.x >= 2)
  38. {
  39. transform.localScale = new Vector3(2, 2, 2);
  40. }
  41. //UI最小可以缩小两倍
  42. if (transform.localScale.x <= 0.2f || transform.localScale.x <= 0.2f)
  43. {
  44. transform.localScale = new Vector3(0.2f, 0.2f, 0.2f);
  45. }
  46. //通过鼠标滚轮键来对照片进行缩放
  47. if (isTrue == true)
  48. {
  49. // Debug.Log("通过鼠标滚轮键来对照片进行缩放");
  50. transform.localScale += new Vector3(Input.mouseScrollDelta.y * wheelSpeed, Input.mouseScrollDelta.y * wheelSpeed, Input.mouseScrollDelta.y * wheelSpeed);
  51. }
  52. }
  53. /// <summary>
  54. /// 回到初始状态
  55. /// </summary>
  56. private void CloseImage()
  57. {
  58. this.transform.position = _originPoint;
  59. this.transform.localScale = _originScale;
  60. }
  61. /// <summary>
  62. /// 鼠标进入
  63. /// </summary>
  64. /// <param name="eventData"></param>
  65. public void OnPointerEnter(PointerEventData eventData)
  66. {
  67. isTrue = true;
  68. }
  69. /// <summary>
  70. /// 鼠标退出
  71. /// </summary>
  72. /// <param name="eventData"></param>
  73. public void OnPointerExit(PointerEventData eventData)
  74. {
  75. isTrue = false;
  76. }
  77. /// <summary>
  78. /// 开始拖拽的时候
  79. /// </summary>
  80. /// <param name="eventData"></param>
  81. public void OnBeginDrag(PointerEventData eventData)
  82. {
  83. if (RectTransformUtility.ScreenPointToWorldPointInRectangle(transform.GetComponent<RectTransform>(), Input.mousePosition
  84. , eventData.enterEventCamera, out arragedPos))
  85. {
  86. offPos = transform.position - arragedPos;
  87. }
  88. }
  89. /// <summary>
  90. /// 拖拽中
  91. /// </summary>
  92. /// <param name="eventData"></param>
  93. public void OnDrag(PointerEventData eventData)
  94. {
  95. transform.position = offPos + Input.mousePosition;
  96. }
  97. }

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号