上一次用UGUI实现了拼图小游戏,这次,我们来用NGUI来实现

实现原理

NGUI中提供了拖拽的基类UIDragDropItem,所以我们要做的就是在要拖拽的图片上加一个继承于该类的脚本,并实现其中的方法即可

 public class DragOnPic : UIDragDropItem {

     protected override void OnDragDropStart ()
{
base.OnDragDropStart ();
} protected override void OnDragDropRelease (GameObject surface)
{
base.OnDragDropRelease (surface);
}
}

拼图游戏实例

1、准备拼图素材,由于NGUI使用的Atlas为Texture,所以不能用UGUI中裁剪图片的方法,所以偷了一下懒,从网上找了一个小工具把图片裁剪了一下。。

2、给图片命名,为了最后检测简单一点,所以我这里统一命名为f-0~f-15,并制作Atlas

下面的基本操作步骤跟UGUI大同小异,所以我们这里搞一个不一样的方式,Unity中不做任何操作,只在摄像机上挂一个脚本ImageCreater来调用我们的代码

     void Start () {
//调用UIManager中生产图片的方法.
UIManager.Instance.CreatPics();
}

新建一个UIManager类,下面是该类的内容:

 1 public class UIManager  {
2
3 //单例.
4 private static UIManager instance;
5 public static UIManager Instance{
6 get{
7 if(instance == null)
8 {
9 instance = new UIManager();
10 }
11 return instance;
12 }
13 }
14 private UIManager(){}
15
16 //根节点.
17 UIPanel panel;
18
19 public void CreatPics()
20 {
21 panel = NGUITools.CreateUI(false);
22
23 //添加Grid组件用于自动排列图片.
24 UIGrid grid = NGUITools.AddChild<UIGrid>(panel.gameObject);
25
26 //设置grid各个属性.
27 grid.arrangement = UIGrid.Arrangement.Horizontal;
28 grid.maxPerLine = 4;
29 grid.cellWidth = 100;
30 grid.cellHeight = 100;
31 grid.pivot = UIWidget.Pivot.TopLeft;
32 grid.transform.localPosition = new Vector3(-150, 150, 0);
33
34 //从Resources文件夹中动态加载Atlas
35 UIAtlas myAtlas = Resources.Load<UIAtlas>("Atlas/MyAtlas");
36
37 //通过GameManager得到一个随机数数组.
38 int[] randomIndex = GamaManager.RandomArray();
39
40 //生成图片.
41 for (int i = 0; i < 16; i++) {
42 UISprite cell = NGUITools.AddChild<UISprite>(grid.gameObject);
43
44 //设置图片容器的Atlas及图片名称.
45 cell.atlas = myAtlas;
46 cell.spriteName = "box";
47 cell.name = "f-" + i.ToString();
48
49 //添加UIDragDropContainer组件用于接收图片.
50 UIDragDropContainer container = NGUITools.AddMissingComponent<UIDragDropContainer>(cell.gameObject);
51 container.reparentTarget = cell.transform;
52
53 //添加显示图片的sprite.
54 UISprite sprite = NGUITools.AddChild<UISprite>(cell.gameObject);
55 sprite.atlas = myAtlas;
56 sprite.spriteName = "f-" + randomIndex[i];
57
58 sprite.tag = "Cell";
59
60 //设置sprite的depth使其能显示在上方.
61 sprite.depth = cell.depth + 1;
62
63 //为图片添加BoxCollider组件用于鼠标交互,并重新设置它的大小与图片大小一致.
64 NGUITools.AddMissingComponent<BoxCollider>(sprite.gameObject);
65 sprite.autoResizeBoxCollider = true;
66 sprite.ResizeCollider();
67
68 //添加我们自己写的DragOnPic脚本用于实现拖拽功能.
69 NGUITools.AddMissingComponent<DragOnPic>(sprite.gameObject);
70 }
71 }
72
73 }

拖拽脚本:

 public class DragOnPic : UIDragDropItem {

     UISprite _sprite;

     Transform myParent;

     void OnEnable()
{
_sprite = this.GetComponent<UISprite>();
} protected override void OnDragDropStart ()
{
//开始拖拽时增加depth,是其能显示在别的图片上方.
_sprite.depth += ; //开始拖拽时记下自己的父物体.
myParent = this.transform.parent; //父类的方法.
base.OnDragDropStart ();
} protected override void OnDragDropRelease (GameObject surface)
{
//父类的方法.
base.OnDragDropRelease (surface); //松开鼠标时如果是另一张图片,则交换两张图片的位置,否则重置自己的位置.
if(surface.tag == "Cell")
{
this.transform.SetParent(surface.transform.parent);
surface.transform.SetParent(myParent);
this.transform.localPosition = Vector3.zero;
surface.transform.localPosition = Vector3.zero;
}
else {
this.transform.localPosition = Vector3.zero;
} //拖拽结束时判断是否完成拼图.
if(GamaManager.CheckWin())
{
NGUIDebug.Log("Win!!!!");
} //结束拖拽时重置depth.
_sprite.depth -= ;
} }

GameManager中判断是否完成拼图分方法跟UGUI中的类似,这里就不多写了~~~

好了,大功告成!

网页版预览

PC版下载

使用NGUI实现拖拽功能(拼图小游戏)的更多相关文章

  1. 使用UGUI实现拖拽功能(拼图小游戏)

    实现方式 1.引入UGUI自带的事件系统 UnityEngine.EventSystems 2.为我们的类添加接口 IBeginDragHandler, IDragHandler, IEndDragH ...

  2. canvas drag 实现拖拽拼图小游戏

    博主一直心心念念想做一个小游戏-  前端时间终于做了一个小游戏,直到现在才来总结,哈哈- 以后要勤奋点更新博客! 实现原理 1.如何切图? 用之前的方法就是使用photoshop将图片切成相应大小的图 ...

  3. React Editor 应用编辑器(1) - 拖拽功能剖析

    这是可视化编辑器 Gaea-Editor 的第一篇连载分析文章,希望我能在有限的篇幅讲清楚制作这个网页编辑器的动机,以及可能带来的美好使用前景(画大饼).它会具有如下几个特征: 运行在网页 文档流布局 ...

  4. JQuery UI的拖拽功能

    JQuery UI是JQuery官方支持的WebUI 代码库,包含底层交互.动画.特效等API,并且封装了一些Web小部件(Widget).同时,JQuery UI继承了jquery的插件支持,有大量 ...

  5. Js元素拖拽功能实现

    Js元素拖拽功能实现 需要解决的问题 最近项目遇到了一个问题,就是用户某个操作需要弹出一个自定义的内容输入框,但是有个缺点,当浏览太大的时候没办法点击确认和取消按钮,应为这个弹出框是采用绝对定位的,取 ...

  6. (Demo分享)利用JavaScript(JS)实现一个九宫格拖拽功能

    利用JavaScript(JS)实现一个九宫格拖拽功能   Demo实现了对任意方格进行拖拽,可以交换位置,其中Demo-1利用了勾股定理判断距离! Demo-1整体思路: 1.首先div实现自由移动 ...

  7. html5中的拖拽功能

    拖拽元素支持的事件 ondrag 应用于拖拽元素,整个拖拽过程都会调用 ondragstart 应用于拖拽元素,当拖拽开始时调用 ondragleave 应用于拖拽元素,当鼠标离开拖拽元素是调用 on ...

  8. 使用 vue3 的自定义指令给 element-plus 的 el-dialog 增加拖拽功能

    element-plus 提供的 el-dialog 对话框功能非常强大,只是美中不足不能通过拖拽的方式改变位置,有点小遗憾,那么怎么办呢?我们可以通过 vue 的自定义指令来实现一个可以拖拽的对话框 ...

  9. RCP:拖拽功能的实现 Drag and Drop

    SWT中的拖拽是使用的org.eclipse.swt.dnd. 有三个需要密切注意的类: 1.DragSource 2.DropTarget 3.Transfer DragSource封装了需要被拖拽 ...

随机推荐

  1. 【锋利的Jquery】读书笔记七

    第七章  jquery插件 管理cookie的插件--cookie jquery插件太多没什么好讲的,百度太多 说以下 cookie插件 <!DOCTYPE html> <html& ...

  2. Selenium也是一个用于Web应用程序测试的工具

    Selenium也是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE.Mozilla Firefox.Mozilla Suite ...

  3. Lua 日志

    Lua 环境安装 编辑调试Lua脚本

  4. IIS8中使用OpenSSL来创建CA并且签发SSL证书

    前言 [转载]http://alvinhu.com/blog/2013/06/12/creating-a-certificate-authority-and-signing-the-ssl-certi ...

  5. vedio_note2

    例化 for example ex_cnt ex_cnt_inst( //模块名 例化的名字 .clk(ckl_o), // . 后面是原模块的名字 括号里面是top里的名字 .rst_n(rst_n ...

  6. C# 根据IP获取省市

    /// <summary> /// 根据IP获取省市 /// </summary> public void GetAddressByIp() { string ip = &qu ...

  7. sql注入示例

    实验指导说明 实验环境 • 实验环境 o 操作机:Windows XP o 目标机:Windows 2003 o 目标网址:www.test.ichunqiu • 实验工具: Tools Path S ...

  8. 30分钟学习sea.js使用指南

    : seajs如何解决? ①引入sea.js的库 <script src="../sea/sea.js" ></script> ②如何变成模块? defin ...

  9. 运行CUDA实例时候出现的问题

    问题一:>LINK : fatal error LNK1123: 转换到 COFF 期间失败:文件无效或损坏 将 项目——项目属性——配置属性——连接器——清单文件——嵌入清单 “是”改为“否” ...

  10. js将xml对象,xml文件解析成xml dom对象,来对对象进行操作

    由于ie与其他的浏览器对于xml文件的解析方式不同,所以有不同的解析方式 1.1 IE解析xml文件的方式 var xmlDoc=new ActiveXObject("Microsoft.X ...