zoukankan      html  css  js  c++  java
  • 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。

  • 相关阅读:
    C# 如何生成CHM帮助文件
    Excel导出问题
    JS一些类实现方式的性能研究
    Date对象的一些相关函数
    ECMASCRIPT5新特性(转载)
    javascript apo
    $linq A Javascript LINQ library
    javascript 编程规范
    flash 工程师的标准
    flash 弹出 网页
  • 原文地址:https://www.cnblogs.com/chongxin/p/4445215.html
Copyright © 2011-2022 走看看