using UnityEngine;
using System.Collections; public class ins1 : MonoBehaviour
{ //bool isTarget = false;
private Vector3 world;
private Vector3 screenpos;
private Vector3 mousepos;
private Vector3 offset;
public RaycastHit hit;
private Vector3 obj_pos;
private GameObject obj;
//private int speed;
//LayerMask mask_3 = 1 << 10;
//LayerMask mask_2 = 11 << 13;
// Use this for initialization
void Start()
{
StartCoroutine(move());
}
void Update()
{
if (Input.GetMouseButtonDown())
{
Ray();
}
}
IEnumerator move()
{
screenpos = Camera.main.WorldToScreenPoint(this.transform.position); offset = this.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenpos.z));
while (true)
{
mousepos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenpos.z);
world = Camera.main.ScreenToWorldPoint(mousepos) + offset;
this.transform.position = world;
yield return new WaitForFixedUpdate();
}
}
public void Ray()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//RaycastHit hit;
GameObject hitobj = null;
if (Physics.Raycast(ray, out hit))
{
hitobj = hit.collider.gameObject;
print(hitobj.name); if (hit.collider.gameObject.name == "plane")
{
obj_pos = hit.point;
obj = (GameObject)Instantiate(this.gameObject);
obj.transform.position = obj_pos;
Destroy(obj.GetComponent<ins1>());
Destroy(this.gameObject);
}
}
else
Destroy(this.gameObject); } }

  

主要实现选择label 显示图片 拖拽物体的功能。

预览:

过程:(1)添加label attach boxcollider UIbutton

添加sprite 如图

还有plane

(2)为label绑定脚本

using UnityEngine;
using System.Collections; public class getText : MonoBehaviour { private UILabel label;
public UIAtlas atlass;
public UISprite sprite;
public GameObject[] cells; // Use this for initialization
void Start () { } // Update is called once per frame
void Update () { }
public void get()
{
label=this.GetComponent<UILabel>();
show(label.text);
}
public void show(string name)
{
for (int i = 0; i < cells.Length; i++)
{
if (cells[i].GetComponent<UISprite>().spriteName.Equals("Right Bracket"))//图集默认图片 {
sprite = cells[i].GetComponent<UISprite>(); sprite.atlas = atlass;
sprite.spriteName = name;
print(name);
break;
}
} }
}

  为sprite绑定脚本

using UnityEngine;
using System.Collections; public class listen : MonoBehaviour
{
public GameObject[] obj;
private Vector3 screenpos;
private Vector3 mousepos;
private Vector3 world; // Use this for initialization
void Start()
{ } // Update is called once per frame
void Update()
{ }
void Awake()
{
UIEventListener.Get(this.gameObject).onClick += buttonclick;
} void buttonclick(GameObject button)
{
screenpos = Camera.main.WorldToScreenPoint(this.gameObject.transform.position);
mousepos = Input.mousePosition;
mousepos.z = screenpos.z;
world = Camera.main.ScreenToWorldPoint(mousepos);
string name = button.GetComponent<UISprite>().spriteName;
for (int i = 0; i < obj.Length; i++)
{
if (name == obj[i].name)
{ GameObject product = (GameObject)Instantiate(obj[i]);
product.transform.position = world; this.GetComponent<UISprite>().spriteName = "Right Bracket";
product.tag = "obj";
product.layer =10;
product.AddComponent<ins1>();
} }
}
}

  ins1脚本:

NGUI-学习笔记(2)一个项目需求的更多相关文章

  1. django学习笔记二:一个项目多个App项目搭建

    django充许在一个项目中存在多个app,如一个大门户网站中可以包含论坛,新闻等内容,其中每一个模块称之为一个App,也可以理解为一个个独立的小型项目最终集成在一个门户网站中最终呈现给用户 本次测试 ...

  2. ASP.NET MVC Web API 学习笔记---第一个Web API程序

    http://www.cnblogs.com/qingyuan/archive/2012/10/12/2720824.html GetListAll /api/Contact GetListBySex ...

  3. NGUI学习笔记汇总

    NGUI学习笔记汇总,适用于NGUI2.x,NGUI3.x 一.NGUI的直接用法 1. Attach a Collider:表示为NGUI的某些物体添加碰撞器,如果界面是用NGUI做的,只能这样添加 ...

  4. NGUI 学习笔记实战之二——商城数据绑定(Ndata)

    上次笔记实现了游戏商城的UI界面,没有实现动态数据绑定,所以是远远不够的.今天采用NData来做一个商城. 如果你之前没看过,可以参考上一篇博客   NGUI 学习笔记实战——制作商城UI界面  ht ...

  5. NGUI 学习笔记实战——制作商城UI界面

    http://www.cnblogs.com/chongxin/p/3876575.html Unity3D的uGUI听说最近4.6即将推出,但是目前NGUI等UI插件大行其道并且已经非常成熟,所以我 ...

  6. golang学习笔记6 beego项目路由设置

    golang学习笔记5 beego项目路由设置 前面我们已经创建了 beego 项目,而且我们也看到它已经运行起来了,那么是如何运行起来的呢?让我们从入口文件先分析起来吧: package main ...

  7. Docker技术入门与实战 第二版-学习笔记-10-Docker Machine 项目-2-driver

    1>使用的driver 1〉generic 使用带有SSH的现有VM/主机创建机器. 如果你使用的是机器不直接支持的provider,或者希望导入现有主机以允许Docker Machine进行管 ...

  8. Scrapy:学习笔记(2)——Scrapy项目

    Scrapy:学习笔记(2)——Scrapy项目 1.创建项目 创建一个Scrapy项目,并将其命名为“demo” scrapy startproject demo cd demo 稍等片刻后,Scr ...

  9. Angular学习笔记—创建一个angular项目

    开始项目前,你需要先安装node和npm,然后执行npm install -g @angular/cli安装Angular CLI. 如何安装node.js和npm npm使用介绍 1.安装angul ...

  10. NGUI学习笔记(一)UILabel介绍

    来个前言: 作为一个U3D程序员,自然要写一写U3D相关的内容了.想来想去还是从UI开始搞起,可能这也是最直观同时也最重要的部分之一了.U3D自带的UI系统,也许略坑,也没有太多介绍的价值,那么从今天 ...

随机推荐

  1. UIimageView GIF动画

    1.代码如下 (注释都有) - (void)viewDidLoad { [super viewDidLoad]; UIImageView * bigImageView = [[UIImageView ...

  2. ios7--系统自带的向右滑动手势返回上一个界面

    当从控制器A push到控制器B,我们返回控制器A,除了使用按钮返回 [self.navigationController pushViewController:Vc animated:YES]; 还 ...

  3. CoreAnimation6-基于定时器的动画和性能调优

    基于定时器的动画 定时帧 动画看起来是用来显示一段连续的运动过程,但实际上当在固定位置上展示像素的时候并不能做到这一点.一般来说这种显示都无法做到连续的移动,能做的仅仅是足够快地展示一系列静态图片,只 ...

  4. LINQ 101——分组、Set、转换、Element

    一.Grouping(分组) 例1:对于0-9数按被3整除的结果分组 代码: static void Linq1() { , , , , , , , , , }; var numModBy3 = fr ...

  5. 数据库(学习整理)----3--Oracle创建表和设置约束

    BBS论坛表设计 包含的表:BBSusers(用户表),BBSsection(版块表),BBStopic(主贴表),BBSreply(跟帖表) 表结构 1)BBSusers 字段名 字段说明 数据类型 ...

  6. Linux下彻底卸载LibreOffice方法

    Linux下彻底卸载LibreOffice方法 终端中输入命令: 对所有基于 Debian 的发行版(Debian.Ubuntu.Kubuntu.Xubuntu.*buntu.Sidux 等): su ...

  7. tar、zip 、unzip 打包与压缩 (参考:http://pengyl.blog.51cto.com/5591604/1191197)

    通常都是先通过tar命令将多个文件或目录打包成一个包文件,然后再通过gzip或bzip2进行压缩,如*.tar.gz和*.tar.bz2就属于这种先打包再压缩的文件.在实际使用中,一般都是通过tar命 ...

  8. WPF之Treeview控件简单用法

    TreeView:表示显示在树结构中分层数据具有项目可展开和折叠的控件 TreeView 的内容是可以包含丰富内容的 TreeViewItem 控件,如 Button 和 Image 控件.TreeV ...

  9. 百度编辑器ueditor 使用

    ueditor 百度开源的一个 编辑器 ,支持api.扩展,demo丰富.推荐下 以前写 编辑 词典的使用 jquery-te  轻量级编辑器..当时看中了 它代码轻巧.容易改. 把他的功能改了好多. ...

  10. scala 安装

    http://www.scala-lang.org/download/install.html http://zh.scala-tour.com/#/hello-wolrd scala指南 To ru ...