基于Ugui的窗口拖拽功能

用惯电脑的同学都知道,PC上面的窗口有个最大的特点就是可以拖动,随便拖动,很方便,但游戏中也要有的东西能够拖动,因为要做这个功能,所以就慢慢写出来了,下面写个博客来记录下拖拽功能的实现!

首先要在场景里面建立一张图片,或者butten,只要能显示的就可以,然后写下一个这样的脚本:

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

public class Chick : MonoBehaviour ,IDragHandler,IPointerDownHandler,IPointerUpHandler{
    public void OnDrag(PointerEventData eventData)
    {
        GetComponent<RectTransform>().pivot.Set(0, 0);
        transform.position = Input.mousePosition;
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        transform.localScale = new Vector3(0.7f, 0.7f, 0.7f);
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        transform.localScale = new Vector3(1f, 1f, 1f);
    }
}
然后将这个脚本挂在你想拖动的图片或者butten上面就可以实现拖拽功能了。

这个脚本的效果还是很不错的,因为有一个大小的变换,整体不错,好本期教程到此结束GoodLuck!

猜你喜欢

转载自blog.csdn.net/Superficialtise/article/details/72843454