unity UGUI对image控件检测鼠标按下和抬起

在UGUI中对image控件检测鼠标按下和抬起使用OnPointerDown和OnPointerUp方法

其中OnPointerDown方法需要类继承IPointerDownHandler接口,而OnPointerUp方法需要类继承IPointerUpHandler接口。

  • OnPointerDown方法
public void OnPointerDown(PointerEventData eventData)
{

}
  • OnPointerUp方法
public void OnPointerUp(PointerEventData eventData) 
{

}

当鼠标在控件上按下时,会触发OnPointerDown方法;当鼠标抬起时会会触发OnPointerUp方法。
- 当需要使用这两个方法时,需要调用using UnityEngine.EventSystems;


希望完成的效果
这里写图片描述
鼠标按住“开始”控件并下滑时,检测该控件的位置,当移动的距离超过一半时如果松开,控件会自动移动到背景控件下方,若为超过距离的一半,松开鼠标,控件会返回上端。
1. 为背景控件添加ScrollRect方法,并将content属性设置为开始控件。
2. 设置ScrollRect方法,勾选Vertical,使开始控件只能上下移动。
3. 通过代码控制开始控件的位置,判断不同位置控件的变化。
源代码如下

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class StartButton : MonoBehaviour,IPointerDownHandler,IPointerUpHandler{

    private Transform ButtonStart;
    private Image ButtonImage;

    private bool isPointerDown;
    private Vector3 InitMousePos;


    void Awake() {
        ButtonStart = this.transform.Find("Start").transform;
        ButtonImage=ButtonStart.GetComponent<Image>();
    }
    void Start() {
        ButtonStart.localPosition = new Vector3(ButtonStart.localPosition.x, 60f, ButtonStart.transform.localPosition.z);
        InitMousePos = Vector3.zero;
    }
    void Update() {
        UpdateButton();
    }

    //根据Y值来改变游戏状态
    private void UpdateButton()
    {
        if (isPointerDown)
        {
            if (ButtonStart.localPosition.y > 60f || ButtonStart.localPosition.y < -60f)
            {
                float newY = (Mathf.Abs(ButtonStart.localPosition.y) / ButtonStart.localPosition.y) * 60f;
                if (newY <= 0) {
                    ButtonImage.color = new Color(104, 255, 0, 255);
                }
                ButtonStart.localPosition = new Vector3(ButtonStart.localPosition.x, newY, ButtonStart.transform.localPosition.z);
            }
        }
        else {
            float y = ButtonStart.localPosition.y;
            if (y <= 0)
            {
                ButtonStart.localPosition = new Vector3(ButtonStart.localPosition.x, -60f, ButtonStart.transform.localPosition.z);
                ButtonImage.color = new Color(104, 255, 0, 255);
            }
            else
            {
                ButtonStart.localPosition = new Vector3(ButtonStart.localPosition.x, 60f, ButtonStart.transform.localPosition.z);
            }
            if (ButtonStart.localPosition.y == -60f) {
                this.GetComponent<ScrollRect>().enabled = false;
                StartCoroutine(WaitAndSkip());
            }

        }
    }

    //控制场景等待、跳转
    private IEnumerator WaitAndSkip() {
        yield return new WaitForSeconds(0.5f);
        Application.LoadLevel(1);
    }


    //检测鼠标按下与抬起
    public void OnPointerDown(PointerEventData eventData)
    {
        isPointerDown = true;
    }
    public void OnPointerUp(PointerEventData eventData) {
        isPointerDown = false;
    }
}

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值