You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.2 KiB

using UnityEngine.EventSystems;
using UnityEngine;
namespace Level24
{
public class RawImageCtrl : MonoBehaviour,IDragHandler,IPointerDownHandler,IPointerUpHandler
{
[Header("脚本所控制的物体,为空则控制本身")]
public RectTransform ctrlTrans=null;
public int mouseButton_Darg = 0;
private Vector2 mouseClickPos;
private Vector2 tempDargPos;
private float scaleValue = 1;
public float maxScaleValue;
public float minScaleValue;
public void OnDrag(PointerEventData eventData)
{
if (Input.GetMouseButton(mouseButton_Darg))
{
tempDargPos = Input.mousePosition;
float distanceX = tempDargPos.x - mouseClickPos.x;
float distanceY = tempDargPos.y - mouseClickPos.y;
mouseClickPos = tempDargPos;
if (ctrlTrans == null)
{
this.GetComponent<RectTransform>().position += new Vector3(distanceX,distanceY,0);
}
else
{
ctrlTrans.position += new Vector3(distanceX, distanceY,0);
}
}
}
public void OnPointerDown(PointerEventData eventData)
{
mouseClickPos = Input.mousePosition;
}
public void OnPointerUp(PointerEventData eventData)
{
mouseClickPos = Vector2.zero;
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetAxis("Mouse ScrollWheel") != 0)
{
scaleValue += Input.GetAxis("Mouse ScrollWheel");
scaleValue = Mathf.Clamp(scaleValue, minScaleValue, maxScaleValue);
if (ctrlTrans != null)
{
ctrlTrans.transform.localScale = new Vector3(1 * scaleValue, 1 * scaleValue, 1 * scaleValue);
}
else
{
this.transform.localScale= new Vector3(1 * scaleValue, 1 * scaleValue, 1 * scaleValue);
}
}
}
}
}