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. js实现FileUpload选择图片后预览功能

    当asp.net的FileUpload选择一个图片后不需要上传就能显示出图片的预览功能, 代码: <%@ Page Language="C#" AutoEventWireup ...

  2. 将decimal类型的数值后面的0和.号去掉

    今天在群里面看到有朋友在问如下的需求,想到以前在写项目时也遇到这种处理数值的需求,所以写一个例子贴在博客里. 需求:在许多显示货币值时,可能需要截取掉后面的0,显示小数值或者整型值. 举例:(1)数据 ...

  3. WIN7 64位配置Oracle SQL Developer工具

    在使用Oracle SQL 的过程中,很多参考资料,辅导机构,各种书籍绝大多数都是使用PL/SQL进行讲解,但是问题是PL/SQL对WIN7 64位系统支持不好,网上有各种各样的配置教程,我尝试了很多 ...

  4. canvas之----浮动小球

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  5. 关于移动div的具体实现(js)

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

  6. 界面信息获取库的封装,记有爱UI助手的诞生

    距离上次写文章已经过去了10个月,这次把我最近做的一个东西的一些心得写下来吧. 上一篇文章写了MSAA技术对于QQ界面信息获取的相关知识,近一段时间我把这个技术包括一些其他的功能做了一个封装并准备公开 ...

  7. javascript ajax请求

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  8. TensorFlow和最近发布的slim

    笔者将和大家分享一个结合了TensorFlow和最近发布的slim库的小应用,来实现图像分类.图像标注以及图像分割的任务,围绕着slim展开,包括其理论知识和应用场景. 之前自己尝试过许多其它的库,比 ...

  9. #Leet Code# Unique Tree

    语言:Python 描述:使用递归实现 class Solution: # @return an integer def numTrees(self, n): : elif n == : else: ...

  10. decimall类型数据

    同样是decimal(18,5)   和 decimal(18,4)  在VB中经过几次转化过后,数据就有可能改变. 遇到的情况 decimal(18,5)到  decimal(18,4)转换过程中数 ...