背包效果-使用NGUI实现物品的拖拽效果Drag

效果实现如图

对象层级关系图


  • PacketCell - Right

    • 对象作为单元格背景
  • PacketContainer

    • 对象作为单元格容器
  • PacketLabel

    • 对象作为单元格物体
  • PacketCell - Left

    • 对象作为单元格背景
  • PacketContainer

    • 对象作为单元格容器
  • PacketLabel

    • 对象作为单元格物体
  • 'Label - Middle'

    • 用来显示当前文字处于哪个位置

物体能够被拖拽的几个条件

  • 碰撞器 BoxCollider
  • 拖拽功能 UIDragDropItem

创建第一个拖拽功能的空子类

using System;
using UnityEngine; /// <summary>
/// 第一个自己创建的拖拽功能
/// </summary>
public class MyFirstDragDropItem:UIDragDropItem
{ }

容器可以监测正在被拖拽物体是否到自己对象位置的几个条件

  • 碰撞器 BoxCollider
  • 容器功能 UIDragDropContainer

GMUser.cs

using System;

/// <summary>
/// 用户管理器
/// </summary>
public class GMUserManager
{
//存储当前正在玩游戏的玩家信息
private static GMUser user = null;
//公开访问器
public static GMUser User{
get {
if (GMUserManager.user == null) {
GMUserManager.user = new GMUser ();
} return GMUserManager.user;
}
}
} public class GMUser
{
//游戏用户的姓名
public string Name{set;get;}
public GMUser ()
{
//设置每个用户的默认姓名是Right
this.Name = "Right";
}
}

MyFirstDragDropItem.cs

using System;
using UnityEngine; /// <summary>
/// 第一个自己创建的拖拽功能
/// </summary>
public class MyFirstDragDropItem:UIDragDropItem
{ private GameObject sourceParent;
/// <summary>
/// 重写父类的拖拽开始函数
/// </summary>
protected override void OnDragDropStart ()
{
//当拖拽开始时存储原始的父对象
this.sourceParent = this.transform.parent.gameObject;
base.OnDragDropStart ();
}
/// <summary>
/// 重写父类的拖拽释放函数
/// </summary>
protected override void OnDragDropRelease (GameObject surface)
{
//如果不是拖拽到场景表面的话
if (!surface.name.Equals ("UI Root")) {
//寻找surface对象的父对象
GameObject cell = surface.transform.parent.gameObject; //判断当前单元格的对象姓名
if (cell.name.Equals ("PacketCell - Left")) {
GMUserManager.User.Name = "Left";
}
if (cell.name.Equals ("PacketCell - Right")) {
GMUserManager.User.Name = "Right";
}
} else {
//其他的错误位置时,重置父子关系
this.transform.parent = this.sourceParent.transform;
} //最终调用父类的功能
base.OnDragDropRelease(surface);
//调整位置
this.transform.localPosition = new Vector3(0,0,0);
}
}

Test.script

挂载在MainCamera对象上的脚本

using UnityEngine;
using System.Collections; public class TestScript : MonoBehaviour { //指向游戏中间的那个label控件
public UILabel label;
// Use this for initialization
void Start () { } // Update is called once per frame
void Update ()
{
this.label.text = GMUserManager.User.Name;
}
}

工程存放位置

[Unity]背包效果-使用NGUI实现物品的拖拽效果Drag的更多相关文章

  1. Unity实现放大缩小以及相机位置平移实现拖拽效果

    放大缩小功能是游戏开发中用到的功能,今天就来讲一下Unity中放大缩小怎么实现. 1.IDragHandler, IBeginDragHandler, IEndDragHandler这三个接口是Uni ...

  2. jQuery的DOM操作实例(2)——拖拽效果&&拓展插件

    一.原生JavaScript编写拖拽效果 二.jQuery编写的拖拽效果 三.在jQuery中拓展一个拖拽插件

  3. React.js实现原生js拖拽效果及思考

    一.起因&思路 不知不觉,已经好几天没写博客了...近来除了研究React,还做了公司官网... 一直想写一个原生js拖拽效果,又加上近来学react学得比较嗨.所以就用react来实现这个拖 ...

  4. js拖拽效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. WinForm支持拖拽效果

    有一个MSDN客户提问在WinForm中如何实现拖拽效果——比如在WinForm中有一个Button,我要实现的效果是拖拽这个Button到目标位置后生成一个该控件的副本. 其实这个操作主要分成三步走 ...

  6. js div浮动层拖拽效果代码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. JS实现漂亮的窗口拖拽效果(可改变大小、最大化、最小化、关闭)

    转自<JS实现漂亮的窗口拖拽效果(可改变大小.最大化.最小化.关闭)>:http://www.jb51.net/article/73157.htm   这篇文章主要介绍了JS实现漂亮的窗口 ...

  8. JQ实现3D拖拽效果

    <!DOCTYPE HTML> <html onselectstart='return false'> <head> <meta http-equiv=&qu ...

  9. 用JS实现版面拖拽效果

    类似于这样的一个版面,点击标题栏,实现拖拽效果. 添加onmousedown事件 通过获取鼠标的坐标(clientX,clientY)来改变面板的位置 注意:面板使用绝对定位方式,是以左上角为参考点, ...

随机推荐

  1. 在C#中使用反射调用internal的方法

    MSDN上解释Internal如下: The internal keyword is an access modifier for types and type members. Internal t ...

  2. LeetCode OJ 160. Intersection of Two Linked Lists

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  3. virtualenv 管理python 环境

    virualenvvirtualenv用于创建独立的Python环境,多个Python相互独立,互不影响,它能够:1. 在没有权限的情况下安装新套件2. 不同应用可以使用不同的套件版本3. 套件升级不 ...

  4. iptables查看、添加、删除规则

    1.查看iptables -nvL –line-number -L 查看当前表的所有规则,默认查看的是filter表,如果要查看NAT表,可以加上-t NAT参数-n 不对ip地址进行反查,加上这个参 ...

  5. replication across two data centers

    http://andyhan.net/index.php/sys-adm/item/291-hbase-replication http://shitouer.cn/2013/04/hbase-mul ...

  6. ref与out之间的区别整理 摘自与望楼http://blog.csdn.net/xiaoning8201/article/details/6893154

    ref和out都是C#中的关键字,所实现的功能也差不多,都是指定一个参数按照引用传递. 对于编译后的程序而言,它们之间没有任何区别,也就是说它们只有语法区别. 总结起来,他们有如下语法区别: .ref ...

  7. table详解

    1.tr 元素定义表格行,th 元素定义表头,td 元素定义表格单元. tr内是th还是td可由自己定义,th,td可存在于任一行,th与td的区别在与th字体更粗 2.定义一个table默认有bor ...

  8. translucent 属性

    <pre name="code" class="objc">//适配ios7 if( ([[[UIDevice currentDevice] sys ...

  9. Category / Extention / 属性 / 成员变量 /

    转载自:http://blog.csdn.net/itianyi/article/details/8618128 在ios第一版中,我们为输出口同时声明了属性和底层实例变量,那时,属性是oc语言的一个 ...

  10. WinRAR5.31 注册码

    RAR registration dataState Grid Corporation Of China50000 PC usage licenseUID=5827a0bd1c43525d0a5d64 ...