ScaleGestureController.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using FairyGUI;
  2. using GFGGame.Launcher;
  3. using UnityEngine;
  4. namespace GFGGame
  5. {
  6. public class ScaleGestureController : SingletonMonoBase<ScaleGestureController>
  7. {
  8. public Transform target; //目标
  9. //缩放
  10. public bool isScaling;
  11. Vector3 touch1, touch2, oriPos, scaleCenter;
  12. float scale, disX, disY, oriScale;
  13. public float scaleSpeed = 1;
  14. public float min = 1f;
  15. public float max = 1.7f;
  16. //移动
  17. private bool isDraging;
  18. private Vector3 offset = Vector3.zero;
  19. public Vector2 uiSize;
  20. private void Update()
  21. {
  22. if (target == null)
  23. {
  24. isScaling = false;
  25. isDraging = false;
  26. return;
  27. }
  28. if(Input.touchCount != 2)
  29. {
  30. isScaling = false;
  31. }
  32. if (isScaling)
  33. {
  34. //两指缩放比例
  35. scale = Vector3.Distance(Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position), Camera.main.ScreenToWorldPoint(Input.GetTouch(1).position)) / Vector3.Distance(touch1, touch2);
  36. //利用scaleSpeed控制缩放速度
  37. scale = (scale - 1) * scaleSpeed;
  38. var targetScale = oriScale + scale;
  39. //给缩放比例加限制
  40. if (targetScale <= min && scale < 0)
  41. {
  42. targetScale = min;
  43. }
  44. if (targetScale >= max && scale > 0)
  45. {
  46. targetScale = max;
  47. }
  48. LogUtil.LogDev($"scaling {oriScale} {scale}");
  49. //缩放目标大小
  50. target.localScale = new Vector3(targetScale, targetScale, 1);
  51. //改变目标位置,让位置保持不变
  52. target.position = new Vector3(oriPos.x - ((target.localScale.x - oriScale) * disX), oriPos.y - ((target.localScale.y - oriScale) * disY), 0);
  53. UpdatePos();
  54. }
  55. if (Input.touchCount == 2)
  56. {
  57. //两指点位
  58. touch1 = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
  59. touch2 = Camera.main.ScreenToWorldPoint(Input.GetTouch(1).position);
  60. //目标初始点位
  61. oriPos = new Vector3(target.position.x, target.position.y, 0);
  62. //两指中点
  63. scaleCenter = new Vector3((Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position).x + Camera.main.ScreenToWorldPoint(Input.GetTouch(1).position).x) / 2, (Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position).y + Camera.main.ScreenToWorldPoint(Input.GetTouch(1).position).y) / 2, 0);
  64. //两指中点和目标距离
  65. disX = scaleCenter.x - oriPos.x;
  66. disY = scaleCenter.y - oriPos.y;
  67. oriScale = target.localScale.x;
  68. LogUtil.LogDev($"start scale {oriPos} {oriScale}");
  69. isScaling = true;
  70. }
  71. if (!Input.GetMouseButton(0) || isScaling)
  72. {
  73. isDraging = false;
  74. return;
  75. }
  76. if (isDraging)
  77. {
  78. target.position = Camera.main.ScreenToWorldPoint(Input.mousePosition - offset);
  79. UpdatePos();
  80. }
  81. else if (Input.GetMouseButtonDown(0))
  82. {
  83. //LogUtil.LogEditor($"DragGestureController {GRoot.inst.touchTarget} {Input.touchCount}");
  84. if (GRoot.inst.touchTarget != null && !(GRoot.inst.touchTarget is GGraph)) return;
  85. isDraging = true;
  86. offset = Input.mousePosition - Camera.main.WorldToScreenPoint(target.position);
  87. }
  88. }
  89. public void UpdatePos()
  90. {
  91. if (target == null) return;
  92. Vector2 size = target.Find("Bg").GetComponent<SpriteRenderer>().size * target.localScale.x;
  93. float deviationWidth = (size.x - uiSize.x / 100) / 2;
  94. float deviationHeigh = (size.y - uiSize.y / 100) / 2;
  95. Vector2 pos = target.transform.position;
  96. if (pos.x <= -deviationWidth)
  97. {
  98. target.transform.position = new Vector2(-deviationWidth, target.transform.position.y);
  99. }
  100. if (pos.x >= deviationWidth)
  101. {
  102. target.transform.position = new Vector2(deviationWidth, target.transform.position.y);
  103. }
  104. if (pos.y <= -deviationHeigh)
  105. {
  106. target.transform.position = new Vector2(target.transform.position.x, -deviationHeigh);
  107. }
  108. if (pos.y >= deviationHeigh)
  109. {
  110. target.transform.position = new Vector2(target.transform.position.x, deviationHeigh);
  111. }
  112. }
  113. }
  114. }