Unity 上滑图片解锁

挂在图片上:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;

/// <summary>
/// **************************UGUI拖动图片,脚本挂在Image上即可*********************************
/// </summary>
/// 
public class TuoDongWuPin : MonoBehaviour
{
    /// <summary>
    /// 第一次按下的位置
    /// </summary>
    private Vector2 first = Vector2.zero;
    /// <summary>
    /// 鼠标的拖拽位置(第二次的位置)
    /// </summary>
    private Vector2 second = Vector2.zero;


    bool isDown = true;


    private void Start()
    {
        isDown = true;
    }

  
    private void Update()
    {
        if (this.GetComponent<RectTransform>().localPosition.y < 0)
        {
            this.GetComponent<RectTransform>().localPosition = new Vector3(0, 0, 0);
        }



        if (Input.GetMouseButtonDown(0))
        {
            first = Input.mousePosition;
            Debug.Log(first);

        }

        else if (Input.GetMouseButton(0))
        {
            second = Input.mousePosition;

            if (second.y < first.y && this.GetComponent<RectTransform>().localPosition.y > 0 && isDown)
            {

                this.transform.localPosition = new Vector3(0, second.y - first.y, 0);

            }

            if (second.y > first.y && isDown)
            {

                this.transform.localPosition = new Vector3(0, second.y - first.y, 0);

            }
        }

        else if (Input.GetMouseButtonUp(0))
        {
            if (UnityEngine.Screen.height / 2 <= this.GetComponent<RectTransform>().localPosition.y || this.GetComponent<RectTransform>().localPosition.y >= UnityEngine.Screen.height)
            {
                isDown = false;
                Vector3 endPn = new Vector3(0, UnityEngine.Screen.height, 0);

                this.GetComponent<RectTransform>().DOLocalMove(endPn, 1.5f);

                //this.GetComponent<RectTransform>().localPosition = endPn;
            }

            else if (UnityEngine.Screen.height / 2 > this.GetComponent<RectTransform>().localPosition.y)
            {

                Vector3 startPn = new Vector3(0, 0, 0);
                this.GetComponent<RectTransform>().DOLocalMove(startPn, 1.5f);
                //this.GetComponent<RectTransform>().localPosition = startPn;
            }
        }
    }

用到了Dotween,如果不用Dotween删掉打开注释代码即可使用。

展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读