53 lines
1.8 KiB
C#
53 lines
1.8 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
|
|
// this is only a simple example code to show the zoomming is possible with the correct hierarchy, please, use a more reliable code in your project
|
|
namespace MeadowGames.UINodeConnect4.SampleScene.ScrollViewGraph
|
|
{
|
|
public class Zoom : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
|
|
{
|
|
bool _mouseIsOver;
|
|
public RectTransform contentRectTransform;
|
|
public GraphManager graphManager;
|
|
|
|
void Start()
|
|
{
|
|
graphManager = GetComponentInParent<GraphManager>();
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (_mouseIsOver)
|
|
{
|
|
if (Input.mouseScrollDelta.y != 0)
|
|
{
|
|
// 计算新的缩放比例
|
|
Vector3 newScale = contentRectTransform.localScale + Vector3.one * Input.mouseScrollDelta.y * 0.2f;
|
|
|
|
// 将缩放值限制在 0.6 到 1.6 之间
|
|
newScale.x = Mathf.Clamp(newScale.x, 0.6f, 1.6f);
|
|
newScale.y = Mathf.Clamp(newScale.y, 0.6f, 1.6f);
|
|
newScale.z = Mathf.Clamp(newScale.z, 0.6f, 1.6f);
|
|
|
|
// 应用新的缩放比例
|
|
contentRectTransform.localScale = newScale;
|
|
|
|
// 更新 connectionDetectionDistance
|
|
graphManager.connectionDetectionDistance = 15 * contentRectTransform.localScale.x;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void OnPointerEnter(PointerEventData eventData)
|
|
{
|
|
_mouseIsOver = true;
|
|
}
|
|
|
|
public void OnPointerExit(PointerEventData eventData)
|
|
{
|
|
_mouseIsOver = false;
|
|
}
|
|
}
|
|
} |