2025-03-10 10:18:11 +08:00

80 lines
2.7 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
/*******************************************************************************
*Create By CG
*Function 可移动控制的示例图版
*******************************************************************************/
namespace ZXK.ZPS
{
public class SampleCtrl : MonoBehaviour
{
[SerializeField]
private GameObject _canvas = null;
[SerializeField]
private string _checkGeoName = null;
//镜头拉近速度
private float _zoomSpeed = 30.0f;
private Vector2 _curPos = Vector2.zero;
private Vector3 _orgPos = Vector3.one;
private void OnEnable()
{
transform.localScale = Vector3.one;
_orgPos = transform.localPosition;
}
private void OnDisable()
{
transform.localScale = Vector3.one;
transform.localPosition = _orgPos;
}
private void Update()
{
if (_canvas != null && !string.IsNullOrEmpty(_checkGeoName))
{
GameObject selectGeo = GetOverUI();
if (selectGeo!=null&& selectGeo.name.Equals(_checkGeoName))
{
//缩放
transform.localScale += Vector3.one * Input.mouseScrollDelta.y * Time.deltaTime * _zoomSpeed;
if (transform.localScale.x < 0.3f)
{
transform.localScale = Vector3.one * 0.3f;
}
//位移
if (Input.GetMouseButtonDown(1))
{
_curPos = Input.mousePosition;
Debug.Log("Down:" + _curPos);
}
else if (Input.GetMouseButton(1))
{
Vector2 movePos = (Vector2)Input.mousePosition - _curPos;
transform.GetComponent<RectTransform>().anchoredPosition += movePos;
_curPos = Input.mousePosition;
Debug.Log("Ing:" + _curPos);
}
}
}
}
public GameObject GetOverUI()
{
PointerEventData pointerEventData = new PointerEventData(EventSystem.current);
pointerEventData.position = Input.mousePosition;
GraphicRaycaster gr = _canvas.GetComponent<GraphicRaycaster>();
List<RaycastResult> results = new List<RaycastResult>();
gr.Raycast(pointerEventData, results);
if (results.Count != 0)
{
return results[0].gameObject;
}
return null;
}
}
}