美文网首页
ugui拖拽

ugui拖拽

作者: 繁木成林 | 来源:发表于2017-12-21 23:49 被阅读23次
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class testfordrag : MonoBehaviour,IDragHandler,IPointerDownHandler,IPointerUpHandler,IEndDragHandler{

    public RectTransform canvas;
    private RectTransform textRect;
    Vector2 offset = new Vector2 ();

    void Start(){
        textRect = GetComponent<RectTransform> ();
    }
    public void OnPointerDown(PointerEventData eventData){
        Vector2 mouseDown = eventData.position;
        Vector2 mouseUguiPos = new Vector2 ();
        bool isRect = RectTransformUtility.ScreenPointToLocalPointInRectangle (canvas, mouseDown, eventData.enterEventCamera, out mouseUguiPos);
        if (isRect) {
            offset = textRect.anchoredPosition - mouseUguiPos;
        }
    }
    public void OnDrag(PointerEventData eventData)
    {
        Vector2 mouseDrag = eventData.position;
        Vector2 uguiPos = new Vector2 ();
        bool isRect = RectTransformUtility.ScreenPointToLocalPointInRectangle (canvas, mouseDrag, eventData.enterEventCamera, out uguiPos);
        if (isRect) {
            textRect.anchoredPosition = offset + uguiPos;
        }
    }
    public void OnPointerUp(PointerEventData eventData)
    {
        offset = Vector2.zero;
    }
    public void OnEndDrag(PointerEventData eventData)
    {
        offset = Vector2.zero;
    }
    void Update(){
        GetComponent<Text> ().text = GetComponentInParent<fighter> ().ShowCard ();
    }
}

相关文章

网友评论

      本文标题:ugui拖拽

      本文链接:https://www.haomeiwen.com/subject/jgwzwxtx.html