NGUI 自定义 Drag Item Script

 

  最近要实现一个NGUI效果。

  

  

 

 

   查看了一下,NGUI有个自带 UIDragDropItem.cs 的组件进过修改后即可以实现。

   下面贴上UI布局,代码:

  

     

  mDragDropItem.cs
 1 using UnityEngine;
 2 using System;
 3 using System.Collections;
 4 
 5 public class mDragDropItem : UIDragDropItem
 6 {
 7     public BingZhong_Id bingzhong_id;
 8 
 9     protected override void OnDragDropStart()
10     {
11         base.OnDragDropStart();
12 
13         ///一旦被克隆,克隆体设置新的参数
14         ///表示 克隆体移动 是直接移动本体
15         if (this.cloneOnDrag)
16         {
17             this.cloneOnDrag = false;
18             this.restriction = Restriction.None;
19             Destroy(GetComponent<UIDragScrollView>());
20         }
21     }
22 
23     protected override void OnDragDropRelease(GameObject surface)
24     {
25         // Is there a droppable container?
26         UIDragDropContainer container = surface ? NGUITools.FindInParents<UIDragDropContainer>(surface) : null;
27 
28         //检测到容器
29         if (container != null)
30         {
31             //clear child
32             foreach (Transform item in container.transform)
33             {
34                 Destroy(item.gameObject);
35             }
36 
37 
38             mTouchID = int.MinValue;
39 
40             // Re-enable the collider
41             if (mButton != null) mButton.isEnabled = true;
42             else if (mCollider != null) mCollider.enabled = true;
43 
44             mTrans.parent = (container.reparentTarget != null) ? container.reparentTarget : container.transform;
45             mTrans.localPosition = Vector3.zero;
46 
47             // Update the grid and table references
48             mParent = mTrans.parent;
49 
50             // Notify the widgets that the parent has changed
51             NGUITools.MarkParentAsChanged(gameObject);
52         }
53         //没有检测到
54         else
55         {
56             NGUITools.Destroy(gameObject);
57         }
58     }
59 }

 

 

  

     

  在本体上设置如上图的参数, 这样你拖出来就可以是克隆体,原来Grid的Item不变。

  但是 克隆体自己 要重新设置参数,

  参数设置在 OnDragDropStart() 函数中

     (注意:OnDragDropStart() 函数是由本体调用克隆体的, 本体不会调用自己的 OnDragDropStart(), 所以你不用担心你在修改本体的参数)

     

      

  

      拖放结束,克隆体 调用 OnDragDropRelease(GameObject surface) 方法,

  GameObject surface 表示释放后 NGUI射线 检测到的碰撞体,

    如果是NULL或者 没有检测到UIDragDropContainer,则销毁自己,

    不然如果有 UIDragDropContainer,则重新设置自己的位置,重新设置自己的parent。

转载于:https://www.cnblogs.com/chongxin/p/4445215.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值