unity 当中UI的通过鼠标滚轮来进行缩放,并且约束放大缩小的范围

unity 当中UI的通过鼠标滚轮来进行缩放,并且约束放大缩小的范围```

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class UIScaleContor : MonoBehaviour, IPointerExitHandler, IPointerEnterHandler, IBeginDragHandler, IDragHandler
{
public float wheelSpeed;
public bool isTrue;
Vector3 _originScale;
Vector3 _originPoint;
public Button _closeButton;
Vector3 _scaleOne;
public void OnPointerEnter(PointerEventData eventData)
{
isTrue = true;
}

public void OnPointerExit(PointerEventData eventData)
{
    isTrue = false;
}

// Start is called before the first frame update
void Start()
{
    _originPoint = this.transform.position;
    _originScale = this.transform.localScale;
    _closeButton.onClick.AddListener(CloseImage);
    _scaleOne = transform.localScale;
}

private void CloseImage()
{
    this.transform.position = _originPoint;
    this.transform.localScale = _originScale;

}

// Update is called once per frame
void Update()
{
    //UI最大可以放到两倍
    if (transform.localScale.x >= 2 || transform.localScale.x >= 2)
    {
        transform.localScale = new Vector3(2, 2, 2);
    }

//UI最小可以缩小两倍
if (transform.localScale.x <= 0.5f || transform.localScale.x <= 0.5f)
{
transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
}
//通过鼠标滚轮键来对照片进行缩放
if (isTrue == true)
{
transform.localScale += new Vector3(Input.mouseScrollDelta.y * wheelSpeed, Input.mouseScrollDelta.y * wheelSpeed, Input.mouseScrollDelta.y * wheelSpeed);
}

}

private Image img;
Vector3 offPos;
Vector3 arragedPos; 
/// 
/// 开始拖拽的时候
/// 
/// 
public void OnBeginDrag(PointerEventData eventData)
{
    if (RectTransformUtility.ScreenPointToWorldPointInRectangle(transform.GetComponent(), Input.mousePosition
 , eventData.enterEventCamera, out arragedPos))
    {
        offPos = transform.position - arragedPos;
    }
}
/// 
/// 拖拽中
/// 
/// 
public void OnDrag(PointerEventData eventData)
{
    transform.position = offPos + Input.mousePosition;

}

}


```javascript
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class YeMianSuoFang : MonoBehaviour, IPointerExitHandler, IPointerEnterHandler, IBeginDragHandler, IDragHandler
{
    public float wheelSpeed;
    public bool isTrue;
    Vector3 _originScale;
    Vector3 _originPoint;
    public Button _closeButton;
    Vector3 _scaleOne;
    public void OnPointerEnter(PointerEventData eventData)
    {
        isTrue = true;
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        isTrue = false;
    }

    // Start is called before the first frame update
    void Start()
    {
        _originPoint = this.transform.position;
        _originScale = this.transform.localScale;
        _closeButton.onClick.AddListener(CloseImage);
        _scaleOne = transform.localScale;
    }

    private void CloseImage()
    {
        this.transform.position = _originPoint;
        this.transform.localScale = _originScale;

    }

    // Update is called once per frame
    void Update()
    {
    //UI最大可以放到两倍
        if (transform.localScale.x >= 2 || transform.localScale.x >= 2)
        {
            transform.localScale = new Vector3(2, 2, 2);
        }
        //UI最小可以缩小两倍
        if (transform.localScale.x <= 0.5f || transform.localScale.x <= 0.5f)
        {
            transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
        }
        //通过鼠标滚轮键来对照片进行缩放
        if (isTrue == true)
        {
            transform.localScale += new Vector3(Input.mouseScrollDelta.y * wheelSpeed, Input.mouseScrollDelta.y * wheelSpeed, Input.mouseScrollDelta.y * wheelSpeed);
        }

    }
   
    private Image img;
    Vector3 offPos;
    Vector3 arragedPos;
    /// 
    /// 开始拖拽的时候
    /// 
    /// 
    public void OnBeginDrag(PointerEventData eventData)
    {
        if (RectTransformUtility.ScreenPointToWorldPointInRectangle(transform.GetComponent(), Input.mousePosition
     , eventData.enterEventCamera, out arragedPos))
        {
            offPos = transform.position - arragedPos;
        }
    }
    /// 
    /// 拖拽中
    /// 
    /// 
    public void OnDrag(PointerEventData eventData)
    {
        transform.position = offPos + Input.mousePosition;

    }


}

你可能感兴趣的:(#,摄像机,unity3d)