NGUI中实现Sprite拖拽并复位的功能

利用NGUI插件实现Sprite拖拽并复位的功能。

效果如下图:


中间那个picture的图标被鼠标拖出去之后会自己回来原来的位置。

我实现的功能是往屏幕上方拖拽会跳转到下一个页面,往其他方向拖拽就会复位。

代码实现如下:

using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cloudy;//自己写的通讯库,不用管它


public class DragableCard : UIDragDropItem
{

    public GameObject ObjectScene;//01
    public GameObject SelectScene;//02    01和02是两个用于页面跳转的页面
    public GameObject scrollview;//一个滑动视图,没有在截图里,也不用管它
    public GameObject WhichObject;//判断拖拽的对象
    public UILabel main_label;//拖拽框里的label

    public GameObject mianban;//警告框面板

    string[] type = new string[] { "image", "word", "video", "ppt", "excel", "pdf", "res" };
    string str = string.Empty;//消息字符串

    //局部坐标系
    public Vector2 LocPos;
    public ItemAndLabel itembutton;

    //拖拽结束后触发的事件
    protected override void OnDragDropRelease(GameObject surface)
    {
        base.OnDragDropRelease(surface);
        //判断拖拽的位置 若符合则与客户端进行交互 且页面跳转

        //中间屏幕
        if (LocPos.x >= -450 && LocPos.x < 200 && LocPos.y >= 300) //
        {
            if (WhichObject.name == "image")
            {
                print("image");
                //选择按钮以后
                if (itembutton.flag == 1)
                {
                    if (GameMenuController.myArray[0, 0] == null || GameMenuController.myArray[0, 0] == "")
                    {
                        main_label.text = "no picture";
                        mianban.SetActive(true);
                        PositionRecovery();
                    }
                    else
                    {
                        main_label.text = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(GameMenuController.myArray[0, 0]));
                        str += type[0] + ":" + GameMenuController.myArray[0, 0];
                        ICloudy.Instance.SendUdpMsg(Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(str)));
                        print("image01:" + str);
                        str = null;
                        // changePanel();
                    }
                }
               
                PositionRecovery();
            }
        }

        //拖拽复位
        else
        {
            if (WhichObject.name == "image")
            {
                PositionRecovery();
            }
        }

    }
    //void changePanel()
    //{//页面跳转
    //    ObjectScene.SetActive(false);
    //    scrollview.SetActive(false);
    //    SelectScene.SetActive(true);
    //    PositionRecovery();
    //}
    void PositionRecovery()  //找到图标原来的位置,重新设置一下
    {
        //拖拽图标返回原来位置
        LocPos.x = -45;
        LocPos.y = 0;
        transform.localPosition = LocPos;
    }

    void Update()
    {
        LocPos = transform.localPosition;  //这个是为了找图标所在的位置
        //print("location:" + LocPos.x + "," + LocPos.y);  
    }



}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值