栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > Unity3D

Unity-点击和拖拽手势总结

Unity3D 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Unity-点击和拖拽手势总结

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


//把这个脚本挂在想使用的gameobject上即可
public class Demo : MonoBehaviour, IPointerClickHandler, IPointerDownHandler, IPointerUpHandler, IDragHandler, IBeginDragHandler, IEndDragHandler
{

    //点击和拖拽总的触发顺序
    //01 OnPointerDown
    //02 OnBeginDrag
    //03 OnDrag
    //04 OnPointerUp
    //05 OnPointerClick
    //06 OnEndDrag

    //只处理拖拽的触发顺序,先触发OnBeginDrag, 再触发OnDrag,最后触发OnEndDrag
    //开始拖动
    public void OnBeginDrag(PointerEventData eventData)
    {
        //PointerEventData.delta的含义是自上次更新以来的指针坐标增量变化变化。
        this.GetComponent().anchoredPosition += eventData.delta;
        //目前设的世界坐标和屏幕坐标一致,所以此时anchoredPosition左下角为(0,0)。右上角为屏幕的最大值。
        Debug.Log("OnBeginDrag anchoredPosition.x = " + this.GetComponent().anchoredPosition.x);
    }

    //拖动中
    public void OnDrag(PointerEventData eventData)
    {
        this.GetComponent().anchoredPosition += eventData.delta;
        //目前设的世界坐标和屏幕坐标一致,所以此时anchoredPosition左下角为(0,0)。右上角为屏幕的最大值。
        Debug.Log("OnDrag anchoredPosition.x = " + this.GetComponent().anchoredPosition.x);

    }

    //拖动结束后
    public void OnEndDrag(PointerEventData eventData)
    {
        this.GetComponent().anchoredPosition += eventData.delta;
        //目前设的世界坐标和屏幕坐标一致,所以此时anchoredPosition左下角为(0,0)。右上角为屏幕的最大值。
        Debug.Log("OnEndDrag anchoredPosition.x = " + this.GetComponent().anchoredPosition.x);
    }


    //只处理点击的触发顺序,先触发OnPointerDown, 再触发OnPointerUp,最后触发OnPointerClick
    //手指按下
    public void OnPointerDown(PointerEventData eventData)
    {
        Debug.Log("OnPointerDown");
    }
    //手指抬起
    public void OnPointerUp(PointerEventData eventData)
    {
        Debug.Log("OnPointerUp");
    }
    //点击
    public void OnPointerClick(PointerEventData eventData)
    {
        Debug.Log("OnPointerClick");
    }
}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/900797.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号