2025-01-02 12:15:45 +08:00

91 lines
3.0 KiB
C#

using CG.Framework;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
/*******************************************************************************
*Create By CG
*Function 可移动UI
*******************************************************************************/
namespace CG.UTility
{
public class SampleUICtrl : MonoBehaviour
{
private GameObject _canvas = null;
//public GameObject _CheckGeo = null;
//镜头拉近速度
private float _zoomSpeed = 30.0f;
private Vector2 _curPos = Vector2.zero;
private Vector3 _orgPos = Vector3.one;
private void OnEnable()
{
//_CheckGeo = transform.name;
_canvas = GameObject.FindGameObjectWithTag("MainCanvas");
transform.localScale = Vector3.one;
_orgPos = transform.localPosition;
}
private void OnDisable()
{
transform.localScale = Vector3.one;
transform.localPosition = _orgPos;
}
private void Update()
{
if (_canvas != null)
{
//GameObject selectGeo = GetOverUI();
if (GetOverUI(gameObject)) // 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(0))
{
_curPos = Input.mousePosition;
//WDebug.Log("Down:" + _curPos);
}
else if (Input.GetMouseButton(0))
{
Vector2 movePos = (Vector2)Input.mousePosition - _curPos;
transform.GetComponent<RectTransform>().anchoredPosition += movePos;
_curPos = Input.mousePosition;
//WDebug.Log("Ing:" + _curPos);
}
}
}
}
public bool GetOverUI(GameObject hoverGeo)
{
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);
for (int i = 0; i < results.Count; i++)
{
if(results[i].gameObject==hoverGeo)
{
return true;
}
}
return false;
//if (results.Count != 0)
//{
// return results[0].gameObject;
//}
//return null;
}
}
}